Skip to content

Commit b5865a7

Browse files
authored
Merge pull request #18 from MadSpindel/17-deprecation-of-v1-api-2025-10-01
17 deprecation of v1 api 2025 10 01
2 parents e3df0dc + ea653a0 commit b5865a7

5 files changed

Lines changed: 147 additions & 113 deletions

File tree

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PHONY: install
2+
3+
install:
4+
python setup.py install

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name='swish',
12-
version='1.5',
12+
version='1.6',
1313
packages=find_packages(),
1414
include_package_data=True,
1515
license='MIT',

swish/client.py

Lines changed: 98 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import warnings
1+
import uuid
22

33
import requests
44

@@ -20,101 +20,137 @@ def __init__(self, environment, merchant_swish_number, cert, verify=False):
2020
self.cert = cert
2121
self.verify = verify
2222

23+
def __get(self, endpoint, parameter):
24+
url = self.environment.base_url + endpoint + '/' + str(parameter)
25+
return requests.get( url = url
26+
, cert = self.cert
27+
, verify = self.verify
28+
)
29+
2330
def __post(self, endpoint, payload):
2431
if endpoint == 'commerce':
2532
base_url = self.environment.qr_url
2633
else:
2734
base_url = self.environment.base_url
2835
url = base_url + endpoint
29-
return requests.post(url=url, json=payload, headers={'Content-Type': 'application/json'},
30-
cert=self.cert, verify=self.verify)
31-
32-
def post(self, endpoint, payload):
33-
warnings.warn("You shouldn't be calling this method. It will be private in a future version.", DeprecationWarning)
34-
self.__post(endpoint, payload)
35-
36-
def __get(self, endpoint, parameter):
37-
url = self.environment.base_url + endpoint + '/' + str(parameter)
38-
return requests.get(url=url, cert=self.cert, verify=self.verify)
36+
return requests.post( url = url
37+
, json = payload
38+
, cert = self.cert
39+
, verify = self.verify)
3940

40-
def get(self, endpoint, parameter):
41-
warnings.warn("You shouldn't be calling this method. It will be private in a future version.", DeprecationWarning)
42-
self.__get(endpoint, parameter)
41+
def __put(self, endpoint, payload):
42+
if endpoint == 'commerce':
43+
base_url = self.environment.qr_url
44+
else:
45+
base_url = self.environment.base_url
46+
url = base_url + endpoint
47+
return requests.put( url = url
48+
, json = payload
49+
, cert = self.cert
50+
, verify = self.verify
51+
)
4352

4453
def __patch(self, endpoint, parameter, payload):
4554
url = self.environment.base_url + endpoint + '/' + str(parameter)
46-
return requests.patch(url=url, json=payload, headers={'Content-Type': 'application/json-patch+json'},
47-
cert=self.cert, verify=self.verify)
48-
49-
50-
def create_payment(self, amount, currency, callback_url, payee_payment_reference=None, message=None,
51-
payer_alias=None):
52-
payment_request = Payment({
53-
'payee_alias': self.merchant_swish_number,
54-
'amount': amount,
55-
'currency': currency,
56-
'callback_url': callback_url,
57-
'payee_payment_reference': payee_payment_reference,
58-
'message': message,
59-
'payer_alias': payer_alias
60-
})
61-
62-
response = self.__post('paymentrequests', payment_request.to_primitive())
55+
return requests.patch( url = url
56+
, json = payload
57+
, cert = self.cert
58+
, verify = self.verify
59+
)
60+
61+
62+
def create_payment( self
63+
, amount
64+
, currency
65+
, callback_url
66+
, callback_identifier = None
67+
, payee_payment_reference = None
68+
, message = None
69+
, payer_alias = None
70+
):
71+
instruction_uuid = str(uuid.uuid4()).replace('-','').upper()
72+
payment_request = Payment({ 'payee_alias' : self.merchant_swish_number
73+
, 'amount' : amount
74+
, 'currency' : currency
75+
, 'callback_url' : callback_url
76+
, 'callback_identifier' : callback_identifier
77+
, 'payee_payment_reference' : payee_payment_reference
78+
, 'message' : message
79+
, 'payer_alias' : payer_alias
80+
})
81+
82+
response = self.__put(f'v2/paymentrequests/{instruction_uuid}', payment_request.to_primitive())
6383
if response.status_code == 422:
6484
raise SwishError(response.json())
6585
response.raise_for_status()
6686

67-
return Payment({'id': response.headers.get('Location').split('/')[-1],
68-
'location': response.headers.get('Location'),
69-
'request_token': response.headers.get('PaymentRequestToken')})
87+
return Payment({ 'id' : response.headers.get('Location').split('/')[-1]
88+
, 'location' : response.headers.get('Location')
89+
, 'request_token' : response.headers.get('PaymentRequestToken')
90+
})
7091

7192
def get_payment(self, payment_request_id):
72-
response = self.__get('paymentrequests', payment_request_id)
93+
response = self.__get('v1/paymentrequests', payment_request_id)
7394
response.raise_for_status()
7495
return Payment(response.json())
7596

7697
def cancel_payment(self, payment_request_id):
7798
operation = Operation()
78-
response = self.__patch('paymentrequests', payment_request_id, [operation.to_primitive()])
99+
response = self.__patch('v1/paymentrequests', payment_request_id, [operation.to_primitive()])
79100
response.raise_for_status()
80101
return Payment(response.json())
81102

82-
def create_refund(self, original_payment_reference, amount, currency, callback_url, payer_payment_reference=None,
83-
payment_reference=None, payee_alias=None, message=None):
84-
refund_request = Refund({
85-
'payer_alias': self.merchant_swish_number,
86-
'payee_alias': payee_alias,
87-
'original_payment_reference': original_payment_reference,
88-
'amount': amount,
89-
'currency': currency,
90-
'callback_url': callback_url,
91-
'payer_payment_reference': payer_payment_reference,
92-
'payment_reference': payment_reference,
93-
'message': message
94-
})
95-
96-
response = self.__post('refunds', refund_request.to_primitive())
103+
def create_refund( self
104+
, original_payment_reference
105+
, amount
106+
, currency
107+
, callback_url
108+
, instructionUUID
109+
, payer_payment_reference = None
110+
, payment_reference = None
111+
, payee_alias = None
112+
, message = None
113+
):
114+
refund_request = Refund({ 'payer_alias': self.merchant_swish_number
115+
, 'payee_alias': payee_alias
116+
, 'original_payment_reference' : original_payment_reference
117+
, 'amount' : amount
118+
, 'currency' : currency
119+
, 'callback_url' : callback_url
120+
, 'callback_identifier' : instructionUUID
121+
, 'payer_payment_reference' : payer_payment_reference
122+
, 'payment_reference' : payment_reference
123+
, 'message' : message
124+
})
125+
126+
response = self.__put(f'v2/refunds/{instructionUUID}', refund_request.to_primitive())
97127
if response.status_code == 422:
98128
raise SwishError(response.json())
99129
response.raise_for_status()
100130

101-
return Refund({'id': response.headers.get('Location').split('/')[-1],
102-
'location': response.headers.get('Location')})
131+
return Refund({ 'id' : response.headers.get('Location').split('/')[-1]
132+
, 'location' : response.headers.get('Location')
133+
})
103134

104135
def get_refund(self, refund_id):
105-
response = self.__get('refunds', refund_id)
136+
response = self.__get('v1/refunds', refund_id)
106137
response.raise_for_status()
107138
return Refund(response.json())
108139

109140

110-
def commerce_qr_code(self, token, format, size=None, border=None, transparent=None):
111-
commerce_qr_code_request = CommerceQRCodeRequest({
112-
'token': token,
113-
'format': format,
114-
'size': size,
115-
'border': border,
116-
'transparent': transparent
117-
})
141+
def commerce_qr_code( self
142+
, token
143+
, format
144+
, size = None
145+
, border = None
146+
, transparent = None
147+
):
148+
commerce_qr_code_request = CommerceQRCodeRequest({ 'token' : token
149+
, 'format' : format
150+
, 'size' : size
151+
, 'border' : border
152+
, 'transparent' : transparent
153+
})
118154

119155
response = self.__post('commerce', commerce_qr_code_request.to_primitive())
120156
response.raise_for_status()

swish/environment.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,23 @@ def parse_environment(environment):
2222

2323
Environment.MSS = Environment(
2424
name="mss",
25-
base_url="https://mss.cpc.getswish.net/swish-cpcapi/api/v1/",
25+
base_url="https://mss.cpc.getswish.net/swish-cpcapi/api/",
2626
qr_url=None ## MSS doesn't need or have QR codes
2727
)
28+
2829
Environment.Sandbox = Environment(
2930
name="sandbox",
30-
base_url="https://staging.getswish.pub.tds.tieto.com/swish-cpcapi/api/v1/",
31+
base_url="https://staging.getswish.pub.tds.tieto.com/swish-cpcapi/api/",
3132
qr_url="https://staging.getswish.pub.tds.tieto.com/qrg-swish/api/v1/"
3233
)
34+
3335
Environment.Production = Environment(
3436
name="production",
35-
base_url="https://cpc.getswish.net/swish-cpcapi/api/v1/",
37+
base_url="https://cpc.getswish.net/swish-cpcapi/api/",
3638
qr_url="https://mpc.getswish.net/qrg-swish/api/v1/"
3739
)
3840

39-
# deprecated
40-
Environment.Test = Environment(
41-
name="test",
42-
base_url="https://mss.cpc.getswish.net/swish-cpcapi/api/v1/",
43-
qr_url=None
44-
)
45-
4641
Environment.All = {
47-
"test": Environment.Test,
4842
"mss": Environment.MSS,
4943
"sandbox": Environment.Sandbox,
5044
"production": Environment.Production

swish/models.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,62 @@
22

33

44
class Payment(models.Model):
5-
id = types.StringType()
5+
id = types.StringType()
66
payee_payment_reference = types.StringType(serialized_name='payeePaymentReference')
7-
payment_reference = types.StringType(serialized_name='paymentReference')
8-
callback_identifier = types.StringType(serialized_name='callbackIdentifier')
9-
callback_url = types.URLType(serialized_name='callbackUrl')
10-
payer_alias = types.StringType(serialized_name='payerAlias')
11-
payee_alias = types.StringType(serialized_name='payeeAlias')
12-
amount = types.FloatType()
13-
currency = types.StringType()
14-
message = types.StringType()
15-
status = types.StringType()
16-
date_created = types.DateTimeType(serialized_name='dateCreated')
17-
date_paid = types.DateTimeType(serialized_name='datePaid')
18-
location = types.URLType()
19-
request_token = types.StringType()
20-
error_code = types.StringType(serialized_name='errorCode')
21-
error_message = types.StringType(serialized_name='errorMessage')
7+
payment_reference = types.StringType(serialized_name='paymentReference')
8+
callback_identifier = types.StringType(serialized_name='callbackIdentifier')
9+
callback_url = types.URLType(serialized_name='callbackUrl')
10+
payer_alias = types.StringType(serialized_name='payerAlias')
11+
payee_alias = types.StringType(serialized_name='payeeAlias')
12+
amount = types.FloatType()
13+
currency = types.StringType()
14+
message = types.StringType()
15+
status = types.StringType()
16+
date_created = types.DateTimeType(serialized_name='dateCreated')
17+
date_paid = types.DateTimeType(serialized_name='datePaid')
18+
location = types.URLType()
19+
request_token = types.StringType()
20+
error_code = types.StringType(serialized_name='errorCode')
21+
error_message = types.StringType(serialized_name='errorMessage')
2222

2323
class Options:
2424
serialize_when_none = False
2525

2626

2727
class Refund(models.Model):
28-
id = types.StringType()
28+
id = types.StringType()
2929
original_payment_reference = types.StringType(serialized_name='originalPaymentReference')
30-
amount = types.FloatType()
31-
currency = types.StringType()
32-
message = types.StringType()
33-
payer_alias = types.StringType(serialized_name='payerAlias')
34-
payee_alias = types.StringType(serialized_name='payeeAlias')
35-
callback_identifier = types.StringType(serialized_name='callbackIdentifier')
36-
callback_url = types.URLType(serialized_name='callbackUrl')
37-
payer_payment_reference = types.StringType(serialized_name='payerPaymentReference')
38-
payment_reference = types.StringType(serialized_name='paymentReference')
39-
status = types.StringType()
40-
date_created = types.DateTimeType(serialized_name='dateCreated')
41-
date_paid = types.DateTimeType(serialized_name='datePaid')
42-
location = types.URLType()
43-
error_code = types.StringType(serialized_name='errorCode')
44-
error_message = types.StringType(serialized_name='errorMessage')
45-
additional_information = types.StringType(serialized_name='additionalInformation')
30+
amount = types.FloatType()
31+
currency = types.StringType()
32+
message = types.StringType()
33+
payer_alias = types.StringType(serialized_name='payerAlias')
34+
payee_alias = types.StringType(serialized_name='payeeAlias')
35+
callback_identifier = types.StringType(serialized_name='callbackIdentifier')
36+
callback_url = types.URLType(serialized_name='callbackUrl')
37+
payer_payment_reference = types.StringType(serialized_name='payerPaymentReference')
38+
payment_reference = types.StringType(serialized_name='paymentReference')
39+
status = types.StringType()
40+
date_created = types.DateTimeType(serialized_name='dateCreated')
41+
date_paid = types.DateTimeType(serialized_name='datePaid')
42+
location = types.URLType()
43+
error_code = types.StringType(serialized_name='errorCode')
44+
error_message = types.StringType(serialized_name='errorMessage')
45+
additional_information = types.StringType(serialized_name='additionalInformation')
4646

4747
class Options:
4848
serialize_when_none = False
4949

5050
class CommerceQRCodeRequest(models.Model):
51-
token = types.StringType(required=True)
52-
format = types.StringType(required=True, choices=('jpg','png','svg'), default='svg')
53-
size = types.IntType()
54-
border = types.IntType()
51+
token = types.StringType(required=True)
52+
format = types.StringType(required=True, choices=('jpg','png','svg'), default='svg')
53+
size = types.IntType()
54+
border = types.IntType()
5555
transparent = types.BooleanType()
5656

5757
class Options:
5858
serialize_when_none = False
5959

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

0 commit comments

Comments
 (0)