本文介绍了未达到notify_url或在沙盒贝宝中可能是错误的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的网站上应用贝宝结帐流程.我想在成功付款后更新数据库.我给出了正确的notify_url,但是这个URL从未被调用过,但是我已经在Sandbox Paypal中成功付款了.我放置了一个电子邮件地址,以检查是否调用了功能upadatePayments(),但是在贝宝响应后没有收到邮件.

I am applying a paypal checkout process in my website.In which i want to update database after the successful payment.I have given the correct notify_url but this url never been called but i have reached at a successful payment in Sandbox Paypal.I have put a email address to check whether the function upadatePayments() is called or not but getting no mail after the paypal response.

我正在使用的代码如下:-

The code i am using is below:-

index.html

index.html

<form class="paypal" action="payments.php" method="post" id="paypal_form" target="_blank">
    <input type="hidden" name="cmd" value="_xclick" />
    <input type="hidden" name="no_note" value="1" />
    <input type="hidden" name="lc" value="UK" />
    <input type="hidden" name="currency_code" value="GBP" />
    <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynow_LG.gif:NonHostedGuest" />
    <input type="hidden" name="first_name" value="Ankush"  />
    <input type="hidden" name="last_name" value="Kalia"  />
    <input type="hidden" name="payer_email" value="[email protected]"  />
    <input type="hidden" name="item_number" value="6789" / >
    <input type="submit"  value="Submit Payment"/>
</form>

payments.php

payments.php

<?php
// Database variables
$host = "localhost"; //database location
$user = "amarhost_mypay"; //database username
$pass = "problem_12345"; //database password
$db_name = "amarhost_paypal_ipn"; //database name

// PayPal settings
$paypal_email = '[email protected]';
$return_url = 'http://50.116.97.120/~amarhost/paypal2/payment-successful.htm';
$cancel_url = 'http://50.116.97.120/~amarhost/paypal2/payment-cancelled.htm';
$notify_url = 'http://50.116.97.120/~amarhost/paypal2/payments.php';

$item_name = 'Test Item';
$item_amount = 5.00;

// Include Functions
include("functions.php");

//Database Connection
$link = mysql_connect($host, $user, $pass);
mysql_select_db($db_name);

// Check if paypal request or response
if (!isset($_POST["txn_id"]) && !isset($_POST["txn_type"])){

    // Firstly Append paypal account to querystring
        $querystring .= "?notify_url=".urlencode($notify_url)."&";
    $querystring .= "business=".urlencode($paypal_email)."&";


    // Append amount& currency (£) to quersytring so it cannot be edited in html

    //The item name and amount can be brought in dynamically by querying the $_POST['item_number'] variable.
    $querystring .= "item_name=".urlencode($item_name)."&";
    $querystring .= "amount=".urlencode($item_amount)."&";

    //loop for posted values and append to querystring
    foreach($_POST as $key => $value){
        $value = urlencode(stripslashes($value));
        $querystring .= "$key=$value&";
    }

    // Append paypal return addresses
    $querystring .= "return=".urlencode(stripslashes($return_url))."&";
    $querystring .= "cancel_return=".urlencode(stripslashes($cancel_url));

    // Append querystring with custom field
    //$querystring .= "&custom=".'123';

    // Redirect to paypal IPN
    header('location:https://www.sandbox.paypal.com/cgi-bin/webscr'.$querystring);
    exit();

}else{

    // Response from Paypal

    // read the post from PayPal system and add 'cmd'
    $req = 'cmd=_notify-validate';
    foreach ($_POST as $key => $value) {
        $value = urlencode(stripslashes($value));
        $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}',$value);// IPN fix
        $req .= "&$key=$value";
    }

    // assign posted variables to local variables
    $data['item_name']          = $_POST['item_name'];
    $data['item_number']        = $_POST['item_number'];
    $data['payment_status']     = $_POST['payment_status'];
    $data['payment_amount']     = $_POST['mc_gross'];
    $data['payment_currency']   = $_POST['mc_currency'];
    $data['txn_id']             = $_POST['txn_id'];
    $data['receiver_email']     = $_POST['receiver_email'];
    $data['payer_email']        = $_POST['payer_email'];
    $data['custom']             = $_POST['custom'];

    // 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.sandbox.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) {

                // Used for debugging
                //@mail("[email protected]", "PAYPAL DEBUGGING", "Verified Response<br />data = <pre>".print_r($post, true)."</pre>");

                // Validate payment (Check unique txnid & correct price)
                $valid_txnid = check_txnid($data['txn_id']);
                $valid_price = check_price($data['payment_amount'], $data['item_number']);
                // PAYMENT VALIDATED & VERIFIED!
                if($valid_txnid && $valid_price){
                                        $to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
                    $orderid = updatePayments($data);
                    if($orderid){
                        // Payment has been made & successfully inserted into the Database
                    }else{
                        // Error inserting into DB
                        // E-mail admin or alert user
                    }
                }else{
                    // Payment made but data has been changed
                    // E-mail admin or alert user
                }

            }else if (strcmp ($res, "INVALID") == 0) {

                // PAYMENT INVALID & INVESTIGATE MANUALY!
                // E-mail admin or alert user

                // Used for debugging
                //@mail("[email protected]", "PAYPAL DEBUGGING", "Invalid Response<br />data = <pre>".print_r($post, true)."</pre>");
            }
        }
    fclose ($fp);
    }
}
?>

functions.php

functions.php

<?php
// functions.php
function check_txnid($tnxid){
    global $link;
    return true;
    $valid_txnid = true;
    //get result set
    $sql = mysql_query("SELECT * FROM `payments` WHERE txnid = '$tnxid'", $link);
    if($row = mysql_fetch_array($sql)) {
        $valid_txnid = false;
    }
    return $valid_txnid;
}

function check_price($price, $id){
    $valid_price = false;
    //you could use the below to check whether the correct price has been paid for the product

    /*
    $sql = mysql_query("SELECT amount FROM `products` WHERE id = '$id'");
    if (mysql_numrows($sql) != 0) {
        while ($row = mysql_fetch_array($sql)) {
            $num = (float)$row['amount'];
            if($num == $price){
                $valid_price = true;
            }
        }
    }
    return $valid_price;
    */
    return true;
}

function updatePayments($data){
    global $link;
    if(is_array($data)){
        $sql = mysql_query("INSERT INTO `payments` (txnid, payment_amount, payment_status, itemid, createdtime) VALUES (
                '".$data['txn_id']."' ,
                '".$data['payment_amount']."' ,
                '".$data['payment_status']."' ,
                '".$data['item_number']."' ,
                '".date("Y-m-d H:i:s")."'
                )", $link);
    return mysql_insert_id($link);
    }
}
?>

推荐答案

您必须登录[email protected]帐户(在www.sandbox.paypal.com下),转到帐户设置,个人资料,即时付款通知设置

You have to log into the [email protected] account (under www.sandbox.paypal.com),Go to the account settings, profile , instant payment notification settings

(https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_profile-ipn-notify)

(https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_profile-ipn-notify)

并确保已启用此功能.

PayPal在反复失败后会自动将其关闭,无论您是否在API调用中指定了该IPN,都将禁用该帐户的所有 IPN.

PayPal automatically turns it off after repeated failures, and that disables all IPN's for the account, regardless of whether you specified it in the API call.

这篇关于未达到notify_url或在沙盒贝宝中可能是错误的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:41