简体   繁体   English

如何在贝宝中设置定期付款

[英]how to setup recurring payments in paypal

I'm trying to setup recurring payments in paypal with PHP. 我正在尝试使用PHP在Paypal中设置定期付款。 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: 我有一个向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 . 如您所见,我正在使用SetExpressCheckout API方法来获取所需的令牌并将其存储在会话中,以便以后可以将其与CreateRecurringPaymentsProfile的请求一起使用。

I'm currently redirected to a page similar to this: 我目前已重定向到类似于以下的页面:

在此处输入图片说明

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: 用户完成使用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);

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? 也许我把重定向URL弄错了吗? ( https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token= ) or do I have to add additional arguments to the SetExpressCheckout method? https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token= )还是我必须向SetExpressCheckout方法添加其他参数? Please help. 请帮忙。

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". 登录后,您将看到有关订阅的信息,并且该按钮将显示“同意​​并付款”或“同意并继续”(取决于返回URL中的useraction值),而不仅仅是“支付”或“继续”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM