Search code examples
phppaypal-sandboxpaypal-rest-sdkrecurring-billing

PayPal Billing REST Api Php issue


I have an issue with Paypal REST Api billing (recurring payments).

I did exactly as in paypal documentation but when I click payment button it opens blank page (no errors in the error log).

Here is what I do:

Front End Button

<form action="WEBSITE/subscribe/paypal/paypal_agreement.php" method="POST">
                                    <button type="submit" style="margin-top:10px;border: 0; background: transparent">
                                            <img src="WEBSITE/wp-content/uploads/2017/05/paypal.png" style = "width:170px;" alt="submit" />
                                    </button>                                   
                    </form>

paypal_agreement.php (copy/paste from paypal docs)

<?php
require_once("../../paypal/vendor/autoload.php");


$createdPlan = require 'paypal_createplan.php';

use PayPal\Api\Agreement;
use PayPal\Api\Payer;
use PayPal\Api\Plan;
use PayPal\Api\ShippingAddress;


$ppstartdate = date('c', time()+210);

$agreement = new Agreement();
$agreement->setName('Title Agreement')
    ->setDescription('Description Agreement')
    ->setStartDate($ppstartdate);

// Add Plan ID
// Please note that the plan Id should be only set in this case.
$plan = new Plan();
$plan->setId($createdPlan->getId());
$agreement->setPlan($plan);
// Add Payer
$payer = new Payer();
$payer->setPaymentMethod('paypal');

$agreement->setPayer($payer);



// ### Create Agreement
try {
    // Please note that as the agreement has not yet activated, we wont be receiving the ID just yet.
    $agreement = $agreement->create($apiContext);
    // ### Get redirect url
    // The API response provides the url that you must redirect
    // the buyer to. Retrieve the url from the $agreement->getApprovalLink()
    // method
    $approvalUrl = $agreement->getApprovalLink();

    header("Location: ".$approvalUrl);
} catch (Exception $ex) {

    exit(1);
}


return $agreement;

?>

paypal_createplan.php (copy/paste from paypal docs)

<?php
require_once("../../paypal/vendor/autoload.php");

use PayPal\Api\Currency;
use PayPal\Api\MerchantPreferences;
use PayPal\Api\PaymentDefinition;
use PayPal\Api\Plan;



// Create a new instance of Plan object
$plan = new Plan();


// # Basic Information
// Fill up the basic information that is required for the plan
$plan->setName('Title Plan')
    ->setDescription('Description Subscription Plan')
    ->setType('fixed');


// # Payment definitions for this billing plan.
$paymentDefinition = new PaymentDefinition();


// The possible values for such setters are mentioned in the setter method documentation.
// Just open the class file. e.g. lib/PayPal/Api/PaymentDefinition.php and look for setFrequency method.
// You should be able to see the acceptable values in the comments.
$paymentDefinition->setName('Regular Payments')
    ->setType('REGULAR')
    ->setFrequency('Month')
    ->setFrequencyInterval("1")
    ->setCycles("6")
    ->setAmount(new Currency(array('value' => 94.99, 'currency' => 'USD')));

$merchantPreferences = new MerchantPreferences();
$baseUrl = "https://websitelink.com";   

$merchantPreferences->setReturnUrl("$baseUrl/subscribe/paypal/execute.php?success=true")
->setCancelUrl("$baseUrl/subscribe/paypal/execute.php?success=false")
->setAutoBillAmount("yes")
->setInitialFailAmountAction("CONTINUE")
->setMaxFailAttempts("0")
->setSetupFee(new Currency(array('value' => 0, 'currency' => 'USD')));



// ### Create Plan
try {
    $output = $plan->create($apiContext);
} catch (Exception $ex) {

    exit(1);
}


return $output;

?>

execute.php (copy/paste from paypal docs)

<?php
// #Execute Agreement
// This is the second part of CreateAgreement Sample.
// Use this call to execute an agreement after the buyer approves it
require_once("../../paypal/vendor/autoload.php");

// ## Approval Status
// Determine if the user accepted or denied the request
if (isset($_GET['success']) && $_GET['success'] == 'true') {
$token = $_GET['token'];
$agreement = new \PayPal\Api\Agreement();
try {
    // ## Execute Agreement
    // Execute the agreement by passing in the token
    $agreement->execute($token, $apiContext);
} catch (Exception $ex) {

    exit(1);
}


// ## Get Agreement
// Make a get call to retrieve the executed agreement details
try {
    $agreement = \PayPal\Api\Agreement::get($agreement->getId(), $apiContext);

    //done
    header('Location: https://websitelink.com/subscribe/subscribe.php');


} catch (Exception $ex) {

    exit(1);
}

} else {
     $_SESSION['pmsg'] =  $_SESSION['pmsg'].'<h2>Subscription Failed</h2>';
}

But I couldn't find anything about authentication OAuth2 (as for example with express checkout), nothing in documentation. Maybe it doesn't work because of that?


Solution

  • Variable $apiContext should contain paypal app authentication & setConfig