I've created a IPN code that allows paypal to update my database for a item purchased. I am thinking ahead though that the person buying my item, might not confirm upon checkout, or that while they are confirming check out, someone else might click the buy it now button as well before it's confirmed via paypal.
The point to all this is, I want to figure out the best way to fix this. My idea is after they click the buy now button it sets the item to pending, and they have 5 minutes to confirm the purchase or it's sent back to available.
Please help.. I can provide my code if needed.
I'd suggest using something like Resque - there is a PHP port (https://github.com/chrisboulton/php-resque).
When the user goes to PayPal, set the status of the product to be 'reserved', and create a Resque job that's scheduled to run 5 minutes from now.
If the user buys the product, change the status from 'reserved' to 'sold'.
When the Resque job runs after 5 minutes, it checks whether the status is 'reserved', and if it is, it changes it back to 'available'.
If the status is 'sold', then it does nothing.
If you don't want to use Resque, then you could do something a bit more simple like a cron job that selects all products that with a status of 'reserved' where the status was set more than 5 minutes ago. Then you change that status to 'available' again. You can run that cronjob every 60 seconds.
However, this can get into complicated race conditions, and it will be less flexible, so I'd implement a queue-based system if you can.
First sorry that I cant comment, cause of my lvl here. So your idea with locking it is user friendly and 5 minutes would be okay. But I would PayPal update my stock, when the payment is finished. who pays first... But I would program a ajax script which checks the stock for the user, who has this item in the basket and when the stock changed to 0 i would do an alert message like
alert('Item Xy changed his state to 0');
Related
I am making an online shopping cart but I want to be sure that the products that you select are reserved for 3 days and when the time is up, return the total of products that the customer booked and update the status of the shopping cart, or also if it has generated A comment the admin receives a notice on your page as on facebook as when they like like one of your photos notifies you
how I can do it, I had thought of a trigger but how can I go back to the background or what is the correct way to do it?
There are about a million ways (okay, maybe tens) to solve this problem. For example, you can have a cron job that runs once an hour or whatever time period works for you. The job runs your PHP program and performs the actions that you want for out-of-date entries. Simply select the items that meet your criteria, remove them from the cart, and then notify the user in whatever way you want.
I'm developing an order system with PHP + mySQL. There's a high chance that multiple users will create new orders with same product at the same time.
This is the scenario: in MySQL DB, there's a table named "inventory", in which all products' item numbers and available quantity are listed.
Now,
Product A's inventory is 500 pcs.
User1 is working on a new order with 300 pcs of product A, but he hasn't submitted yet. However, there's an AJAX script to send a query to table "inventory" and since the inventory qty is 500 pcs, so it returns 300 pcs as available for this order.
User2 is working on another new order now, while User1 still hasn't submitted his order. User2 wants 400 pcs of product A, and since 300 pcs from User1 has't been submitted, AJAX function returns 400 pcs are available for User2's order.
Now here's the problem. Either User1 or User2 submits his order first, the another user will not have enough quantity to fulfill his order as the result returned by AJAX.
I want to know what is the best (or normally) practice to handle this type of task.
I have thought about PDO transaction handling (not sure if it will work, have never used it, I'm quite new), or maybe should I just forget about AJAX but do the inquiry when user submit the form?
Also I had thought about putting a "check" button on the page then after the user finished inputting the order, they hit "check" to send an inquiry to DB then get the most 'updated' quantity, maybe it's soon enough before the user hits submit.
Thanks.
Thwo things you have to understand first:
There is no such thing like "PDO transaction"
Transactions are inapplicable here anyway.
What you actually need is simple reservation system. Or a notification one.
In case of reservation, you may mark 300 items as "reserved". Then all others may either content with remained 200 or wait till reservation gets released or stock refunded.
While an example of notification system you may see right here on this very site: in case there is a user who is working on the answer, and question happened to be closed, an ajax notification is shown to the user, saying he has not enough items to reserve anymore.
there are 2 ways:
1) you reserve the stock as user1 puts it in their basket
(and deal with timeouts / abandoned orders and moving stock back to inventory and a way to notify the users when items come back in stock)
this means that user2 can't add them if user1 has them.
(you could physically remove the number from the inventory, or you could add another column with 'reserved' in it - e.g. 200 in stock and 300 reserved)
2) you do the stock check + reserve when the user has begun the checkout process and notify the users if the items aren't in stock and you don't have to deal with abandoned orders as they haven't reserved the stock yet.
I have developed e-commerce systems based on both, and #1 is, imho the best - as a user I want to know if the things are in stock as I add them to my basket
This isn't a matter of what you're using to make your query (though using PDO is still a good idea.) When the user finally submits their order, your php script should perform an extra check that the requested amount is in stock. If not, the user should presented with an error that the amount of product is no longer avaiable.
Use ajax to check the stock periodically so the user gets a javascript notification when something in their order is no longer available.
I'm trying to develop a payment module for one shop cms.
Payment is based on acquiring in a Bank.
So firstly for each order creates (with curl) a "ticket" on bank side. Ater that client is forwarded on payment page on bank server with its ticket id. If customer pays and push button "go back to store" everything is fine.
But if customer pays and don't push this button i need to check the result (if he paid or not). It is also made by curl. I'm requesting info by ticket id and receive an array with parameters i need among them is result code i need to check.
So i need to check it automatically (for ex. 1 time each 5 mins during an hour).
Cron can't help me coz it is all dynamically processes.
I created a function that returns me the result. But how can i start it in background with any interval?
Using cron. PHP does not offer anything like scheduler because it is out of scope of the language. You may use DB to store some of parameters needed to the whole process.
I'll extend a little bit WebnetMobile answer on using cron for what you want.
Likely you have a table in your DB where you store are tickets you get from Bank and whenever they suceed or did not suceed as well the date the ticket was created.
Then, if what you want is to check every five minutes from bank ticket creation date whetever it suceed or not if unknown, you should run a cron each minute that will do the following:
Check if there are bank tickets without defined state (Suceed or Failed).
Check if time created (without seconds) minus current time (without seconds) MOD 5 == 0.
If true, means another 5 minutes period have passed from that ticket creation and should be checked, Else do nothing.
I think this will do what you want
I have an e-commerce site developed in magento, an I am using paypal as payment method. When user adds 2 items for a product "Dress" in cart, and goes to checkout page and click place order button then I am redirecting user to paypal site for payment. But there are some users who abandons payment and leave the paypal page.
When they click on place order, magento generates an order in backend and keep its status as Pendin, once paypal payment done and user redirected back to our site, that order's status updated to Processing because payment is done. But if user leaves from pyapal payment page without making payment then my product stocks are decreased and other users can't place the order until I cancel the junk orders.
Is there any way with which I can automate this process, if user do not pay on paypal then stocks/inventory should back to normal?
Please help, thanks!
Here is the cron that we are using since past few months. This code checks for pending orders for more than 7 minutes age to 30 minutes and cancels them. For me this restores the inventory.
public function cancelPending()
{
$orderCollection = Mage::getResourceModel('sales/order_collection');
$orderCollection
->addFieldToFilter('status', 'pending')
->addFieldToFilter('created_at', array('lt' => new Zend_Db_Expr("DATE_ADD('".now()."', INTERVAL -'0:07' HOUR_MINUTE)"),
'gt' => new Zend_Db_Expr("DATE_ADD('".now()."', INTERAL -'0:30' HOUR_MINUTE)")));
foreach($orderCollection->getItems() as $order)
{
$orderModel = Mage::getModel('sales/order');
$orderModel->load($order['entity_id']);
if(!$orderModel->canCancel())
continue;
$orderModel->cancel();
$orderModel->setStatus('canceled');
$orderModel->save();
}
My default status for an order pending payment is "pending". you may have to change that.
I guess you'll need to set up a magento cron job, which takes the orders which are in pending state for more than 20 minutes, and reverting those, therefore enabling your stock again.
I tried to google it, but couldn't find anything. I'm pretty sure something like this exists
'cron' is a way to schedule things periodically (like, every 10 minutes) on UNIX systems, Magento inherited this terminology, here you can find some details: http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/how_to_setup_a_cron_job
So, algorithm plan:
for every 10 minutes
check for orders which are in Pending state and are older than (current_time -20 minutes)
revert these orders automatically
(perhaps you could send a mail to the people who have orders older than 10 minutes that heeey, don't you wanna pay?)
I'm creating a simple online shop with PHP integrated with PayPal that sells unique items. What I'm wondering is how other shops deal with multiple people attempting to go through the payment process with the same item.
This is my current draft strategy:
Items have three status types: available, on hold & sold.
As a user moves to the PayPal payment page it checks the status of all the items in the cart to ensure they're available. It also sets the item's status to "on hold" until they either come back after payment is confirmed or it times out (10 minutes? Not sure what this should be).
Is this standard practice or is there a more practical way I should be going about this?
Thanks in advance!
Have a look at Dell's UK outlet. When someone adds a system to their shopping basket it is held and not available to other customers. If it isn't purchased, the item is removed from the basket after 15mins of inactivity and is then available to other customers.
I would say the first part of your strategy is correct - as you move to the payment page, flag all the products as 'on hold'
When the user has finished the payment, you will get a postback from Paypal which lets you know if the authorisation was successful or not (and possibly also allows you to check the CSC/CVV2 result), and at that point you have the option of either accepting the payment, or rejecting it.
On receipt of the postback you should also check whether the items are still on hold. If they have timed out you can reject the payment and display a message 'sorry - timeout exceeded' or somesuch.
This method also allows you to work out an ideal timeout period if you keep track of how often customers run into the timeout, so you can extend the timeout from (eg) 5 to 10 minutes if too many are timing out, or shorten it if none are timing out.
This is a fairly common issue with fixed inventory systems such as venue, transport/airline tix etc.
I like the airline model where once you get the itinery you want and click select, you get a page with passenger info with a message saying, seats on hold and you now have xx(10/15) minutes to complete the purchase. Everything becomes explicit at that time. For other unique/one of a kind item, I'd think a message on any page, that the user clicks on, saying you have xx(mins) remaining to complete the purchase would be a big motivator for "on the edge" buyers !
woot.com is notorious for this problem but their solution works well. After payment information is verified the user is brought to a page with a small amount of text saying something like "your order is in, we are verifying inventory".
It looks like the paypal API has a "RefundTransaction" message so something like that might not be possible. But the user experience could be awkward if your going to the paypal website then coming back to your site.
This is much like booking theatre tickets or similar online and yes the way you describe is generally the way it works. At some point the item is "reserved" in the system and either the customer completes the transaction or the item is released after some time for others to buy.
Of course at what point you reserve the item (when it's added to the cart, at the point you send them off to pay etc) is up to you. I would expect putting it in the cart would be the best choice as it makes it less likely someone will build up a basket of stuff only to find half of it is no longer available at the checkout.