Skip to content

Commit

Permalink
Implemented cancel_payment. Remove extraneous semicolons which came from
Browse files Browse the repository at this point in the history
switching between Python and JavaScript development too often.
Restructured where to_primitive() is called (again) to better fit
cancel_payment which requires a list of Operation objects.
  • Loading branch information
Colin 't Hart committed Jul 12, 2024
1 parent cdcf3a3 commit e3df0dc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='swish',
version='1.4',
version='1.5',
packages=find_packages(),
include_package_data=True,
license='MIT',
Expand Down
28 changes: 20 additions & 8 deletions swish/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .environment import Environment
from .exceptions import SwishError
from .models import Payment, Refund, CommerceQRCodeRequest
from .models import Payment, Refund, CommerceQRCodeRequest, Operation

try:
from requests.packages.urllib3.contrib import pyopenssl
Expand All @@ -26,21 +26,27 @@ def __post(self, endpoint, payload):
else:
base_url = self.environment.base_url
url = base_url + endpoint
return requests.post(url=url, json=payload.to_primitive(), headers={'Content-Type': 'application/json'},
return requests.post(url=url, json=payload, headers={'Content-Type': 'application/json'},
cert=self.cert, verify=self.verify)

def post(self, endpoint, payload):
warnings.warn("You shouldn't be calling this method. It will be private in a future version.", DeprecationWarning);
warnings.warn("You shouldn't be calling this method. It will be private in a future version.", DeprecationWarning)
self.__post(endpoint, payload)

def __get(self, endpoint, parameter):
url = self.environment.base_url + endpoint + '/' + str(parameter)
return requests.get(url, cert=self.cert, verify=self.verify)
return requests.get(url=url, cert=self.cert, verify=self.verify)

def get(self, endpoint, parameter):
warnings.warn("You shouldn't be calling this method. It will be private in a future version.", DeprecationWarning);
warnings.warn("You shouldn't be calling this method. It will be private in a future version.", DeprecationWarning)
self.__get(endpoint, parameter)

def __patch(self, endpoint, parameter, payload):
url = self.environment.base_url + endpoint + '/' + str(parameter)
return requests.patch(url=url, json=payload, headers={'Content-Type': 'application/json-patch+json'},
cert=self.cert, verify=self.verify)


def create_payment(self, amount, currency, callback_url, payee_payment_reference=None, message=None,
payer_alias=None):
payment_request = Payment({
Expand All @@ -53,7 +59,7 @@ def create_payment(self, amount, currency, callback_url, payee_payment_reference
'payer_alias': payer_alias
})

response = self.__post('paymentrequests', payment_request)
response = self.__post('paymentrequests', payment_request.to_primitive())
if response.status_code == 422:
raise SwishError(response.json())
response.raise_for_status()
Expand All @@ -67,6 +73,12 @@ def get_payment(self, payment_request_id):
response.raise_for_status()
return Payment(response.json())

def cancel_payment(self, payment_request_id):
operation = Operation()
response = self.__patch('paymentrequests', payment_request_id, [operation.to_primitive()])
response.raise_for_status()
return Payment(response.json())

def create_refund(self, original_payment_reference, amount, currency, callback_url, payer_payment_reference=None,
payment_reference=None, payee_alias=None, message=None):
refund_request = Refund({
Expand All @@ -81,7 +93,7 @@ def create_refund(self, original_payment_reference, amount, currency, callback_u
'message': message
})

response = self.__post('refunds', refund_request)
response = self.__post('refunds', refund_request.to_primitive())
if response.status_code == 422:
raise SwishError(response.json())
response.raise_for_status()
Expand All @@ -104,6 +116,6 @@ def commerce_qr_code(self, token, format, size=None, border=None, transparent=No
'transparent': transparent
})

response = self.__post('commerce', commerce_qr_code_request)
response = self.__post('commerce', commerce_qr_code_request.to_primitive())
response.raise_for_status()
return response.content
5 changes: 5 additions & 0 deletions swish/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ class CommerceQRCodeRequest(models.Model):

class Options:
serialize_when_none = False

class Operation(models.Model):
op = types.StringType(required=True, choices=('replace',), default='replace'),
path = types.StringType(required=True, choices=('/status',), default='/status'),
value = types.StringType(required=True, choices=('cancelled',), default='cancelled')

0 comments on commit e3df0dc

Please sign in to comment.