在Codeigniter中自动进行订阅的定期付款计划

时间:2019-11-11 04:08:17

标签: php codeigniter paypal paypal-ipn

在这里,我想为我的订阅计划在Paypal中使用自动重复付款。我找到了一些解决方案,但不能在IPN定期付款中使用。 首次付款有效,但无法自动扣除定期付款。

我正在使用HTML代码在Paypal网关中付款。

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">

    <!-- Identify your business so that you can collect the payments. -->
    <input type="hidden" name="business" value="alice@mystore.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">

    <!-- Set recurring payments until canceled. -->
    <input type="hidden" name="src" value="1">

    <!-- Display the payment button. -->
    <input type="image" name="submit"
    src="https://www.paypalobjects.com/en_US/i/btn/btn_subscribe_LG.gif"
    alt="Subscribe">
    <img alt="" width="1" height="1"
    src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
</form>

1 个答案:

答案 0 :(得分:0)

对于自动重复付款,您还必须使用ipn。在提交按钮之前,在HTML表单中添加以下代码。

<input type="hidden" name="custom" value="User_id">
<!-- specify urls -->
<!-- paypal_cancel_url -->
<input type="hidden" name="cancel_return" value="http://localhost/xxxxx/paypal/cancel">
<!-- paypal success return url -->
<input type="hidden" name="return" value="http://localhost/xxxxx/paypal/success">
<!-- paypal ipn return url for auto recurring payment -->
<input type="hidden" name="notify_url" value="http://localhost/xxxxx/paypal/ipn">
  • 当您说第一次付款有效时,我仅显示定期付款指南。在PayPal控制器中添加以下代码。
function ipn(){


   if ($_SERVER['REQUEST_METHOD'] != "POST") die ("No Post Variables");
    $req = 'cmd=_notify-validate';

    // Read the post from PayPal
    foreach ($_POST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $req .= "&$key=$value";
    }

    /* i'm using sandbox for demo change it with live */
    $paypalURL = 'https://www.sandbox.paypal.com/cgi-bin/webscr';


    // Now Post all of that back to PayPal's server using curl, and validate everything with PayPal
    // We will use CURL instead of PHP for this for a more universally operable script (fsockopen has issues on some environments)
    //$url = "https://www.sandbox.paypal.com/cgi-bin/webscr"; //USE SANDBOX ACCOUNT TO TEST WITH
    //$url = "https://www.paypal.com/cgi-bin/webscr"; //LIVE ACCOUNT

    $curl_result=$curl_err='';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$paypalURL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($req)));
    curl_setopt($ch, CURLOPT_HEADER , 0);   
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $curl_result = @curl_exec($ch);
    $curl_err = curl_error($ch);
    curl_close($ch);

    $req = str_replace("&", "\n", $req);  // Make it a nice list in case we want to email it to ourselves for reporting

    // Check that the result verifies with PayPal
    if (strpos($curl_result, "VERIFIED") !== false) {
        $req .= "\n\nPaypal Verified OK";
    } else {
        $req .= "\n\nData NOT verified from Paypal!";
        //mail("email@gmail.com", "IPN interaction not verified", "$req", "From: email@gmail.com" );
        exit();
    }


    if (array_key_exists("txn_id", $_POST)) {

        /* all response for future use conver it into json format */
        $payment_rawData = json_encode($_POST);

        $item_number = $_POST['item_number'];
        $txn_id = $_POST['txn_id'];
        $amount = $_POST['mc_gross'];
        $currency_code = $_POST['mc_currency'];
        $custom = $_POST['custom'];
        $payment_status = $_POST['payment_status'];

        //Insert tansaction data into the database
        $payment_info = array('payment_userId'=>$custom,'payment_planId'=>$item_number,'payment_txnId'=>$txn_id,'payment_amount'=>$amount,'payment_currency'=>$currency_code,'payment_status'=> $payment_status,'payment_rawData'=>$payment_rawData,'createdDate'=>date('Y-m-d H:i:s'),'updatedDate'=>date('Y-m-d H:i:s'));

        $this->paypal_model->insertTransaction($payment_info);
    }
}