Do you want to integrate PayPal pre-approved payment on your website? That's good, in this article, we will see together how to do it. As you know, if you are a business and want to sell subscriptions on your website, pre-approved payment is the option you need. Indeed, it allows you to request payment from your customers without being constantly behind them for complaints. In this article, we will briefly see what pre-approved payment is. Then, we will explain how it works. Finally, we will see how you can integrate it on your website. If you want ready-to-use code, you can click on this link and contact us .
It is a mechanism used by platforms like PayPal that allows a merchant or service provider to automatically charge a customer's account. You can do this without asking for their explicit approval for each transaction after the initial authorization. This type of payment is commonly used for recurring payments. Businesses use it for subscriptions or for multiple purchases where the user has given a one-time authorization for future transactions.
The way pre-approved payments work is as follows:
The customer gives consent for their PayPal account to be automatically charged in the future. This is usually done by agreeing to a Billing Agreement with the merchant. In this step, PayPal asks the customer to confirm that they authorize future payments.
After this initial authorization, the merchant can trigger automatic payments without the customer needing to approve each time. This is useful for services like
The customer retains control over these payments by being able to cancel the billing agreement at any time through their PayPal account.
If you consume online services, you have certainly had the opportunity to use recurring payments.
For services that require monthly, weekly or annual payments (like Netflix, Spotify).
For utilities or other regular bills where the amount may vary (telephone, electricity).
For services that allow the customer to make multiple purchases without having to approve each payment (e.g., ride-sharing or home rental platforms). In short, a pre-approved payment allows a customer to give consent once for a series of future transactions, facilitating automated payments without requiring manual intervention each time.
To integrate a PayPal pre-approved payment on your website, you need to follow the steps below:
For PHP , you can use Composer to install the PayPal SDK:
composer require paypal/rest-api-sdk-php
For JavaScript , simply include the PayPal script on your page:
<script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID"><script>
Use the PayPal API to create a pre-approved payment. Here are the main steps to follow:
Example API call to create a Billing Agreement:
$agreement = new Agreement(); $agreement->setName('Payment Agreement') ->setDescription('Agreement for future payments') ->setStartDate('2023-12-20T10:00:00Z'); $plan = new Plan(); $plan->setId('P-xxxxxxxxxxxx'); $agreement->setPlan($plan); $payer = new Payer(); $payer->setPaymentMethod('paypal'); $agreement->setPayer($payer); try { $agreement->create($apiContext); $approvalUrl = $agreement->getApprovalLink(); echo "Redirect user to: " . $approvalUrl; } catch (Exception $ex) { // Handle exception }
Once the customer is redirected to the PayPal confirmation URL, you can use the returned token to capture payments in the future. Example to capture a pre-approved payment:
$agreement = new Agreement(); $agreement->execute($token, $apiContext);
Before going to production, use the Sandbox environment to test all transactions. Ensure that payments are captured correctly and that you can run recurring or pre-approved transactions.
Once testing is complete, replace the Sandbox credentials with the Live credentials in your code. This integration allows you to charge the customer's PayPal account automatically without requiring new authorizations after the initial approval.