How to implement paypal payment gateway
Sat 13 June 2015 by GodsonThe PayPal REST APIs are supported in two environments. Use the Sandbox environment for testing purposes, then move to the live environment for production processing.
The following endpoints address are two environments:
Sandbox (for testing) : https://api.sandbox.paypal.com
Live (production) : https://api.paypal.com
A complete REST operation is formed by combining an HTTP method with the full URI to the resource you’re addressing. For example, here is the operation to create a new payment:
POST https://api.paypal.com/v1/payments/payment
The PayPal REST sdk can be obtained through pip
pip install paypalrestsdk
OAuth Request / Response
import paypalrestsdk
api = paypalrestsdk.set_config(
mode="sandbox", # sandbox or live
client_id="CLIENT_ID",
client_secret="CLIENT_SECRET")
api.get_access_token()
Client Id and Secret Id can be obtained from the application created in the paypal account.
For the following each API call, you'll need to set request headers, including the access token.
Creating a WebProfile:
web_profile = WebProfile({
"name": Web_Profile_Name,
"presentation": {
"brand_name": "BusinessName",
"logo_image": URL to logo image,
"locale_code": "US"
},
"input_fields": {
"allow_note": 1,
"no_shipping": 1,
"address_override": 1
},
"flow_config": {
"landing_page_type": "Login"
}
})
web_profile.create(): # Will return True or False
name: | The name of the web experience profile which should be unique among the profiles for a given merchant. |
---|---|
presentation: | It contains the parameters for style and presentation. |
input_fields: | Parameters for input fields customization:
|
flow_config: | Parameters for flow configuration
|
Creating a Payment:
payment = Payment({
"intent": "sale",
"experience_profile_id": web_profile.id,
"payer": {
"payment_method": "paypal",
"status": "VERIFIED" },
"redirect_urls": {
"return_url": RETURN_URL,
"cancel_url": CANCEL_URL },
"transactions": [ {
"amount": {
"total": amount,
"currency": "USD" },
"description": 'description' } ] } )
payment.create(): # Returns True or False
intent: | Payment intent. Allowed values are:
|
---|---|
experience_profile_id: | |
Id that will be obtained from the response of web profile request |
|
payer: | Source of the funds for this payment represented by a PayPal account or a credit card.
|
transactions: | Transactional details including the amount and item details. |
redirect_urls: | Set of redirect URLs you provide only for PayPal-based payments.
|
Execute an approved PayPal payment:
Use this call to complete a PayPal payment that has been approved by the payer.
payment = Payment.find("PAY-23456676765ASCASFE45")
payment.execute({ "payer_id": "7D4FDSFWEF5" })
The payment_id and payer_id are passed in the return_url. Once the payment is executed,it returns an array of payment object.
In the response state of the payment is obtained as : created approved failed, canceled, expired or pending.
The transaction details in the response contains state of the sale which is obtained as: pending, completed, refunded or partially_refunded
If the payment state is approved and the sale state is completed, the payment is successfully executed.