2012-12-14 39 views
4

我有兩個腳本,並希望他們在同一行結合起來。 我已經標記了這條評論:'我需要堆棧溢出幫助'。結合phpmailer_v5.1 use_gmail與貝寶IPN響應

第一個腳本: 這是貝寶IPN應答(https://www.x.com/developers/PayPal/documentation-tools/code-sample/216623):

<?php 
// 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.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')); 

// 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']; 
    $payment_amount = $_POST['mc_gross']; 
    $payment_currency = $_POST['mc_currency']; 
    $txn_id = $_POST['txn_id']; 
    $receiver_email = $_POST['receiver_email']; 
    $payer_email = $_POST['payer_email']; 
} 
else if (strcmp ($res, "INVALID") == 0) { 
    // log for manual investigation 
} 
?> 

第二個腳本:

這是phpmailer_v5.1 use_gmail.php:

<?php 
// example on using PHPMailer with GMAIL 

include("class.phpmailer.php"); 
include("class.smtp.php"); // note, this is optional - gets called from main class if not already loaded 

$mail    = new PHPMailer(); 
$body    = 'this is the body of the email'; 

$mail->IsSMTP(); 
$mail->SMTPAuth = true;     // enable SMTP authentication 
$mail->SMTPSecure = "ssl";     // sets the prefix to the servier 
$mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
$mail->Port  = 465;     // set the SMTP port 

$mail->Username = "[email protected]"; // GMAIL username 
$mail->Password = "password";   // GMAIL password 

$mail->From  = "[email protected]"; 
$mail->FromName = "Webmaster"; 
$mail->Subject = "This is the subject"; 
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body 
$mail->WordWrap = 50; // set word wrap 

$mail->MsgHTML($body); 
$mail->AddReplyTo("[email protected]","Webmaster"); 
$mail->AddAttachment("/path/to/file.zip");    // attachment 
$mail->AddAttachment("/path/to/image.jpg", "new.jpg"); // attachment 
$mail->AddAddress("[email protected]","First Last"); //here I need stackoverflow-help 
$mail->IsHTML(true); // send as HTML 

if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message has been sent"; 
} 
?> 

我有在同folder.use_gmail.php class.smtpclass.phpmailer測試以及工作和發送emails.But只有當我WR在這條線伊特目標電子郵件地址:

$mail->AddAddress("[email protected]","First Last"); 

我想發電子郵件給誰剛纔的payment.How我可以從貝寶的目標電子郵件地址的客戶?

回答

1

在我們的電子商務配置中,我們使用的技巧,使真正的用戶的電子郵件(例如,他用來註冊成我們的客戶數據庫中的一個)通過IPN應答器發送。

當用戶使用貝寶支付,形式發送到貝寶系統有關付款的金額,網址橋樑等信息。這裏有一個例子:

<form name="autoPayFormSubmit" id="autoPayFormSubmit" method="post" action="https://securepayments.paypal.com/cgi-bin/acquiringweb"> 
    <input type="hidden" name="cmd" value="_hosted-payment" /> 
    <input type="hidden" name="subtotal" value="#SUBTOTAL#" /> 
    <input type="hidden" name="shipping" value="#SHIPCOST#" /> 
    <input type="hidden" name="business" value="#NUMBEROFBUSINESS#" /> 
    <input type="hidden" name="paymentaction" value="sale" /> 
    <input type="hidden" name="custom" value=" ## USE ME TO TRICK THE SYSTEM ##" /> 
    <input type="hidden" name="currency_code" value="EUR" /> 
    <input type="hidden" name="shopping_url" value="http://yourwebsite.domain/##" /> 
    <input type="hidden" name="cbt" value="Go back to the shopping" /> 
    <input type="hidden" name="notify_url" value="http://yourwebsite.domain/##" /> 
    <input type="hidden" name="cancel_return" value="http://yourwebsite.domain/##" /> 
    <input type="hidden" name="return" value="http://yourwebsite.domain/##" /> 
    <input type="submit" value="PAYPAL SAFE PAYMENT" onmouseover="this.style.backgroundColor='#CEE4F2';" onmouseout="this.style.backgroundColor='#EAF2F6';" style="font-weight: bold; font-size: 14px; padding: 10px 5px; border-radius: 10px; background: #EAF2F6 none no-repeat scroll 0 0; box-shadow: 3px 3px 5px #888; cursor:pointer;"> 
</form> 

的「絕招」是通過「自定義」輸入發送註冊到系統中的用戶的電子郵件,與其他有用的數據一起。 例如,在我們的電子商務中,我們使用用戶電子郵件,訂單ID和其他「不妥協」值對數組進行序列化。在它被序列化後,我們用我們自己創建的一個crypt類對它進行編碼(或者你可以簡單地使用PHP的mcrypt擴展)。

一旦你的IPN響應,你也將獲得

$custom_encrypted_serialized_variables = $_POST['custom']; 

所以,你可以在第3步替換爲以下內容您的IPN監聽器代碼:

... 
... 
// 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']; 
    $payment_amount = $_POST['mc_gross']; 
    $payment_currency = $_POST['mc_currency']; 
    $txn_id = $_POST['txn_id']; 
    $receiver_email = $_POST['receiver_email']; 
    $payer_email = $_POST['payer_email']; 
    $custom_encrypted_serialized_variables = $_POST['custom']; 

    ... 
    ... 
} 

再入手變量使用通用的反序列化()和解密函數進行解密和反序列化。

隨着您可以與PayPal支付形式的「自定義」變量發送其他有用的數據,向客戶端發送的電子郵件,你就大功告成了!

P.S:我知道這個解決方案不是最優的,也許還有其他解決辦法,但我發現這一個快速,高效。提示和更正是讚賞!

+0

謝謝你這個有價值的提示和技巧。 –