简体   繁体   English

正确付款后如何单独插入购物车的产品 - 条纹?

[英]How to individually insert the products of the shopping cart after correctly making the payment - Stripe?

Using the PayPal payment gateway where all the products added to the shopping cart are issued by means of its form and the generation thereof with PHP 使用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++;
    }
?>

And its return of values: 它的价值回归:

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
)

And then inserting the products individually with your payment status either completed or pending is very easy 然后单独插入产品,您的付款状态已完成或待处理非常容易

$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();
}

Obtaining the following registered result: 获得以下注册结果: 介绍lacripcióndela imagenaquí

How can I get these same results using the stripe payment gateway? 如何使用条带支付网关获得相同的结果?

I already have the following base: 我已经有以下基数:

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!';

Obtaining the following registered result: 获得以下注册结果: 介绍lacripcióndela imagenaquí

You already have a good start. 你已经有了一个良好的开端。 You'll want to add at least one additional call to ensure you get all of the data you need if you want to try this approach. 如果您想尝试这种方法,您需要添加至少一个额外的调用以确保获得所需的所有数据。 I would also add an additional transfer_group to your charge titles, which I will touch on later. 我还会在你的收费标题中添加一个额外的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!';

That endpoint will get you the inscribed date, the only other piece of data you seem to be missing based on a quick review. 该端点将为您提供铭文日期,这是您在快速审核时似乎缺少的唯一其他数据。 The Customer information you already have, you just need to pass it into the customer object. 您已拥有的客户信息,您只需将其传递给客户对象即可。

As for the quantity, you can either manually keep track of the amount of the product and perform multiple charges, updating as you go along. 至于数量,您可以手动跟踪产品的数量并执行多项费用,随着时间的推移进行更新。 Perform one large charge and keep track of it in the description and parse the value out. 执行一次大额充电并在说明中跟踪它并解析出值。 Or you can set up separate charges but link them together, which may be best for your case. 或者您可以设置单独的费用,但将它们链接在一起,这可能最适合您的情况。 To do that you'll need to include the transfer_group field, which I have included above. 为此,您需要包含我在上面包含的transfer_group字段。 I'll touch further on this at the bottom. 我将在底部进一步探讨这一点。

https://stripe.com/docs/api/ https://stripe.com/docs/api/

Overall, if you want to keep the same setup you can just perform this action for every individual customer and then pass in the data as you are doing. 总的来说,如果您想保持相同的设置,您可以为每个客户执行此操作,然后按照您的方式传递数据。

However, I would suggest looking at this reference on how to set up Stripe in different ways before you go further. 但是,我建议在进一步研究之前,先看看如何以不同的方式设置Stripe。

https://stripe.com/docs/connect/charges#choosing-approach https://stripe.com/docs/connect/charges#choosing-approach

I would suggest going with the Separate charges and transfers method, as it is geared towards shopping carts like your example. 我建议使用单独的收费和转账方法,因为它适用于购物车,例如你的例子。 Overall this method allows you to make multiple transactions/charges, and link them together with the transfer_group section. 总的来说,这种方法允许您进行多次交易/收费,并将它们与transfer_group部分链接在一起。 If you used this approach, you would be able to better keep track of the number of items as, as you can make separate charges, and also be able to make a single call to gather all of the items currently in the cart with the following call 如果您使用这种方法,您将能够更好地跟踪项目数量,因为您可以单独收费,并且还可以通过以下方式拨打一个电话来收集当前购物车中的所有项目呼叫

\Stripe\Charge::all(["transfer_group" => 'group_name']);

instead of having to make a call for each one. 而不是必须为每个人打电话。

Your overall architecture in this setup would have you make the customer and each individual charge with the same transfer_group and then make a single call to the above api which will return you an array of all of the data you need. 您在此设置中的整体架构将让您使用相同的transfer_group使客户和每个单独的费用,然后只需调用上面的api,它将返回您需要的所有数据的数组。

Let me know if you would need any more information, there was a very short time limit on this and I wanted to make sure I made it. 如果您需要更多信息,请告诉我,有一个非常短的时间限制,我想确保我做到了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM