ondrakoupil / csob

PHP client for ČSOB payment gateway eAPI
MIT License
44 stars 18 forks source link

Eshop workflow #11

Closed desss1 closed 7 years ago

desss1 commented 7 years ago

Hello,

I would like to ask about your recommended eshop workflow. Let's take a payment status as an example. For a successful call I need instances of $client and $payment, but since I first redirect the user to the gateway and then the user returns back to my eshop, I must 1) create these instances again (which creates a new payment because of calling payment/init while initializing $payment) or 2) I must store these variables in a session or something like that.

What is your recommendation?

ondrakoupil commented 7 years ago

Hello,

for every payment, you should save somehow (preferably to database) its payID, which is returned from paymentInit method. You can acquire it either from paymentInit's return value, or from Payment object via getPayId after paymentInit has been called. The payID is all you need to call subsequent methods - paymentStatus, paymentRefund etc. There is no need to store the original Payment object. You usually save customer's order's data to database, so you just need to accompany it together with the payId. All subsequent methods can be called later, after the customer already left your website. You only need to create the Client object every time. Good practice is to store all the code to create it to some kind of function or class, so you don't repeat yourself.

Payment in bank's system is created only after calling paymentInit(). You can create Payment objects in PHP whenever you like without sending anything to bank.

The code can look like this:

// 1.php - to start payment
$client = new Client(...);
$payment = new Payment();
// add items to cart, set order number, description, whatever
$returnedData = $client->paymentInit($payment);
$payId = $payment->getId();
// or $payId = $returnedData["payId"];
savePayId($payId, $myOrderId) // this should save the payId to your database to customer's order

// 2.php - customer is returned here
$client = new Client(...);
$someData = $client->receiveReturningCustomer();
echo "Thank you for your payment";
// Esentially, you don't need to do this. You already have everything you need.
// However, you might want to check if the payment was successful using paymentStatus()

// 3.php - later, you want to check payment status
$client = new Client(...);
$payId = loadPayId($payId, $myOrderId); // This should load the payId from your database
$status = $client->paymentStatus($payId);

Is it clearer?

desss1 commented 7 years ago

Thank you for your reply

If the user is redirected to the gateway and he clicks on 'cancel the payment' and gets redirected back to the eshop (with an unsuccessful payment status) -- do you create a new payment (with new payment/init call) in this case? So he has an option to pay again.

ondrakoupil commented 7 years ago

In this case, I'd show a button that enables the user to start a new payment. If he clicks it, you start a new payment with new payment/init call, receive new payId and go through the process again.

desss1 commented 7 years ago

Thank you, it is much cleaner as I am thinking about it a few days later. But I could not find this particular information in the CSOB docs. Thank you for your reply even when it is more related to the CSOB gateway itself than this library.