I will explain how PayPal subscriptions works. We should create a sandbox account on PayPal first on developer.paypal.com;
By default Default application is created; In this application, we have 2 accounts: business and personal. We should get CLIENT ID and CLIENT SECRET, and save them in our application. They are necessary to get a bearer token. In order to do a payment, we need to create 2 endpoints:
- to create an order
- to capture an order
https://developer.paypal.com/api/rest/ is the documentation
Authentication
As written in the documentation, we have 2 options to authenticate our request to sandbox server:
- get a bearer token; the request should be sent to like this:
_
_ curl -v -X POST “https://api-m.sandbox.paypal.com/v1/oauth2/token”\
-u “CLIENT_ID:CLIENT_SECRET”\
-H “Content-Type: application/x-www-form-urlencoded”\
-d “grant_type=client_credentials”_
_
- encryption of CLIENT_ID:CLIENT_SECRET in Base64
We will get access_token in both ways. To integrate I will use active interaction gem.
Order
We should create an order;
import requests
headers = {
‘Content-Type’: ‘application/json’,
‘PayPal-Request-Id’: ‘7b92603e-77ed-4896-8e78-5dea2050476a’,
‘Authorization’: ‘Bearer 6V7rbVwmlM1gFZKW_8QtzWXqpcwQ6T5vhEGYNJDAAdn3paCgRpdeMdVYmWzgbKSsECednupJ3Zx5Xd-g’,
}
data = ‘{ “intent”: “CAPTURE”, “purchase_units”: [ { “reference_id”: “d9f80740-38f0-11e8-b467-0ed5f89f718b”, “amount”: { “currency_code”: “USD”, “value”: “100.00” } } ], “payment_source”: { “paypal”: { “experience_context”: { “payment_method_preference”: “IMMEDIATE_PAYMENT_REQUIRED”, “brand_name”: “EXAMPLE INC”, “locale”: “en-US”, “landing_page”: “LOGIN”, “shipping_preference”: “SET_PROVIDED_ADDRESS”, “user_action”: “PAY_NOW”, “return_url”: “your_return_url”, “cancel_url”: “your_cancel_url” } } } }’
response = requests.post(‘https://api-m.sandbox.paypal.com/v2/checkout/orders‘, headers=headers, data=data)
There are two options for intent:
- AUTHORIZE
- CAPTURE
When you set the intent to “AUTHORIZE,” PayPal authorizes the payment amount but doesn’t capture the funds immediately. Instead, it places a hold on the funds, reserving them for capture at a later time. This is useful in scenarios where you need to verify the availability of funds or perform additional checks before completing the transaction.
Use Case: For example, if you operate an e-commerce platform, you might use the “AUTHORIZE” intent when a customer places an order. This allows you to verify the payment details and inventory availability before confirming the purchase and capturing the funds.
CAPTURE: With the “CAPTURE” intent, PayPal immediately captures the funds from the customer’s account when the payment is made. This completes the transaction in real-time, and the funds are transferred to your account.
Use Case: In scenarios where you provide digital goods or instant services, using the “CAPTURE” intent ensures that payments are processed and funds are transferred immediately upon completion of the transaction.
If we create an order with “AUTHORIZE” intent, then we have to authorize (Authorize Order method, Orders API) the order then capture the order (Capture Authorized Payment, Payments API); If we create an order with “CAPTURE” intent, then we directly use Capture Payment for Order method (Orders API);