正确支付后如何单独插入购物车的商品?
Posted
技术标签:
【中文标题】正确支付后如何单独插入购物车的商品?【英文标题】:How to individually insert the products of the shopping cart after correctly making the payment? 【发布时间】:2019-11-03 07:11:39 【问题描述】:使用 PayPal 支付网关,所有添加到购物车的产品都通过其形式发行并使用 PHP
生成<?php
while ($stmt->fetch())
$key = array_search($id_product, $items);
$total = $total + $price * $qty[$key];
?>
<input name="item_number_<?php echo $num;?>" type="hidden" value="<?php echo $id_product; ?>">
<input name="item_name_<?php echo $num;?>" type="hidden" value="<?php echo $product; ?>">
<input name="amount_<?php echo $num;?>" type="hidden" value="<?php echo number_format($price,2);?>">
<input name="quantity_<?php echo $num;?>" type="hidden" value="<?php echo $qty[$key];?>">
<?php
$num++;
?>
及其返回值:
Array
(
[payer_email] => testing-instructor@site.com
[payer_id] => J95XSJRX4WXVS
[payer_status] => VERIFIED
[first_name] => Milck
[last_name] => Jmkc
[address_name] => Milck Jmkc
[address_street] => calle Vilamar� 76993- 17469
[address_city] => Albacete
[address_state] => Albacete
[address_country_code] => ES
[address_zip] => 02001
[residence_country] => ES
[txn_id] => 71U73747YB4505702
[mc_currency] => EUR
[mc_gross] => 524.00
[protection_eligibility] => INELIGIBLE
[payment_gross] => 524.00
[payment_status] => Pending
[pending_reason] => unilateral
[payment_type] => instant
[item_name1] => iPhone X
[item_number1] => 1
[quantity1] => 1
[mc_gross_1] => 100.00
[item_name2] => HP Notebook
[item_number2] => 2
[quantity2] => 1
[mc_gross_2] => 299.00
[item_name3] => Laptop Bag
[item_number3] => 3
[quantity3] => 1
[mc_gross_3] => 125.00
[num_cart_items] => 3
[txn_type] => cart
[payment_date] => 2019-06-20T12:39:01Z
[business] => nuestrocorreo@gmail.com
[notify_version] => UNVERSIONED
[verify_sign] => AafExQbaKRVsBflEXz3fG2x2IuLiAsrid.bh8IJziQRbs.cEOXQNrEFI
)
然后单独插入您的付款状态为已完成或待处理的产品非常容易
$stmt = $mysqli->prepare("INSERT INTO orden (id_user, id_product, product, quantity, price, payment_method) VALUES (?,?,?,?,?,?)");
for($i = 1;$i <= $_POST['num_cart_items']; $i++)
$stmt->bind_param("iisiss",
$id_users,
$_POST['item_number' . $i],
$_POST['item_name' . $i],
$_POST['quantity' . $i],
$_POST['mc_gross_' . $i],
$transaccionPayPal['payment_status']
);
$stmt->execute();
获得以下注册结果:
如何使用条带支付网关获得相同的结果?
我已经有以下基地:
try
$customer = \Stripe\Customer::create(array(
'email' => 'customer2@example.com',
'card' => $token
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => 500,
'description' => 'Event charge',
'currency' => 'usd'
));
$transactionData = [
'id' => $charge->id,
'id_customer' => $charge->customer,
'product' => $charge->description,
'amount' => $charge->amount,
'currency' => $charge->currency,
'status' => $charge->status
];
$stmt = $con->prepare("INSERT INTO stripe (token_stripe, id_transaction, id_customer, product, currency, amount, status) VALUES (?,?,?,?,?,?,?)");
$stmt->bind_param("sssssss", $token,$transactionData['id'],$transactionData['id_customer'],$transactionData['product'],$transactionData['currency'],$transactionData['amount'],$transactionData['status']);
$stmt->execute();
echo 'Successful transaction!';
获得以下注册结果:
【问题讨论】:
【参考方案1】:你已经有了一个好的开始。如果您想尝试这种方法,您将需要至少添加一个额外的调用以确保获得所需的所有数据。我还会在您的收费标题中添加一个额外的 transfer_group,稍后我会谈到。
try
$customer = \Stripe\Customer::create(array(
'email' => 'customer2@example.com',
'card' => $token
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => 500,
'description' => 'Event charge',
'currency' => 'usd',
'transfer_group' => 'Group Name'
));
$transactionCall = \Stripe\BalanceTransaction::retrieve($charge-> balance_transaction);
$transactionData = [
'id' => $charge->id,
'id_customer' => $charge->customer,
'product' => $charge->description,
'amount' => $charge->amount,
'currency' => $charge->currency,
'status' => $charge->status
];
$stmt = $con->prepare("INSERT INTO stripe (token_stripe, id_transaction, id_customer, product, currency, amount, status, inscribedDate,) VALUES (?,?,?,?,?,?,?)");
$stmt->bind_param("sssssss", $token,$transactionData['id'],$transactionData['id_customer'],$transactionData['product'],$transactionData['currency'],$transactionData['amount'],$transactionData['status'], $transactionCall['created'], );
$stmt->execute();
echo 'Successful transaction!';
该端点将为您提供记录的日期,这是您根据快速查看似乎缺少的唯一其他数据。您已经拥有的客户信息,您只需将其传递给客户对象即可。
至于数量,您可以手动跟踪产品数量并执行多次收费,并随时更新。执行一大笔费用并在描述中跟踪它并解析出价值。或者您可以设置单独的费用,但将它们链接在一起,这可能最适合您的情况。为此,您需要包含我在上面包含的 transfer_group 字段。我将在底部进一步讨论。
https://stripe.com/docs/api/
总体而言,如果您想保持相同的设置,您只需为每个单独的客户执行此操作,然后照常传递数据。
但是,我建议您先查看这篇关于如何以不同方式设置 Stripe 的参考资料,然后再进一步了解。
https://stripe.com/docs/connect/charges#choosing-approach
我建议使用“单独收费和转移”方法,因为它适用于像您的示例这样的购物车。总体而言,此方法允许您进行多笔交易/收费,并将它们与 transfer_group 部分链接在一起。如果您使用这种方法,您将能够更好地跟踪商品数量,因为您可以单独收费,并且还可以通过以下方式进行一次调用以收集当前购物车中的所有商品打电话
\Stripe\Charge::all(["transfer_group" => 'group_name']);
而不必为每个人打电话。
您在此设置中的整体架构将让您使用相同的 transfer_group 向客户和每个人收费,然后对上述 api 进行一次调用,它将返回您需要的所有数据的数组。
【讨论】:
以上是关于正确支付后如何单独插入购物车的商品?的主要内容,如果未能解决你的问题,请参考以下文章
根据 WooCommerce 购物车中的商品数量和产品类别禁用支付网关
01 场景:一个真实电商订单系统的整体架构业务流程及负载情况
基于springboot+vue+redis前后端分离家具商城平台系统项目(源码+论文初稿可以直接运行)015主要设计:用户登录注册商城分类商品浏览查看购物车订单支付以及后台的管理