如何在paypal中设置定期付款

时间:2013-12-02 02:48:26

标签: php paypal

我正在尝试使用PHP在paypal中设置定期付款。但我遇到的问题是,我不知道我做的是否正确。我有这个类向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;
      }
   }
}
?>

然后我正在做这样的初始请求:

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)); 
}

&GT;

正如您所看到的,我正在使用SetExpressCheckout API方法获取我需要的令牌并将其存储在会话中,以便稍后我可以将其用于CreateRecurringPaymentsProfile的请求。< / p>

我目前正被重定向到类似这样的页面:

enter image description here

一旦用户完成使用paypal登录并确认其重定向到我指定的包含此代码的成功页面的数量:

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);

这样做有效,我已经在沙盒帐户中验证了定期付款资料的创建以及下一个到期的结算是下个月。但问题是它在paypal接口(前面的屏幕截图)中并不是真的可见,他们正在为订阅付费。也许我的重定向网址错了? (https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=)或者我必须在SetExpressCheckout方法中添加其他参数吗?请帮忙。

1 个答案:

答案 0 :(得分:1)

您只显示登录屏幕。登录后,您将看到有关订阅的信息,按钮将显示“同意​​并支付”或“同意并继续”(取决于您在返回网址中的使用价值),而不仅仅是“付款”或“继续”。