user225269
user225269

Reputation: 10913

how to setup recurring payments in paypal

I'm trying to setup recurring payments in paypal with PHP. But the problem that I'm having is that I don't know if I'm doing the right thing. I have this class which makes the request to the Paypal API:

<?php
class Paypal {

   protected $_errors = array();


   protected $_credentials = array(
      'USER' => 'my-user-id',
      'PWD' => 'my-pass',
      'SIGNATURE' => 'my-signature',
   );


   protected $_endPoint = 'https://api-3t.sandbox.paypal.com/nvp';


   protected $_version = '74.0';

   public function request($method,$params = array()) {
      $this -> _errors = array();
      if( empty($method) ) { 
         $this -> _errors = array('API method is missing');
         return false;
      }

      $requestParams = array(
         'METHOD' => $method,
         'VERSION' => $this -> _version
      ) + $this -> _credentials;


      $request = http_build_query($requestParams + $params);

      $http_header = array(
        'X-PAYPAL-SECURITY-USERID' => 'my-user-id',
        'X-PAYPAL-SECURITY-PASSWORD' => 'my-pass',
        'X-PAYPAL-SECURITY-SIGNATURE' => 'my-signature',
        'X-PAYPAL-REQUEST-DATA-FORMAT' => 'JSON',
        'X-PAYPAL-RESPONSE-DATA-FORMAT' => 'JSON'
      );


      $curlOptions = array (
        CURLOPT_HTTPHEADER => $http_header,
        CURLOPT_URL => $this -> _endPoint,
        CURLOPT_VERBOSE => 1,
        CURLOPT_SSL_VERIFYPEER => true,
        CURLOPT_SSL_VERIFYHOST => 2,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => $request
      );

      $ch = curl_init();
      curl_setopt_array($ch,$curlOptions);


      $response = curl_exec($ch);

      if (curl_errno($ch)) {
         $this -> _errors = curl_error($ch);
         curl_close($ch);
         return false;

      } else  {
         curl_close($ch);
         $responseArray = array();
         parse_str($response,$responseArray); 
         return $responseArray;
      }
   }
}
?>

Then I'm making the initial request like this:

session_start();
require_once('Paypal.php');
$paypal = new Paypal();
$amount = 1;

$requestParams = array(
   'RETURNURL' => 'http://localhost/tester/paypal/new_test/test_done.php',
   'CANCELURL' => 'http://localhost/tester/paypal/new_test/test_cancel.php',
   'NOSHIPPING' => '1',
   'ALLOWNOTE' => '1',
   'L_BILLINGTYPE0' => 'RecurringPayments',
   'L_BILLINGAGREEMENTDESCRIPTION0' => 'site donation'
);

$orderParams = array(
   'PAYMENTREQUEST_0_AMT' => '1',
   'PAYMENTREQUEST_0_CURRENCYCODE' => 'USD',
   'PAYMENTREQUEST_0_ITEMAMT' => $amount
);

$item = array(
   'L_PAYMENTREQUEST_0_NAME0' => 'site donation',
   'L_PAYMENTREQUEST_0_DESC0' => 'site donation',
   'L_PAYMENTREQUEST_0_AMT0' => $amount,
   'L_PAYMENTREQUEST_0_QTY0' => '1'
);


$response = $paypal->request('SetExpressCheckout', $requestParams + $orderParams + $item);
$sandbox_location = 'https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=';


if(is_array($response) && $response['ACK'] == 'Success'){
    $token = $response['TOKEN'];
    $_SESSION['token'] = $token;
    header('Location: ' . $sandbox_location . urlencode($token)); 
}

?>

As you can see I'm using the SetExpressCheckout API method to get the token that I need and store it in a session so that I can use it later with the request for CreateRecurringPaymentsProfile.

I'm currently redirected to a page similar to this:

enter image description here

Once the user is done logging in with paypal and confirming the amount it redirects to the success page that I've specified which contains this code:

session_start();
require_once('Paypal.php');

$amount = 1;
$paypal = new Paypal();

$token_param = array('TOKEN' => $_SESSION['token']);

$current_date = date('Y-m-d');

$recurring_payment_params = array(
  'PROFILESTARTDATE' => gmdate('Y-m-d H:i:s', strtotime($current_date . ' + 1 months')),
  'DESC' => 'site donation',
  'BILLINGPERIOD' => 'Month',
  'BILLINGFREQUENCY' => '1',
  'TOTALBILLINGCYCLES' => '0',
  'AMT' => $amount
);


$recurringpayment_response = $paypal->request('CreateRecurringPaymentsProfile', $recurring_payment_params + $token_param);

This works, I've verified in the sandbox account that the recurring payment profile was created and that the next billing due is next month. But the problem is that its not really visible in the paypal interface (the screenshot earlier) that they're paying for a subscription. Perhaps I'm getting the redirect url wrong? (https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=) or do I have to add additional arguments to the SetExpressCheckout method? Please help.

Upvotes: 0

Views: 1837

Answers (1)

Drew Angell
Drew Angell

Reputation: 26036

You're only showing the login screen. After you login you'll see information about the subscription and the button will see "Agree and Pay" or "Agree and Continue" (depending on your useraction value in the return URL) instead of just "Pay" or "Continue".

Upvotes: 1

Related Questions