PayPal Subscriptions(recurring payments) Payment Gateway Integration in PHP Source Code Download

Today, We want to share with you paypal recurring payments.In this post we will show you paypal smart button subscription, hear for paypal rest-api-sdk-php laravel we will give you demo and example for implement.In this post, we will learn about Paypal Recurring Payments PHP REST API Example with an example.

how to manage recurring payments using paypal subscriptions in php?

  • Get an Access Token – cURL
  • Create Product
  • Create Billing Plans
Escrow with Paypal Payments Pro API Integration Using PHP
Escrow with Paypal Payments Pro API Integration Using PHP

Get an Access Token – cURL

<?php

if($_GET['is_demo'] ==1){
    $CLIENT_ID = "Axxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxX";
    $SECRET_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxx-U";
    $sandbox="sandbox.";
    $plan_id = 'P-XXXXXXXXXXXXXXXXXXXXXXXX';
}else{
    $CLIENT_ID = "***********************************-***********_i6GEYJ3-****************_******_";
    $SECRET_KEY="***_***************-*****************_****************_************************";
    $sandbox="";
    $plan_id = 'P-XXXXXXXXXXXXXXXXXXXXXXXX';
}
$name = "Pakainfo Test Production Recurring";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.'.$sandbox.'paypal.com/v1/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD,  $CLIENT_ID.':'.$SECRET_KEY );
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Accept-Language: en_US';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$accessToken = json_decode($result); 

//print_r($accessToken->access_token); exit;

Create New Product

if(isset($accessToken->access_token)){
    $payload = json_encode( array( "name"=> $name,"type"=>"SERVICE" ) );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.'.$sandbox.'paypal.com/v1/catalogs/products');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_POST, 1);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: '.$accessToken->token_type.' '.$accessToken->access_token.'';
   // $headers[] = 'Paypal-Request-Id: <your client id>';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
    $createProduct = json_decode($result);
}else{
    echo" access_token empty";
}

Create Billing Plans

//print_r($createProduct->id); exit;
if(isset($createProduct->id)){
    $plan_data = json_encode( array( "product_id"=> $createProduct->id,"name"=>$createProduct->name,"description"=> "Basic plan testing demo","billing_cycles"=> array(
        array("frequency"=> array("interval_unit"=>"MONTH","interval_count"=> 1),"tenure_type"=> "TRIAL", "sequence"=> 1, "total_cycles"=> 1),
       array("frequency"=> array("interval_unit"=>"MONTH","interval_count"=> 1),"tenure_type"=> "REGULAR", "sequence"=> 2, "total_cycles"=> 12,"pricing_scheme"=>array("fixed_price"=>array("value"=> "2", "currency_code"=>"USD") ) )),
       "payment_preferences"=> array("auto_bill_outstanding"=> true,"setup_fee"=> array("value"=> "1","currency_code"=> "USD"),"setup_fee_failure_action"=> "CONTINUE", "payment_failure_threshold"=> 3),
       "taxes"=> array("percentage"=> "10","inclusive"=> false) ) );
       
   // print_r($plan_data); exit;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api-m.'.$sandbox.'paypal.com/v1/billing/plans');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $plan_data);
    curl_setopt($ch, CURLOPT_POST, 1);
    $headers = array();
    $headers[] = 'Accept: application/json';
    $headers[] = 'Authorization: '.$accessToken->token_type.' '.$accessToken->access_token.'';
    //$headers[] = 'PayPal-Request-Id:'.$createProduct->id;
    $headers[] = 'Prefer: return=representation';
    $headers[] = 'Content-Type: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
    $createPlan = json_decode($result);

    //print_r($createPlan);
}else{
    echo "Your Plan id empty";
}

//print_r($createPlan->id); exit;
//$plan_id = $createPlan->id;

Full Example:

<?php

if($_GET['is_demo'] ==1){
    $CLIENT_ID = "Axxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxX";
    $SECRET_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxx-U";
    $sandbox="sandbox.";
    $plan_id = 'P-XXXXXXXXXXXXXXXXXXXXXXXX';
}else{
    $CLIENT_ID = "***********************************-***********_i6GEYJ3-****************_******_";
    $SECRET_KEY="***_***************-*****************_****************_************************";
    $sandbox="";
    $plan_id = 'P-XXXXXXXXXXXXXXXXXXXXXXXX';
}
$name = "Pakainfo Test live Recurring";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.'.$sandbox.'paypal.com/v1/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD,  $CLIENT_ID.':'.$SECRET_KEY );
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Accept-Language: en_US';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$accessToken = json_decode($result); 

//print_r($accessToken->access_token); exit;

if(isset($accessToken->access_token)){
    $payload = json_encode( array( "name"=> $name,"type"=>"SERVICE" ) );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.'.$sandbox.'paypal.com/v1/catalogs/products');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_POST, 1);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: '.$accessToken->token_type.' '.$accessToken->access_token.'';
   // $headers[] = 'Paypal-Request-Id: <your client id>';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
    $createProduct = json_decode($result);
}else{
    echo" access_token empty";
}
//print_r($createProduct->id); exit;
if(isset($createProduct->id)){
    $plan_data = json_encode( array( "product_id"=> $createProduct->id,"name"=>$createProduct->name,"description"=> "Basic plan testing demo","billing_cycles"=> array(
        array("frequency"=> array("interval_unit"=>"MONTH","interval_count"=> 1),"tenure_type"=> "TRIAL", "sequence"=> 1, "total_cycles"=> 1),
       array("frequency"=> array("interval_unit"=>"MONTH","interval_count"=> 1),"tenure_type"=> "REGULAR", "sequence"=> 2, "total_cycles"=> 12,"pricing_scheme"=>array("fixed_price"=>array("value"=> "2", "currency_code"=>"USD") ) )),
       "payment_preferences"=> array("auto_bill_outstanding"=> true,"setup_fee"=> array("value"=> "1","currency_code"=> "USD"),"setup_fee_failure_action"=> "CONTINUE", "payment_failure_threshold"=> 3),
       "taxes"=> array("percentage"=> "10","inclusive"=> false) ) );
       
   // print_r($plan_data); exit;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api-m.'.$sandbox.'paypal.com/v1/billing/plans');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $plan_data);
    curl_setopt($ch, CURLOPT_POST, 1);
    $headers = array();
    $headers[] = 'Accept: application/json';
    $headers[] = 'Authorization: '.$accessToken->token_type.' '.$accessToken->access_token.'';
    //$headers[] = 'PayPal-Request-Id:'.$createProduct->id;
    $headers[] = 'Prefer: return=representation';
    $headers[] = 'Content-Type: application/json';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);
    $createPlan = json_decode($result);

    //print_r($createPlan);
}else{
    echo "Your Plan id empty";
}

//print_r($createPlan->id); exit;
//$plan_id = $createPlan->id;


?>

I hope you get an idea about paypal recurring payments.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.