2015-07-19 123 views
1

嗨我试图实现贝宝结账与laravel 5能够做付款也但我的IPN监听器没有听完所有的付款通知。Laravel 5和Paypal结帐

我没有创建开发者帐户或任何东西。我有我的商家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(); 
} 
    }); 
+0

手动设置'header('HTTP/1.1 200 OK')'有点奇怪。如果客户端请求是HTTP 1.0,该怎么办? –

+0

你的问题到底是什么? PayPal确实会打电话给你的听众,不是吗?当您尝试手动触发它时是否有任何错误? –

+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/

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

如果你没有找到答案我在这些文档中,您可以使用PayPal商户技术支持部门打开一张票: https://ppmts.custhelp.com/

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