Skip to content

Commit 58146c1

Browse files
committed
PaypalProvider: allow to make requests without payment parameter
1 parent a3f7943 commit 58146c1

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

payments/paypal/__init__.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ def http_request(self, payment, *args, http_method=None, **kwargs):
127127
except ValueError:
128128
data = {}
129129
if 400 <= response.status_code <= 500:
130-
self.set_error_data(payment, data)
130+
if payment is not None:
131+
self.set_error_data(payment, data)
131132
logger.debug(data)
132133
message = "Paypal error"
133134
if response.status_code == 400:
@@ -139,9 +140,11 @@ def http_request(self, payment, *args, http_method=None, **kwargs):
139140
message = error_data.get("message", message)
140141
else:
141142
logger.warning(message, extra={"status_code": response.status_code})
142-
payment.change_status(PaymentStatus.ERROR, message)
143+
if payment is not None:
144+
payment.change_status(PaymentStatus.ERROR, message)
143145
raise PaymentError(message)
144-
self.set_response_data(payment, data)
146+
if payment is not None:
147+
self.set_response_data(payment, data)
145148
return data
146149

147150
def post(self, payment, *args, **kwargs):
@@ -157,17 +160,18 @@ def get_last_response(self, payment, is_auth=False):
157160
return extra_data.get("response", {})
158161

159162
def get_access_token(self, payment):
160-
last_auth_response = self.get_last_response(payment, is_auth=True)
161-
created = payment.created
162-
now = timezone.now()
163-
if (
164-
"access_token" in last_auth_response
165-
and "expires_in" in last_auth_response
166-
and (created + timedelta(seconds=last_auth_response["expires_in"])) > now
167-
):
168-
return "{} {}".format(
169-
last_auth_response["token_type"], last_auth_response["access_token"]
170-
)
163+
if payment is not None:
164+
last_auth_response = self.get_last_response(payment, is_auth=True)
165+
created = payment.created
166+
now = timezone.now()
167+
if (
168+
"access_token" in last_auth_response
169+
and "expires_in" in last_auth_response
170+
and (created + timedelta(seconds=last_auth_response["expires_in"])) > now
171+
):
172+
return "{} {}".format(
173+
last_auth_response["token_type"], last_auth_response["access_token"]
174+
)
171175
headers = {"Accept": "application/json", "Accept-Language": "en_US"}
172176
post = {"grant_type": "client_credentials"}
173177
response = requests.post(
@@ -178,8 +182,9 @@ def get_access_token(self, payment):
178182
)
179183
response.raise_for_status()
180184
data = response.json()
181-
last_auth_response.update(data)
182-
self.set_response_data(payment, last_auth_response, is_auth=True)
185+
if payment is not None:
186+
last_auth_response.update(data)
187+
self.set_response_data(payment, last_auth_response, is_auth=True)
183188
return "{} {}".format(data["token_type"], data["access_token"])
184189

185190
def get_transactions_items(self, payment):

0 commit comments

Comments
 (0)