-1

I have a situation for shop market, when user wants to pay, first a factor will create then redirect him to the payment gateway.

In factor creation, number of items that user is selected, reduce from stock number count of existence items, so if user close the payment gateway browser's tab, wanted items are reduced from stock items count and never returns.

How manage this situation for payment??

My Solution

I think a lot on that and come with this solution that create a reserved factors table to store current factor that is going to pay and when back from payment gateway, simply delete it.

If browser's tab was close by user then calculate that if time of reserved factor is more than payment gateway the delete it from reserved table and add reduced items number to stock number count.

I add this code to my construct (because I think this is right place to check for all items in reserved factor right before show items to user. This helps for items that are not available, to be available now) but in another hand I think if number of reserved factors in database are big enough, it might have a huge effect of loading performance.

SO what is the right solution for situations like this?

Can I have something like a schedule plan in MySQL to delete those records? or even in PHP?

OR ANY IDEA...

EDIT:

I do want a code base solution if any exists.

MMDM
  • 465
  • 3
  • 11
  • Possible duplicate of [PHP: running scheduled jobs (cron jobs)](https://stackoverflow.com/questions/120228/php-running-scheduled-jobs-cron-jobs) – Brilliand Oct 17 '19 at 00:04

2 Answers2

1

Can I have something like a schedule plan in MySQL to delete those records? or even in PHP?

Usually this sort of thing is done using your server's "cron" feature (or "Scheduled Tasks" if your server is running Windows). You can write a PHP script that clears these abandoned carts when run, and configure your server to execute that PHP script at regular intervals.

MySQL also has an "Event Scheduler" feature; I don't think that gets used very often, but it is an option.

Brilliand
  • 13,404
  • 6
  • 46
  • 58
  • I have not access to server and i think it's Linux. What I have in mind was something that can handle with code – MMDM Oct 17 '19 at 04:18
  • @MMDM Then see the question I called this a duplicate of, it appears to have some PHP-only answers. – Brilliand Oct 17 '19 at 08:58
  • I saw that and my question is about how handle such situations in payment, and not just schedule a task – MMDM Oct 17 '19 at 14:43
0

...so if user close the payment gateway browser's tab, wanted items are reduced from stock items count and never returns.

This is the wrong way to look at this problem. Consider these scenarios:

  • User accidentally closes their browser, re-opens it, and tries to finish purchasing
  • User loses internet access and cannot finish purchasing... browser isn't closed

While it is possible to use the Beacon API in newer browsers to send an update when a browser is closed, for these reasons it's a bad idea.

You have two general options:

Option 1: Track the user/cart activity

Every time the user does something meaningful on the site, update their last active time on a record for their cart. If a user is still browsing the site and they have a product in their cart, keep it reserved there until the sale is complete or until they are no longer active for some period of time. For example, if they haven't been on the page in 15 minutes, release the reservation for the product. (Be prepared to reserve it again if they come back and the product is still available.)

Option 2: Don't reserve until purchase

Keep the item in-stock until it's actually bought. If at the time of purchase the product is no longer available, let the user know.

Which of these options you choose depends on your business conditions and the sort of stuff you're selling.

Brad
  • 159,648
  • 54
  • 349
  • 530