php - 如何在paypal中设置定期付款

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

?>

如您所见,我正在使用 SetExpressCheckout API 方法获取我需要的 token 并将其存储在 session 中,以便稍后在 CreateRecurringPaymentsProfile 请求中使用它

我目前被重定向到与此类似的页面:

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 方法添加额外的参数?请帮忙。

最佳答案

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

关于php - 如何在paypal中设置定期付款,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20320068/

相关文章:

php - 使用 PHP API 编辑 Mikrotik 用户配置文件

php - 如何在 PHP 中从 0-X 获取加密强整数?

php - 根据 json 响应选择单选按钮

php - 无法通过 cURL 连接到 PayPal API

php - Paypal API 'Timeout processing request' 错误

paypal - 使用即时付款通知取消 Paypal 定期付款

ios6 - 中国和新西兰的 Paypal 沙盒账户

php - 在 codeigniter 中显示来自 mysql 的图像

php - 搜索引擎如何处理查询字符串?

php - Paypal 休息api