How to implement paypal payment gateway

Sat 13 June 2015 by Godson

The 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:

  1. allow_note : It enables the buyer to enter a note to the merchant on the paypal page during checkout.

  2. no_shipping : Determines whether or not PayPal displays shipping address fields on the experience pages.

    • 0 - PayPal displays the shipping address on the PayPal pages.
    • 1 - PayPal does not display shipping address fields whatsoever.
    • 2 - If you do not pass the shipping address, PayPal obtains it from the buyer’s account profile.
  3. address_override : Determines if the PayPal pages should display the shipping address supplied in this call, rather than the shipping address on file with PayPal for this buyer.

    • 0 - PayPal pages should display the address on file
    • 1 - PayPal pages should display the addresses supplied in this call instead of the address from the buyer’s PayPal account.
flow_config:

Parameters for flow configuration

  1. landing_page_type : Type of PayPal page to be displayed when a user lands on the PayPal site for checkout.

    • Billing - The Non-PayPal account landing page is used
    • Login - The paypal account login page is used.

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:

  • "sale" - For immediate payment
  • "authorize" - To authorize a payment for capture later
  • "order" - To create an order
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.

  • payment_method : Payment method used. Must be either credit_card or paypal.
  • funding_instruments : A list of funding instruments for the current payment
  • payer_info : Information related to the payer
  • status : Status of the payer's PayPal account. VERIFIED or UNVERIFIED
transactions:

Transactional details including the amount and item details.

redirect_urls:

Set of redirect URLs you provide only for PayPal-based payments.

  • return_url : The payer is redirected to this URL after approving the payment.
  • cancel_url : The payer is redirected to this URL after canceling the payment.

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.