Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to preselect payment method and some other tweaks #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
dist
django_saferpay.egg-info/
__pycache__/
43 changes: 30 additions & 13 deletions saferpay/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
TRANSACTION_CANCEL='/Payment/v1/Transaction/Cancel'
)
API_PATHS_LOOKUP = {v: k for k, v in API_PATHS.items()}
SPECVERSION = 1.9
SPECVERSION = "1.20"


class SaferpayService:
def __init__(self, order_id=None, notify_token='', amount=None, currency=None,
def __init__(self, order_id=None, notify_token='', amount=None, currency=None, payment_methods=None,
language_code='en', token=None, sp_trans=None):
self.order_id, self.amount, self.currency = (
order_id, amount, currency
self.order_id, self.amount, self.currency, self.payment_methods = (
order_id, amount, currency, payment_methods
)
self.language_code = language_code
self.FORCE_LIABILITY_SHIFT_ACTIVE = getattr(
settings, 'SAFERPAY_FORCE_LIABILITYSHIFT_ACTIVE', False
)
self.DO_NOTIFY = getattr(
self.DO_NOTIFY = getattr( # according to the docs, this is only relevant to Paydirekt transactions
settings, 'SAFERPAY_DO_NOTIFY', False
)
self.ORDER_TEXT_NR = getattr(settings, 'SAFERPAY_ORDER_TEXT_NR', 'Order nr. %s')
Expand All @@ -55,6 +55,7 @@ def __init__(self, order_id=None, notify_token='', amount=None, currency=None,
self._next_url, self.sp_trans, self.token, self.notify_token = (
None, sp_trans, token, notify_token
)
self.capture_response_payload, self.assert_response_payload, init_response_payload = (None, None, None)

@classmethod
def init_from_transaction(cls, token=None):
Expand Down Expand Up @@ -86,7 +87,7 @@ def _new_saferpay_response(
)
sp_res.save()

def payload_init(self, billing_address):
def payload_init(self, billing_address, register_alias=False):
payload = {
'RequestHeader': {
'SpecVersion': SPECVERSION,
Expand All @@ -101,6 +102,7 @@ def payload_init(self, billing_address):
'CurrencyCode': self.currency
},
'OrderId': self.order_id,
'PayerNote': "Ref: {}".format(self.order_id),
'BillingAddress': billing_address,
'Description': self.ORDER_TEXT_NR % self.order_id,
},
Expand All @@ -113,6 +115,17 @@ def payload_init(self, billing_address):
}
}

if register_alias:
# this alias can later be used to execute operations in SaferPay with the payment info
# only works with credit cards
payload['RegisterAlias'] = {
'IdGenerator': 'RANDOM_UNIQUE',
'Lifetime': getattr(settings, "SAFERPAY_ALIAS_LIFETIME", 1600)
}

# PaymentMethods according to http://saferpay.github.io/jsonapi/#Payment_v1_Transaction_Initialize
if self.payment_methods:
payload['PaymentMethods'] = self.payment_methods
return payload

def add_do_notify(self, payload, notify_token):
Expand All @@ -134,13 +147,16 @@ def paymentpage_init(self, payload):
amount=self.amount, currency=self.currency, language_code=self.language_code
)
sp_trans.save()

self._new_saferpay_response(
'PAYMENTPAGE_INIT', payload, res, res_time, sp_trans, status_code=res.status_code
)
self.init_response_payload = res.json() # somebody might need the result synchronously in the instanc
self._next_url = data['RedirectUrl']
self.token = data['Token']
return data['Token']
elif res:
self.init_response_payload = res.json()
raise GatewayError('PAYMENTPAGE_INIT failed')
else:
print(res.status_code, res.text)
Expand Down Expand Up @@ -169,22 +185,21 @@ def paymentpage_assert(self):
self.sp_trans.transaction_id = res_data['Transaction']['Id']
self.sp_trans.status = res_data['Transaction']['Status']
self.sp_trans.save()
self.assert_response_payload = res.json() # somebody might need the result synchronously in the instance
self._new_saferpay_response(
'PAYMENTPAGE_ASSERT', payload, res, res_time, self.sp_trans,
status_code=res.status_code
)
if self.FORCE_LIABILITY_SHIFT_ACTIVE and 'Liability' in res_data \
and res_data['Liability']['LiabilityShift'] is False:
return self.transaction_cancel()
if res_data['Transaction']['Status'] == 'AUTHORIZED':
return self.transaction_capture()
elif res_data['Transaction']['Status'] == 'CAPTURED':
return 'CAPTURED'
return res_data['Transaction']['Status']
elif res:
self._new_saferpay_response(
'PAYMENTPAGE_ASSERT', payload, res, res_time, self.sp_trans,
status_code=res.status_code
)
self.assert_response_payload = res.json()
raise GatewayError('PAYMENTPAGE_ASSERT failed')
else:
raise PaymentError('PAYMENTPAGE_ASSERT')
Expand All @@ -210,17 +225,19 @@ def transaction_capture(self):
and res.json()['Status'] == 'CAPTURED':
self.sp_trans.status = 'CAPTURED'
self.sp_trans.save()
self.capture_response_payload = res.json() # someone might need the response synchronously in the instance
self._new_saferpay_response(
'TRANSACTION_CAPTURE', payload, res, res_time, self.sp_trans,
status_code=res.status_code
)
return 'CAPTURED'
elif res:
self._new_saferpay_response(
'PAYMENTPAGE_ASSERT', payload, res, res_time, self.sp_trans,
'TRANSACTION_CAPTURE', payload, res, res_time, self.sp_trans,
status_code=res.status_code
)
self.transaction_cancel()
self.capture_response_payload = res.json()
raise UnableToTakePayment('TRANSACTION_CAPTURE failed')
else:
raise PaymentError('TRANSACTION_CAPTURE')
Expand Down Expand Up @@ -253,6 +270,6 @@ def transaction_cancel(self):
'TRANSACTION_CANCEL', payload, res, res_time, self.sp_trans,
status_code=res.status_code
)
raise UnableToTakePayment('TRANSACTION_CAPTURE failed')
raise UnableToTakePayment('TRANSACTION_CANCEL failed')
else:
raise PaymentError('TRANSACTION_CAPTURE')
raise PaymentError('TRANSACTION_CANCEL')
18 changes: 18 additions & 0 deletions saferpay/migrations/0008_auto_20200627_2048.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.12 on 2020-06-27 18:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('saferpay', '0007_auto_20180911_2207'),
]

operations = [
migrations.AlterField(
model_name='saferpaytransaction',
name='order_id',
field=models.CharField(max_length=64),
),
]
2 changes: 1 addition & 1 deletion saferpay/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SaferpayTransaction(models.Model):
('CANCELD', 'CANCELD'),
)
token = models.CharField(max_length=32, primary_key=True)
order_id = models.IntegerField()
order_id = models.CharField(max_length=64)
date_created = models.DateTimeField(auto_now_add=True)
amount = models.DecimalField(max_digits=12, decimal_places=2, null=True, blank=True)
currency = models.CharField(max_length=8, null=True, blank=True)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
url='https://github.com/taywa/django-saferpay',
keywords=['saferpay', 'payment'],
install_requires=[
'Django>=1.11,<2.2',
'Django>=1.11,<=3',
],
include_package_data=True,
classifiers=[
Expand Down