每月在PHP中重复支付PayPal

时间:2015-09-18 05:55:49

标签: php paypal paypal-ipn paypal-sandbox paypal-subscriptions

我正在研究在PHP中定期支付paypal。我看过一些例子,下面是我觉得很容易理解的代码。

    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<!-- Identify your business so that you can collect the payments. -->
<input type="hidden" name="business" value="info@capleswebdev.com">
<!-- Specify a Subscribe button. -->
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<!-- Identify the subscription. -->
<input type="hidden" name="item_name" value="Alice's Weekly Digest">
<input type="hidden" name="item_number" value="DIG Weekly">
<!-- Set the terms of the regular subscription. -->
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="a3" value="5.00">
<input type="hidden" name="p3" value="1">
<input type="hidden" name="t3" value="M">
<!-- Display the payment button. -->
<input type="image" name="submit" border="0" src="https://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif" alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" src="https://www.paypal.com/en_US/i/scr/pixel.gif" >
</form> 

根据上述代码,每个月将收取5美元。我真的不知道该代码是否有效。我也想知道在上面的代码中没有Paypal API用户名,密码和密钥的选项只有它获得了在paypal注册的电子邮件ID的业务。我完全感到困惑,如果它能工作或不工作或如何做得好。请建议我一些事情,如何在用户取消或成功将钱存入我的PayPal账户后才能收到数据

1 个答案:

答案 0 :(得分:0)

为避免重复测试,您可以使用价格进行测试,例如0,01。要创建定期付款,请使用以下HTML格式:

<form method="post" name="formName" id="submitThisForm" action="https://www.paypal.com/cgi-bin/webscr" >
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<input type="hidden" name="business" value="your@papypamail.com" />
<input type="hidden" name="item_name" value="Your Membership" />
<input type="hidden" name="a3" value="0.01">
<input type="hidden" name="p3" value="1"> 
<input type="hidden" name="t3" value="M">
<input type="hidden" name="src" value="1">
<input type="hidden" name="sra" value="1">
<input type="hidden" name="item_number" value="2" />
<input type="hidden" name="custom" value="SECURITYCODE" />
<input type="hidden" name="currency_code" value="USD" />
<input type="hidden" name="quantity" value="1" />
<input type="hidden" name="no_shipping" value="1" />
<input type="hidden" name="return" value="page going after payment" />
<input type="hidden" name="cancel_return" value="" />
<input type="hidden" name="cbt" value="ITEM DESCRIPTION" />
<input type="hidden" name="rm" value="2" />
<input type="hidden" name="notify_url" value="your_listener_file.php" />

当用户取消会员资格时,paypal将通知“notify_url” - 在您的情况下,这将是文件your_listener_file.php。在此文件中,您必须检查paypal POST变量'txn_type'='subscr_cancel'。有几个要点:

  1. 您必须验证ipn事务:

        $post           = array( 'cmd' => '_notify-validate' );
    foreach($_POST as $key=>$value){
            $post[$key] = $value;
    }
    $c  = curl_init();
    curl_setopt_array($c, array(
        CURLOPT_FOLLOWLOCATION  => TRUE,
        CURLOPT_RETURNTRANSFER  => TRUE,
        CURLOPT_CONNECTTIMEOUT  => 15,
        CURLOPT_MAXREDIRS       => 15,
        CURLOPT_TIMEOUT         => 15,
        CURLOPT_URL             => 'https://www.paypal.com/cgi-bin/webscr',
        CURLOPT_POST            => TRUE,
        CURLOPT_POSTFIELDS      => $post,
    ));
    $res = curl_exec($c);
    curl_close($c);
    $res    = trim($res);
    
    if( $res != 'VERIFIED' ) {
        exit();
    }
    
  2. 第二 - 检查是使用唯一键在数据库中存在的事务。您必须使用Paypal POST变量'custom'。

  3. 如果交易存在,只需进行一些简单的检查:

    if( !empty($_POST['txn_type']) && $_POST['txn_type'] == 'subscr_cancel' )
        $paypalData['approved'] = 0;