How to individually insert the products of the shopping cart after correctly making the payment?

Viewed 1162

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

<?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: introducir la descripción de la imagen aquí

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: introducir la descripción de la imagen aquí

1 Answers

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.

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. I'll touch further on this at the bottom.

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.

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. 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.

Related