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

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: ';
    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:

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: ';
    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.

Leave a Comment