2017-04-22 43 views
0

我一直花費幾個小時使用PayPal IPN,當狀態='Completed'時我無法獲取它來更新數據庫。PayPal IPN - 如何更新數據庫完成

我知道有一百萬個關於這方面的問題,但我無法弄清楚。我想每個人都有不同的問題。

我從互聯網上得到了腳本的一部分,因爲我不熟悉IPN的工作。請允許我分享我當前的代碼(我灰色的代碼不重要的部分):

<?php 

// Connect my my own database and start the session that has the ID of the current account user 
session_start(); 
include("connection.php"); 

/* Greying out 
// STEP 1: Read POST data 

// reading posted data from directly from $_POST causes serialization 
// issues with array data in POST 
// reading raw POST data from input stream instead. 
$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]); 
} 
// read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-validate'; 
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"; 
} 


// STEP 2: Post IPN data back to paypal to validate 

$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr'); // change to [...]sandbox.paypal[...] when using sandbox to test 
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')); 

// In wamp like environments that do not come bundled with root authority certificates, 
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path 
// of the certificate as shown below. 
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); 
if(!($res = curl_exec($ch))) { 
    // error_log("Got " . curl_error($ch) . " when processing IPN data"); 
    curl_close($ch); 
    exit; 
} 
curl_close($ch); 

*/ 

// STEP 3: Inspect IPN validation result and act accordingly 
if (strcmp ($res, "VERIFIED") == 0) { 
    // check whether the payment_status is Completed 
    // check that txn_id has not been previously processed 
    // check that receiver_email is your Primary PayPal email 
    // check that payment_amount/payment_currency are correct 
    // process payment 

    // assign posted variables to local variables 
    $item_name = $_POST['item_name']; 
    $item_number = $_POST['item_number']; 
    $payment_status = $_POST['payment_status']; 
    if ($_POST['mc_gross'] != NULL) 
     $payment_amount = $_POST['mc_gross']; 
    else 
     $payment_amount = $_POST['mc_gross1']; 
    $payment_currency = $_POST['mc_currency']; 
    $txn_id = $_POST['txn_id']; 
    $receiver_email = $_POST['receiver_email']; 
    $payer_email = $_POST['payer_email']; 
    $custom = $_POST['custom']; 

    if($payment_status == "Completed") { 

     if(array_key_exists("id", $_COOKIE) AND $_COOKIE['id']) { 

      $_SESSION["id"] = $_COOKIE["id"]; 

     } 

     if(array_key_exists("id", $_SESSION) AND $_SESSION['id']) { 

      // Update plan and due date to database 
      $id = $_SESSION['id']; 
      $due = date('Y-m-d', strtotime('+1 month')); 

      $query = "UPDATE users SET plan = 'paid' , due_date = '".mysqli_real_escape_string($link, $due)."' WHERE id = ".$id." LIMIT 1"; 
      $result = mysqli_query($link, $query); 

     } 


    } 

} else if (strcmp ($res, "INVALID") == 0) { 
    // log for manual investigation 

} 

>

我把整個沙箱環境設置。我查看了IPN的歷史記錄,並且所有IPN的的發送狀態爲'已發送',HTTP響應爲200。但是,我的數據庫沒有更新。我試圖將MySQL更新查詢放在'Completed'檢查之外,但是這並沒有對我的數據庫做任何事情。

任何幫助將高度讚賞,

彌敦道

回答

1

在你的數據庫列保存PayPal交易的ID並嘗試使用你的數據庫使用您保存的交易ID進行更新。在使用IPN操作時,Cookie - 會話不可取。

你能與你的VERIFIED條件的代碼替換下面的代碼

(代碼下,如果(STRCMP($水庫, 「已檢查」)== 0){}):

$item_name = $_POST['item_name']; 
$item_number = $_POST['item_number']; 
$payment_status = $_POST['payment_status']; 
if ($_POST['mc_gross'] != NULL) 
    $payment_amount = $_POST['mc_gross']; 
else 
    $payment_amount = $_POST['mc_gross1']; 
$payment_currency = $_POST['mc_currency']; 
$txn_id = $_POST['txn_id']; 
$receiver_email = $_POST['receiver_email']; 
$payer_email = $_POST['payer_email']; 
$custom = $_POST['custom']; 

if($payment_status == "Completed") { 
     $due = date('Y-m-d', strtotime('+1 month')); 
     $query = "UPDATE users SET plan = 'paid' , due_date = '".mysqli_real_escape_string($link, $due)."' WHERE txn_id = '".$txn_id."'"; 
     $result = mysqli_query($link, $query);  

} 

我希望以上能解決你的問題!

+0

我已經想通了,但我知道,如果我沒有,我會非常感激。 ;-)接受。 – Nathan