Technical Website&E-commerce

Payment Gateway Integration

Integrating the payment gateway with Odoo is an easy task if we have a basic knowledge in python. In this section let us look how to integration on a payment gateway with the Odoo. First of all, I am using the Paytab as the payment gateway provider here Once we create an account in the Pay tab we will get a merchant email and secret key. Which is then used as the login credentials. After creating the account in Pay tab, we now let us focus on the integration. The main steps in the integration is, Validating the secret key Creating the pay page Verifying the payment Now let us look how to validate the secret key Secret Key validation

The secret is validating to check the existence of the account and to know the details of the account to which the amount has to be remitted. This method uses the merchant email and secret key to validate your credentials. You can find the secret key from your Pay tab account like this. Click on Merchant’s Dashboard >> Pay Tabs Services >> e-commerce Plugins and API. Here you can find the merchant email and the secret key. After getting the both, we have to send a request to validate secret key like this, URL: https://www.paytabs.com/apiv2/validate_secret_key Method: POST We have to pass the merchant email and the secret key along with this URL. For the validation of the secret key. Sample Code: secret_key_validation_url = 'https://www.paytabs.com/apiv2/validate_secret_key/{}'

merchant_email = 'niyasraphyk@gmail.com'

secret_key = 'BSWzZCWTrCgeKzCpS3rugWnhWNDgNFopykYxXVqpxGDiggI9kaDA61S2OviCbH1K1ZiZUJosp1LGhtPBDtcKu4MiA5UMMWxOVPNQ'

params = {'merchant_email': merchant_email, 'secret_key': secret_key}

r = requests.post(url=secret_key_validation_url, data=params) This is how we will send the validation request, then we will get the response from the gateway in the variable r. Let us look the different response that we are possible to get, Response Description 4000 Vaid Seceret Key 4001 missing secret_key or merchant_email parameter 4002 Invalid Secret Key If we get the response as 4000, we can process to next step, i.e., Pay Page creation. To get the response code from the variable r, we can use the following code r = requests.post(url=payment_url, data=params)

d = ast.literal_eval(r.text)

print d['response_code'] Pay page creation Pay page creation method will accept all the parameters required to create a Pay Page and then return the response as well as the link where all the parameters posted to the API will appear in the Pay Page customer can enter ONLY the credit card information such as account number and cvv to make the payment. For the Pay Page creation, we have to pass the following values. Value Description Type Example cc_first_name First Name of the Customer String niyas cc_last_name Last Name of the Customer String raphy cc_phone_number Country code for Phone Number of the Customer String +91 phone_number Phone Number of the Customer String 7736****** email Email of the customer String niyasraphyk@gmail.com products_per_title Product title of the product. If multiple products then add “||” separator String E.g.: IPhone || Samsung S5 || Samsung S4 unit_price Unit price of the product. If multiple products then add “||” separator String E.g.: 21.09 || 22.12 || 12.01 quantity Quantity of products. If multiple Products then add “||” separator. String E.g.: 1 || 2 || 3 other_charges Additional charges. e.g.: shipping charges, taxes, VAT, etc String E.g.: 123.399 amount Amount of the products and other charges, it should be equal to: amount = (sum of all products’ (unit_price * quantity)) + other_charges This field will be displayed in the invoice as the subtotal field String E.g.: 123.399 discount Discount of the transaction The Total amount of the invoice will be= amount - discount String E.g.: 123.399 currency Currency of the amount stated. 3 character ISO currency code String reference_no Invoice reference number String E.g.: Abc-5566 ip_customer The client IP with which the order is placed. String E.g.: 123.123.12.2 ip_merchant Server IP where the order is coming from String E.g.: 11.11.22.22 billing_address Complete Address of the customer. Multiple address lines will be Merged into one single line. String state Billing State (part of the address) entered by the customer String E.g.: Manama city Name of the billing city selected by customer String E.g.: Manama postal_code Billing Postal code provided by the customer String 12345 country Country of the customer String BHR shipping_first_name First Name of the Customer in shipping address String niyas shipping_last_name Last Name of the Customer in shipping address String raphy address_shipping Shipping address of the customer String Flat abc road 123 city_shipping Shipping City of the customer String Manama state_shipping Shipping State of the customer String Manama postal_code_shipping Shipping postal code of the customer String 403129 country_shipping Shipping country of the customer String BHR msg_lang Language of the Pay Page to be created. Invalid or blank entries will default to English String English / Arabic cms_with_version CMS / Language that you are using with its version. This will help us to troubleshoot issues, if any. String Magento 0.1.9 The above listed fields are required to create a Pay Page, so that we have .to pass all these datas along with the url to create the Pay Page. URL: https://www.paytabs.com/apiv2/create_pay_page Method: POST Sample code



pay_page_url = "https://www.paytabs.com/apiv2/create_pay_page"

params2 = {'merchant_email': merchant_email, 'secret_key': secret_key, 'pay_page_url': pay_page_url,

'site_url': site_url, 'return_url': return_url, 'title': title, 'currency': currency,

'amount': amount, 'quantity': quantity, 'unit_price': unit_price, 'discount': discount,

'other_charges': other_charges, 'cc_first_name': cc_first_name, 'cc_last_name': cc_last_name,

'cc_phone_number': cc_phone_number, 'phone_number': phone_number, 'email': email,

'products_per_title': products_per_title, 'msg_lang': msg_lang, 'country_shipping': country_shipping,

'postal_code_shipping': postal_code_shipping, 'state_shipping': state_shipping,

'city_shipping': city_shipping, 'address_shipping': address_shipping,

'billing_address': billing_address, 'shipping_last_name': shipping_last_name,

'shipping_first_name': shipping_first_name, 'country': country, 'postal_code': postal_code,

'reference_no': reference_no, 'state': state, 'city': city, 'ip_merchant': ip_merchant,

'ip_customer': ip_customer, 'cms_with_version': cms_with_version}

d = requests.post(url=pay_page_url, data=params2) After placing this request, we will get the response from the gateway. Response message

Code Description 4012 Pay Page created successfully 4404 You don't have permissions to create an Invoice 4001 Variable not found 4002 Invalid Credentials 4007 'currency' code used is invalid. Only 3 character ISO currency codes are valid. 4008 Your SITE URL is not matching with your profile URL 4013 Your 'amount' post variable should be between 0.27 and 5000.00 USD 4014 Products titles, Prices, quantity are not matching 4094 Your total amount is not matching with the sum of unit price amounts per quantity Once the pay page is created successfully (pay page is the page where which user can add his card details) After entering the card details, a user can click the pay now button. On creating the Pay Page we have to pass one more value, i.e., return url, the URL to which the page gets redirected after the payment. Payment Verification To verify the payment we will receive a p_id in the response message to the page. URL: https://www.paytabs.com/apiv2/verify_payment Method: POST To verify the payment we have to pass the following parameters along with url Element Description Format merchant_email Merchant email that you use to sign up and/or login into Pay tabs Merchant Dashboard niyasraphyk@gmail.com secret_key SECRET Key generated by merchant dashboard EG :- BSWzZCWTrCgeKzCpS3rugWnhWNDg NFopykYxXVqpxGDiggI9kaDA61S2OviCbH1K1ZiZUJ osp1LGhtPBDtcKu4MiA5UMMWxOVPNQ payment_reference This is the p_id that is returned in the response of create Pay Page when the page is created successfully. E.g.: t2938yh202tu0 Response codes that you will get after this, Error Code Description 4001 Missing parameters 4002 Invalid Credentials 0404 You don't have permissions 0400 There are no transactions available 100 Payment is completed 481 482 This transaction may be suspicious. If this transaction is genuine, please contact Pay tabs customer service to inquire about the feasibility of processing this transaction. Any response code other than 100, 481,482 Any other response than 100, 482, 481 means that the transaction has been rejected and the rejection reason is the description of the corresponding response code Sample code



verify_payment = 'https://www.paytabs.com/apiv2/verify_payment/{}'

merchant_email = "malappuram132@gmail.com"

secret_key = "BSWzZCWTrCgeKzCpS3rugWnhWNDgNFopykYxXVqpxGDiggI9kaDA61S2OviCbH1K1ZiZUJosp1LGhtPBDtcKu4MiA5UMMWxOVPNQ"

p_id = "85533"

params = {'merchant_email': merchant_email, 'secret_key': secret_key, 'payment_reference': p_id}

g = requests.post(url=verify_payment, data=params) This is how a payment integration with the pay tab is done