收到PayPal付款时运行脚本

时间:2012-08-20 18:58:10

标签: php paypal notifications

我想进行设置,以便当用户通过PayPal付款时,将运行PHP脚本。我该如何设置此类通知?

3 个答案:

答案 0 :(得分:2)

它被称为IPN https://www.paypal.com/ipn 你只需要进入你的设置,启用IPN,告诉它将处理回调的文件,然后编写你的php文件。

答案 1 :(得分:1)

以下是我如何用php处理paypal ipns

<?php
if($_POST){

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// check the payment_status is Completed

// assign posted variables to local variables - NOT NECESSARY..
$receiver_email = mysql_real_escape_string($_POST['receiver_email']);
$payer_email = mysql_real_escape_string($_POST['payer_email']);
$business_id = mysql_real_escape_string($_POST['item_number']);
$payment_gross = mysql_real_escape_string($_POST['mc_gross']);
$item_name = mysql_real_escape_string($_POST['item_name']);
$currency = mysql_real_escape_string($_POST['mc_currency']);
$payment_date = mysql_real_escape_string($_POST['payment_date']);
$payment_status = mysql_real_escape_string($_POST['payment_status']);
$address_street = mysql_real_escape_string($_POST['address_street']);
$first_name = mysql_real_escape_string($_POST['first_name']);
$last_name = mysql_real_escape_string($_POST['last_name']);
$address_street = mysql_real_escape_string($_POST['address_street']);
$address_city = mysql_real_escape_string($_POST['address_city']);
$address_state = mysql_real_escape_string($_POST['address_state']);
$address_country_code = mysql_real_escape_string($_POST['address_country_code']);
$address_zip = mysql_real_escape_string($_POST['address_zip']);
$quantity = mysql_real_escape_string($_POST['quantity']);
$payment_fee = mysql_real_escape_string($_POST['mc_fee']);
$address_name = mysql_real_escape_string($_POST['address_name']);
$payment_type = mysql_real_escape_string($_POST['payment_type']);
$payer_business_name = mysql_real_escape_string($_POST['payer_business_name']);
$item_number = mysql_real_escape_string($_POST['item_number']);
$transaction_subject = mysql_real_escape_string($_POST['transaction_subject']);
$parent_txn_id = mysql_real_escape_string($_POST['parent_txn_id']);
$txn_id = mysql_real_escape_string($_POST['txn_id']);
$custom = mysql_real_escape_string($_POST['custom']);

//insert into payments table
    $result = mysql_query("INSERT INTO payments (payment_date, payment_type, receiver_email, payer_email, gross_amt, quantity, currency, payment_status, paypal_fee, transaction_subject, item_name, item_number, first_name, last_name, payer_business_name, address_name, address_street,address_city,address_state,address_zip,address_country_code,parent_txn_id, txn_id, custom) VALUES ('$payment_date','$payment_type', '$receiver_email', '$payer_email', '$payment_gross','$quantity','$currency','$payment_status','$payment_fee','$transaction_subject','$item_name','$item_number','$first_name','$last_name','$payer_business_name','$address_name','$address_street','$address_city','$address_state','$address_zip','$address_country_code','$parent_txn_id','$txn_id','$custom')") or die(mysql_error());       

//end of if invalid 
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}

//end of post
}

 ?>

答案 2 :(得分:0)

PayPal提供您必须通过帐户设置的即时付款通知系统。

以下是指导您入门的链接。 https://cms.paypal.com/cms_content/US/en_US/files/developer/IPNGuide.pdf

他们还为您的IPN听众提供模板。这是PHP中的一个:

<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=' . urlencode('_notify-validate');

foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HEADER, 0);
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_HTTPHEADER, array('Host: www.paypal.com'));
$res = curl_exec($ch);
curl_close($ch);


// 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'];


if (strcmp ($res, "VERIFIED") == 0) {
    // check 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
}    
else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
}
?>