Package: python-payway
PayWay REST API - PythonStore customers, their card and bank account in PayWayTake payment using a stored credit card or bank accountProcess and Capture a pre-authorisationLookup or poll transactionsRefund transactionsVoid transactionsUpdate a customer's payment setup in PayWayInstallpip install python-paywayTake payment using a stored credit cardCreate a Client class with your PayWay API credentialsfrom payway.model import *
from payway.client import *

client = Client(merchant_id='',
                bank_account_id='',
                publishable_api_key='',
                secret_api_key='')Create a PayWayCustomer class with your customer's detailscustomer = PayWayCustomer(custom_id='c981a',
                          customer_name='John Smith',
                          email_address='johnsmith@example.com',
                          send_email_receipts=False,  # not available in sandbox
                          phone_number='0343232323',
                          street='1 Test Street',
                          street2='2 Test Street',
                          city_name='Sydney',
                          state='NSW',
                          postal_code='2000')Create a PayWayCard class with your customer's card detailscard = PayWayCard(card_number='',
                  cvn='',
                  card_holder_name='',
                  expiry_date_month='',
                  expiry_date_year='')Create a token from your card and create a customer in PayWaytoken_response, errors = client.create_card_token(card)
token = token_response.token        
customer.token = token
payway_customer, customer_errors = client.create_customer(customer)Note the 'payway_customer' object contains the full customer response fields from PayWay.Create a Payment class with the payment details and process the transactioncustomer_number = payway_customer.customer_number
payment = PayWayPayment(customer_number=customer_number,
                        transaction_type='payment',
                        amount='',
                        currency='aud',
                        order_number='',
                        ip_address='')
transaction, errors = client.process_payment(payment)Check thetransactionfor the resultif not errors and transaction.status == 'approved':
    # process successful responseTake payment using a credit card token onlyclient = Client(merchant_id='',
                bank_account_id='',
                publishable_api_key='',
                secret_api_key='')
card = PayWayCard(card_number='',
                  cvn='',
                  card_holder_name='',
                  expiry_date_month='',
                  expiry_date_year='')
token_response, errors = client.create_card_token(card)
# your customer reference number or a stored PayWay customer number
customer_number = ''    
payment = PayWayPayment(customer_number=customer_number,
                        transaction_type='payment',
                        amount='',
                        currency='aud',
                        order_number='',
                        ip_address='',
                        token=token_response.token,
                        merchant_id=client.merchant_id)
transaction, errors = client.process_payment(payment)Handling errorsDocumented errors (such as 422 Unprocessable entity) are parsed into an PaymentError class that you can use in an customer error message.https://www.payway.com.au/docs/rest.html#http-response-codesif errors:
    for error in errors: 
        print(error.field_name)
        print(error.message) 
        print(error.field_name)
    # or use a method
    PaymentError().list_to_message(errors)Direct DebitDirect debit transactions are possible by creating a token from a bank account:bank_account = BankAccount(account_name='Test', bsb='000-000', account_number=123456)
token_response, errors = client.create_bank_account_token(bank_account)
token = token_response.tokenStore the token with a customer in PayWay using the same process as the card outlined above.Note: direct debit transactions take days to process so they must be polled regularly for the latest transaction status from the customer's bank.Lookup transactionPoll a transaction using theget_transactionmethod.transaction, errors = client.get_transaction(transaction.transaction_id)Process and capture a pre-authorisationTo process a credit card pre-authorisation using a credit card stored against a customer usepreAuthas thetransaction_typealong with the customer's PayWay number, amount and currency.pre_auth_payment = PayWayPayment(customer_number='',
                                 transaction_type='preAuth',
                                 amount='',
                                 currency='aud',
                                 order_number='',
                                 ip_address='')
transaction, errors = client.process_payment(pre_auth_payment)To capture the pre-authorisation supply a pre-authorisation transaction ID,captureas thetransaction_typealong with an amount to capture.capture_payment = PayWayPayment(transaction_type='capture',
                                parent_transaction_id='',
                                amount='',
                                order_number='',
                                ip_address='')
transaction, errors = client.process_payment(capture_payment)RefundsRefund a transaction by supplying a PayWay transaction ID and the refund amount.refund, errors = client.refund_transaction(
    transaction_id=transaction.transaction_id,
    amount=transaction.principal_amount,
)Voiding a transactionVoid a transaction by supplying a PayWay transaction ID.void_transaction, errors = client.void_transaction(transaction.transaction_id)Update Payment SetupUpdate a customer's payment setup with a new credit card or bank account in PayWay. Supply the new token and an existing PayWay customer number.payment_setup, errors = client.update_payment_setup(new_token, payway_customer.customer_number)Additional notesPayWay API documentationhttps://www.payway.com.au/docs/rest.htmlIt is recommended to use PayWay's Trusted Framehttps://www.payway.com.au/docs/rest.html#trusted-framewhen creating a single use token of a card or bank account so your PCI-compliance scope is reduced.FraudPlease follow PayWay's advice about reducing your risk of fraudulent transactions.https://www.payway.com.au/docs/card-testing.html#card-testingTestingSign up for a PayWay sandbox account:https://www.payway.com.au/sandboxTest the integration by adding your PayWay REST API keys to your local environment. These keys are in Account Settings >REST API Keys. Copy yourPublishableandSecretAPI keys.export PAYWAY_PUBLISHABLE_API_KEY="your PayWay Publishable API Key"export PAYWAY_SECRET_API_KEY="your PayWay Secret API Key"Run the tests to ensure the integration is working.python -m unittest discover tests