首页 > 解决方案 > PayPal 按钮不会将“自定义”字段传递给 IPN

问题描述

我使用带有 PP 按钮的以下页面(从 PP 教程复制,添加了一些字段):

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
    
    <script src="lib/jquery.localize.min.js"></script>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
    
    <script src="lib/alertify/alertify.min.js"></script>    
    <link rel="stylesheet" href="lib/alertify/css/alertify.min.css" />
    <link rel="stylesheet" href="lib/alertify/css/themes/default.min.css" />
    
    <link rel="stylesheet" type="text/css" href="styles.css">
    
  </head>

  <body>
  
    <script src="https://www.paypal.com/sdk/js?client-id=MY_PP_ID&currency=EUR"> 
    </script>

    <div id="paypal-button-container"></div>

    <script>
      paypal.Buttons({
        createOrder: function(data, actions) {
          return actions.order.create({
            purchase_units: [{
                amount: { value: '1' },
                item_name: { value:"points" },
                quantity: { value:"100" },
                no_shipping: { value:"1" },
                custom: { value:'test'},
                currency_code: { value:"EUR" },
                lc: { value:"US" }
            }]
          });
        },
        onApprove: function(data, actions) {
          return actions.order.capture().then(function(details) {
            strInvoiceInfo = "When payment comes through, you will receive points and accompanying documents.\
                <p>You will be able to see the status of your payment under the \"Points Exchanged\" section of menu.\
                <p>\
                <p>Press 'Back' button to return";
            alertify.alert("Transaction completed", strInvoiceInfo);
          });
        }
      }).render('#paypal-button-container'); // Display payment options on your web page
    </script>
                
  </body>
</html>

它有效,我可以在 PP 页面等中看到交易。

现在,我已经为 IPN 通知配置了一个 PHP(通过 PP 页面上的“添加 Web Hooks”):

<?php namespace Listener;

require('includes/PaypalIPN.php');
use PaypalIPN;

$ipn = new PaypalIPN();
$ipn->usePHPCerts();    // Disable certificates

// Use the sandbox endpoint during testing.
$ipn->useSandbox();
$verified = $ipn->verifyIPN();

if ($verified) 
{
    $txtParams = "";
    foreach ($_POST as $key => $value) 
    {
        $txtParams .= $key . " = " . $value . "\r\n";
    }

    if($myfile = fopen("paypal_log.txt", "w"))
    {
        fwrite($myfile, $txtParams);
        fclose($myfile);    
    }       
            
}
header("HTTP/1.1 200 OK");
?>

这是它生成的 paypal_log.txt 文件:

mc_gross = 1.00
protection_eligibility = Eligible
address_status = confirmed
payer_id = Y44K3UE4GLSA6
address_street = calle Vilamar 76993- 17469
payment_date = 03:44:42 Jun 28, 2020 PDT
payment_status = Completed
charset = windows-1252
address_zip = 02001
first_name = John
mc_fee = 0.38
address_country_code = ES
address_name = John Doe
notify_version = 3.9
custom = 
payer_status = verified
business = visualfork@gmail.com
address_country = Spain
address_city = Albacete
quantity = 1
verify_sign = A-8sxAsepLwVCTE9rkoV7Fc5m-kFAsVbIMGBBpRZDl7ovLm-txoyz6Bf
payer_email = fizpok@yandex.ru
txn_id = 5V619447F77018605
payment_type = instant
last_name = Doe
address_state = Albacete
receiver_email = visualfork@gmail.com
payment_fee = 
shipping_discount = 0.00
insurance_amount = 0.00
receiver_id = 37YV8M4PFEFWN
txn_type = express_checkout
item_name = 
discount = 0.00
mc_currency = EUR
item_number = 
residence_country = ES
test_ipn = 1
shipping_method = Default
transaction_subject = 
payment_gross = 
ipn_track_id = 380f4c8945955

如您所见,“自定义”字段为空。此外,我添加的所有其他字段,如“item_name”,也是空的。有什么建议么?

谢谢你。

标签: phppaypalpaypal-ipn

解决方案


            purchase_units: [{
                amount: { value: '1' },
                item_name: { value:"points" },
                quantity: { value:"100" },
                no_shipping: { value:"1" },
                custom: { value:'test'},
                currency_code: { value:"EUR" },
                lc: { value:"US" }
            }]

这在很大程度上是完全错误的。未知变量将被忽略。

有关有效的变量名称,请参阅https://developer.paypal.com/docs/api/orders/v2/#orders_create

例如,你想要custom_id

并且您已将不属于 purchase_units 的东西放在 purchase_units 中,而是将其放在之外application_context,例如shipping_preference: 'NO_SHIPPING'. 并且locale属于 SDK 查询字符串,不属于lc

item_name,数量,需要特定的项目子结构


推荐阅读