【问题标题】:Braintree Payment Gateway - processing ordersBraintree 支付网关 - 处理订单
【发布时间】:2023-03-25 03:18:01
【问题描述】:

我正在尝试使用 Braintree 支付网关创建一个电子商务网站。

我已经使用 dropin 容器创建了表单,如下所示https://www.youtube.com/watch?v=dUAk5kwKfjs

然后我尝试添加处理这些数据并将其发送到 Braintree 并将所有内容存储在我的数据库中。这是我的PHP 处理:

<?php include_once("connection.php"); ?>

<?php
var_dump($_POST);
session_start();
require "boot.php";

$active_country_code = $_SESSION["active_country_code"];
$active_country_braintree = $_SESSION["active_country_braintree"];

$subtotal = $_POST['subtotal'];
$vat = $_POST['vat'];
$vat_percent = $_POST['vat_percent'];
$total = $_POST['total'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$nonce = $_POST['payment_method_nonce'];
$serialized_cart_array = $_POST['cart_array'];
$cart_array = unserialize($serialized_cart_array);
$currency = $active_country_currency;
$customer_id = $_POST['customer_id'];
$address = $_POST['address'];

$params = [$customer_id,$address,$active_country_braintree,$serialized_cart_array,$subtotal,$vat,$vat_percent,$total,$currency,$active_country_code];
$sql = "INSERT INTO orders (customer_id,address_id,braintree_account,cart_array,subtotal,vat,vat_percent,total,currency,country_id,date_created,status,date_last_status_change) VALUES (?,?,?,?,?,?,?,?,?,?,now(),'created',now())";
$stmt = DB::run($sql,$params);
$order_id = DB::lastInsertId();

if (!isset($_POST['payment_method_nonce']) || $_POST['payment_method_nonce']!="") {
    echo 'fail';
}

$result = Braintree_Transaction::sale([
    'amount' => $total,
    'orderId' => $order_id,
    'merchantAccountId' => $active_country_braintree,
    'paymentMethodNonce' => $nonce,
    'customer' => [
        'firstName' => $first_name,
        'lastName' => $last_name,
        'email' => $email
    ], 
    'options' => [
        'submitForSettlement' => true
    ]
]);

if ($result->success === true) {
    $transaction_id = $result->transaction->id;
    $params = [$transaction_id,$order_id];
    $sql = "UPDATE orders SET transaction_id=?, status='processing payment', date_last_status_change=now() WHERE id=?";
    $stmt = DB::run($sql,$params);
}else{
    $error = serialize($result->errors);
    $params = [$error,$order_id];
    $sql = "UPDATE orders SET errors=? WHERE id=?";
    $stmt = DB::run($sql,$params);
}
?>

这会产生以下输出:

array (size=12)
  'first_name' => string 'Paddy' (length=5)
  'last_name' => string 'Hallihan' (length=8)
  'email' => string 'it@sublift.ie' (length=13)
  'address' => string '2' (length=1)
  'customer_id' => string '2' (length=1)
  'subtotal' => string '196' (length=3)
  'vat' => string '45.08' (length=5)
  'vat_percent' => string '23' (length=2)
  'total' => string '241.08' (length=6)
  'guest_checkout' => string 'true' (length=4)
  'cart_array' => string 'a:1:{i:0;a:4:{s:7:"item_id";s:1:"1";s:8:"quantity";d:1;s:9:"attribute";s:10:"attribute1";s:15:"attribute_price";s:3:"196";}}' (length=124)
  'payment_method_nonce' => string '59698de8-8d4e-0a05-5d02-e8d512057712' (length=36)

fail

正如您所见,它正在回显“失败”,因为 $_POST['payment_method_nonce'] 不存在,但我也可以看到它存在,尽管 var_dump($_POST); 确实很奇怪。

事实上,它正在向我的表中添加 2 个订单。第一个从 Braintree 收到错误,声明 'Cannot determine payment method',第二个得到正确处理并从 Braintree 获取事务 ID。

就像页面没有它就无法运行随机数,然后刷新并正确读取发布的变量。

将除var_dump($_POST); 之外的所有内容都注释掉会导致:

array (size=12)
  'first_name' => string 'Paddy' (length=5)
  'last_name' => string 'Hallihan' (length=8)
  'email' => string 'it@sublift.ie' (length=13)
  'address' => string '5' (length=1)
  'customer_id' => string '5' (length=1)
  'subtotal' => string '196' (length=3)
  'vat' => string '45.08' (length=5)
  'vat_percent' => string '23' (length=2)
  'total' => string '241.08' (length=6)
  'guest_checkout' => string 'true' (length=4)
  'cart_array' => string 'a:1:{i:0;a:4:{s:7:"item_id";s:1:"1";s:8:"quantity";d:1;s:9:"attribute";s:10:"attribute1";s:15:"attribute_price";s:3:"196";}}' (length=124)
  'payment_method_nonce' => string '' (length=0)

对此的任何帮助将不胜感激。

【问题讨论】:

    标签: php post braintree


    【解决方案1】:

    问题是我如何提交到此页面

    我正在使用:

    <button onclick=this.form.submit();>Pay Now</button>
    

    我现在正在使用:

    <input type="submit" value="Pay Now">
    

    但是,我不确定这首先导致问题的原因。我认为它们的行为应该相同。

    【讨论】:

    • 如果您从该 youtube 视频中复制了代码,他们正在使用 JSv2 的 Drop-In 集成 (developers.braintreepayments.com/guides/drop-in/…)。 Drop-In 劫持表单提交行为以在提交中注入付款方式随机数。通过单击按钮后立即提交,您将跳过 Drop-In 行为并且不会收到付款方式 nonce。
    猜你喜欢
    • 1970-01-01
    • 2018-01-09
    • 2016-05-01
    • 2014-12-06
    • 2017-02-04
    • 2013-05-12
    • 2013-01-21
    • 2012-05-12
    • 2019-04-18
    相关资源
    最近更新 更多