2015-07-19 82 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消息和服務器的響應。