Laravel 5和Paypal Checkout

时间:2015-07-19 11:32:11

标签: php paypal laravel-5 paypal-ipn

您好我正在尝试使用laravel 5实现paypal结帐,但是我的IPN监听器根本没有收听支付通知。

我没有创建开发者帐户或其他任何东西。我有我的商家ID并发送其他详细信息,如返回网址,通知网址如下。

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

    <div class="hide" id="errorBox"></div>
    <input type="hidden" name="button" value="subscribe">
    <input type="hidden" name="item_name" value="xyz Membership">
    <input type="hidden" name="quantity" value="1">
    <input type="hidden" name="amount" value="9">
    <input type="hidden" name="currency_code" value="USD">
    <input type="hidden" name="shipping" value="0">
    <input type="hidden" name="tax" value="0">
    <input type="hidden" name="notify_url" value="https://members.xyz.com/ipn">
    <input type="hidden" name="return" value="https://members.xyz.com">
    <input type="hidden" name="cancel" value="https://members.xyz.com">
    <input type="hidden" name="size" value="small">
    <input type="hidden" name="p3" value="1">
    <input type="hidden" name="t3" value="M">
    <input type="hidden" name="custom" value="410">
    <input type="hidden" name="cmd" value="_xclick-subscriptions">
    <input type="hidden" name="a3" value="9">
    <input type="hidden" name="business" value="xxxxxxxxx">
    <input type="hidden" name="bn" value="JavaScriptButton_subscribe">
    <input type="hidden" name="env" value="www">
    <button type="submit" class="paypal-button small">Monthly</button>

   </form>

请在下面找到我的ipn代码。

  Route::post('ipn', function() {


     $payment_settings = PaymentSetting::first();

     $plugin_data = (object) array_build(PluginData::where('plugin_slug', 'paypal')->get(), function($key, $data) {
    return array($data->key, $data->value);
});

     header('HTTP/1.1 200 OK');

     $sandbox = 'sandbox.';
     if($payment_settings->live_mode)
         $sandbox = '';

     $receiver_email   = Input::get('receiver_email');
     $txn_type         = Input::get('txn_type');
     $payment_status   = Input::get('payment_status');
     $payment_amount   = Input::get('mc_gross');
     $payment_currency = Input::get('mc_currency');
     $user_id          = Input::get('custom');


     // reference: https://developer.paypal.com/docs/classic/ipn/ht_ipn/
     $raw_post_data = file_get_contents('php://input');
     $raw_post_array = explode('&', $raw_post_data);
     $myPost = array();
     foreach ($raw_post_array as $keyval) {
        $keyval = explode ('=', $keyval);
       if (count($keyval) == 2)
          $myPost[$keyval[0]] = urldecode($keyval[1]);
     }

     $req = 'cmd=_notify-validate';
     $get_magic_quotes_exists = false;
     if(function_exists('get_magic_quotes_gpc')) {
        $get_magic_quotes_exists = true;
     }
     foreach ($myPost as $key => $value) {
        if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
            $value = urlencode(stripslashes($value));
        } else {
            $value = urlencode($value);
        }
        $req .= "&$key=$value";
     }

     $result = '';
     $used_curl = false;

     if(function_exists('curl_init')) {

       $ch = curl_init('https://www.' . $sandbox . 'paypal.com/cgi-bin/webscr');
       curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
       curl_setopt($ch, CURLOPT_POST, 1);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
       curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
       curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

       $result = curl_exec($ch);
       curl_close($ch);

       if($result !== false)
           $used_curl = true;
      }

if( ! $used_curl) {

    $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
    $header .= "Host: www." . $sandbox . "paypal.com\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

    if($fp = fsockopen('www.paypal.com', 80, $errno, $errstr, 15)) {
        socket_set_timeout($fp, 15);
        fwrite($fp, $header . $req);

        while( ! feof($fp)) {
            $result = fgets($fp, 1024);
            if(strcmp($result, 'VERIFIED') == 0)
                break;
        }

        fclose($fp);
    }
}


if($result == 'VERIFIED' && strtolower($receiver_email) == strtolower($plugin_data->paypal_merchant_id)) {
    $user = User::find($user_id);

    if(
        in_array($txn_type, array('web_accept', 'subscr_payment')) &&
        in_array($payment_amount, array($plugin_data->monthly_price, $plugin_data->yearly_price)) &&
        $payment_currency == 'USD' &&
        $payment_status == 'Completed'
    ) {
        $user->role = 'subscriber';
    $user->stripe_active = 1;

        if($payment_amount == $plugin_data->yearly_price)
            $user->setSubscriptionEndDate(Carbon::now()->addYear());
        else
            $user->setSubscriptionEndDate(Carbon::now()->addMonth());
    }
    elseif($payment_status == 'Reversed' || $payment_status == 'Refunded') {
        $user->setSubscriptionEndDate(Carbon::now());
    }

    $user->save();
}
  });

1 个答案:

答案 0 :(得分:0)

你问题中的评论是一个好的开始。您可能需要检查您的IPN侦听器。您可以设置日志以保存对特定日志文件的调用,以便您查看IPN进入时发生的情况。

这是IPN的基本集成指南: https://developer.paypal.com/docs/classic/products/instant-payment-notification/

以下是一些示例代码,说明如何处理IPN通知并发送响应: https://developer.paypal.com/docs/classic/ipn/ht_ipn/

识别您的PayPal IPN监听器: https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNSetup/

如果您在这些文档中找不到答案,可以使用PayPal Merchant技术支持打开一张票: https://ppmts.custhelp.com/

他们可以帮助您查看您的IPN消息和服务器的响应。