python-targetpay

Python payment module for targetpay.com


Keywords
payment, targetpay, com, ideal, mr, cash, sofort
License
Other
Install
pip install python-targetpay==1.3

Documentation

Targetpay python module

Python module for payments via targetpay.com We assume that you already have an account with TargetPay, if not you can register for an account at http://www.targetpay.com.

Supported payment methods

At this moment the package only supports payments via iDeal, Mr. Cash and Sofort banking. Other payment methods will be implemented soon.

Installation

We recommend installing the package with pip.

pip install python-targetpay

Usage

1. Setup the targetpay client

from targetpay.core import TargetPay
client = TargetPay(rtlo = 123456)

Rtlo is your TargetPay layout number. If you want to activate the test mode, then add the test argument with the value True:

client = TargetPay(rtlo = 123456, test = True) # Activate the test mode

To use the client for callback or verification purposes the client can be initialized together with the transaction_id and the payment method.

client = TargetPay(rtlo = 123456, transaction_id = 123456, method = 'IDL')

The method argument have the following values: IDL for iDeal, MRC for Mr. Cash and SOF for Sofort banking.

2. Initialize a payment

2.1 Get the list of banks (iDeal only)

banks = client.ideal_banks()

This will return the list of banks as a tuple.

If you'd need the list of banks as a dict then use the as_dict argument:

banks = client.ideal_banks(as_dict = True)

This method can be used to create a selection of banks, so that the customer can pick his bank for paying by iDeal.

2.2 Create the payment

iDeal:

This example initiates an iDeal payment for € 2.50:

payment = client.ideal_payment(description = 'Test payment', 
                               amount = 2.50, 
                               return_url='http://www.example.com/return_url', 
                               report_url = 'http://www.example.com/process_payment')
Mr. Cash:

This example initiates an Mr. Cash payment for € 2.50 and uses the Dutch language in the payment screen:

payment = client.mrcash_payment(description = 'Test payment', 
                                amount = 2.50, 
                                ip = '1.2.3.4', 
                                return_url='http://www.example.com/return_url', 
                                report_url = 'http://www.example.com/process_payment', 
                                language='NL')
Sofort banking:

This example initiates a Sofort payment for € 2.50 for a digital service by a customer in Germany:

from targetpay.utils import SOFORT_TYPES, SOFORT_COUNTRIES

payment = client.sofort_payment(country = SOFORT_COUNTRIES['DE'], 
                                payment_type = SOFORT_TYPES['DIGITAL'], 
                                description = 'Test payment', 
                                amount = 2.50, 
                                ip = '1.2.3.4', 
                                return_url='http://www.example.com/return_url', 
                                report_url = 'http://www.example.com/process_payment', 
                                language='EN')

2.3 Checking if initiating the payment went well and redirecting the customer to the payment url.

if payment.success:
    payment_url = payment.payment_url
    # Redirect the customer to the payment_url
else:
    print 'An error occured. Code: %s - %s' % (payment.code, payment.explanation)

If initiating the payment was successful then success will be set to True and payment_url will contain the url to process the payment. If not, then code will contain the error code and explanation will contain extra information.

Arguments for payment initiation functions
Argument Description iDeal Mr. Cash Sofort Mandatory
amount The amount in euro. YES YES YES YES
description A description of the transaction. YES YES YES YES
return_url The url to redirect the customer after payment. YES YES YES YES
cancel_url The url to redirect the customer after cancellation of the payment. YES NO NO NO
report_url The url which targetpay will use to report the status after payment. YES YES YES NO
bank A code which represents the bank which will process the payment. If this option isn't set then TargetPay will offer to select at their payment screen. YES NO NO NO
language The language code for the payment screen. Valid values for Mr. Cash are: EN for English, NL for Dutch and FR for French. Valid values for Sofort are: EN for English, NL for Dutch and DE for German NO YES YES NO
ip The customer's ip address. NO YES YES YES
country The customer's country. See the note on Sofort banking. NO NO YES YES
payment_type The payment type. See the note on Sofort banking. NO NO YES YES
Sofort banking note

The country argument should contain a code, representing the customers's country. Best practise is to use the SOFORT_COUNTRIES dict. Valid items are DE, AT, CH, BE and IT, representing Germany, Austria, Switzerland, Belgium and Italy.

The same principle applies the payment_type argument and the SOFORT_TYPES dict. Valid items are: PHYSICAL for sales of physical goods, DIGITAL for sales of non-adults digital services/goods and DIGITAL_ADULT for sales of ADULT digital services/goods.

3. Processing callbacks

The return_url and report_url can be used to verify if the customer fulfilled payment. TargetPay will send various parameters to this urls, which can be passed on to the callback method. The callback method will then trigger a verification of the payment.

Note: Although not mandatory, especially with iDeal it's good practise to use report_url, because TargetPay will send more information about the payment and this call isn't visible to the customer. You will then have to pass these values to the callback method. The values you'll have to pass from the report_url with iDeal are cname, cbank and idealtrxid.

Example:

payment.callback(cname = cname, cbank = cbank, idealtrxid = idealtrxid)

If you don't use report_url it's still possible to trigger verification of the payment by calling the callback method without any arguments. However, we don't recommend this, because the customer's name (cname) and customer's bank (cbank) values won't be set in this case.

For this function to work the object needs to have at least the transaction_id and method fields set.

4. Verifying the customer's payment

payment.verify_payment()
 if payment.paid:
    # Payment has been successful 
 else:
    # Payment failed

The verify_payment method uses TargetPay's once option by default. This option will confirm payment only once. Any next calls will cause the response 'TP00014 - Already exchanged'. This behavior can be convenient in some cases, for example to prevent double deliveries. If you want to disable this behavior then use verify_payment as follows:

payment.verify_payment(once = False)

The callback method invokes verify_payment with once set as False.

For this function to work the object needs to have at least the transaction_id and method fields set.

Implementation into Django

To be written

Implementation into Flask

To be written

Support

Please use the GitHub issue system for support.

If you need commercial support for implementation of this software into your system, please contact the package sponser: Balkan Technologies at info@balkan.tech.

Contributions

We encourage people to fork the code to contribute to the package.

Disclaimer

This program has been created by initiative of Balkan Technologies EOOD & Co. KD and has been released as open source under the GPL 3 license. Balkan Technologies EOOD & Co. KD is in no way related or affiliated to TargetPay.com. Neither did TargetPay.com give an order to create this software.

Copyright

Copyright (C) 2016, Balkan Technologies EOOD & Co. KD

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or(at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

For more information: info@balkan.tech