Package: django-saferpay
django-saferpaySaferpay integration for djangoThis packages uses the saferpay json api and the saferpay payment page interface.
This is NOT an offical package from SIX Payment Services.See the officialsaferpay json apifor more information.
Take also a look attest.saferpay.com.django setupThis django package in used in cunjunction with django-oscar. It can work with other django shop systems,
nothing is tied to django-oscar.It's used with dango 1.11 but should/could work with django 2.0, 2.1.first updatesettings.py:fromdjango.urlsimportreverse_lazyfromdjango.utils.translationimportugettext_lazyas_INSTALLED_APPS=[...'saferpay',]...SAFERPAY_APIURL='https://test.saferpay.com/api'SAFERPAY_USERNAME='API_XXX'SAFERPAY_PASSWORD='XXX'SAFERPAY_CUSTOMERID='xxxxxx'# a numberSAFERPAY_TERMINALID='xxxxxx'# a numberSAFERPAY_SUCCESS_CAPTURE_URL=reverse_lazy('yourshop:payment-capture')# your callback after getting the moneySAFERPAY_SUCCESS_URL=reverse_lazy('yourshop:thank-you')# your callback after a successfull orderSAFERPAY_FAIL_URL=reverse_lazy('yourshop:payment-failed')# your callback after a failed paymentSAFERPAY_ORDER_TEXT_NR=_('Payment for the company XXX, Order nr.%s.')SAFERPAY_FORCE_LIABILITYSHIFT_ACTIVE=False# default: FalseSAFERPAY_DO_NOTIFY=True# default: False...You need to provide three URLs from your shop app for providing callbacks for the saferpay gateway.SAFERPAY_SUCCESS_CAPTURE_URL- saferpay callback for a successfull capture of the amount
after PAYMENTPAGE_INIT and PAYMENTPAGE_ASSERTSAFERPAY_SUCCESS_URL- everything went fine, saferpay callback to shop success urlSAFERPAY_FAIL_URL- something went wrong, saferpay callback to shop failure urlin your projecturls.py:...url(r'',include('saferpay.urls')),...create the db models./manage.pymigratedjango oscar setupIf you use django oscar got todjango-oscar-saferpayto procced with the setup process and see an sample usage implementation.sample usageYou can use this code as example. You need to integrate it into
your own views in the shop system.Intizialize the servicefromsaferpay.gatewayimportSaferpayServicefromsaferpayimportexecptionsassp_execptionsdefbilling_address_for_saferpay(billing_address):"""Transform you shop billing address toa format that saferpay uses.Your billing_address object can be totaly differnt,adjust it to your needs."""data={}data['FirstName']=billing_address.first_namedata['LastName']=billing_address.last_namedata['Street']=billing_address.line1ifbilling_address.line2:data['Street2']=billing_address.line2data['Zip']=billing_address.postcodedata['City']=billing_address.cityreturndatadefpayment_init(request,order):"""Intizialize a new SaferpayService instance, with yourorder data. Adjust the the order calls for your shop.The init payload is created and after success you getredirected to the saferpay PayementPage."""saferpay_service=SaferpayService(order_id=order.number,amount=order.total.incl_tax,currency=order.total.currency,language_code=language_code)payload=saferpay_service.payload_init(billing_address=billing_address_data)try:token=saferpay_service.paymentpage_init(payload)request.session['saferpay_token']=tokenraiseRedirectRequired(saferpay_service.paymentpage_redirect().url)exceptsp_execptions.GatewayErrorase:# do your rollback in case of payment failurereturnredirect(reverse_lazy('yourshop:payment-failed'))Assert and Capture the money and place the orderfromsaferpay.gatewayimportSaferpayServicefromsaferpayimportexecptionsassp_execptionsdefpayment_capture(request):token=request.session['saferpay_token']saferpay_service=SaferpayService.init_from_transaction(token=token)try:status=saferpay_service.paymentpage_assert()except(sp_execptions.GatewayError,sp_execptions.TransactionDeclined,sp_execptions.UnableToTakePayment,sp_execptions.PaymentError)ase:# do your rollback in case of payment failurereturnredirect(reverse_lazy('yourshop:payment-failed'))ifstatus=='CAPTURED':# register the payment in you shoplogger.info("Order #%s: payment successful, placing order",saferpay_service.order_id)try:# place the orderexceptUnableToPlaceOrderase:# go back to preview