Jump to content

Recommended Posts

I am creating PrestaShop payment module, on whose payment page there is JavaScript script, which sends payment request via AJAX to the PHP script inside the module. PHP script change the cart into the order after successful payment. Changing cart into order looks as follows:

if ($cart->OrderExists() == false) {
        $module->validateOrder($cart->id, $actual_state, $total, 'billon', NULL, array(), (int)$cart->id_currency, false, $customer->secure_key);
    } else {
	      $history = new OrderHistory();
	      $history->id_order = Order::getOrderByCartId((int)Tools::getValue('control'));
	      $lastOrderState = OrderHistory::getLastOrderState($history->id_order);
	      $history->changeIdOrderState($actual_state, $history->id_order);
	      $history->addWithemail(true);
  	}

How is the best way to lock the cart before the payment and unlock it if payment failed? Should I create order from the cart with another status:

if ($cart->OrderExists() == false) {
        $module->validateOrder($cart->id, 1, $total, 'billon', NULL, array(), (int)$cart->id_currency, false, $customer->secure_key);
    }

Which status should I set? How to get identifier of status created by module method "addNewOrderState"? How to restore the cart (from order) then?

 

Should I try to clone the object of cart, call the method "delete" of cart, and call the method of payment "save" of the cloned object in case of a failure of the payment:

$cart     = new Cart((int) 0);
$tempCart = $cart;
$myShop->paymentStatus($taskId ? $taskId : $_GET['taskId'], function($response) use (&$cart, &$tempCart)
{
    $responseString = json_encode($response);
    if (is_string($response['status']))
            {
                $status = explode('_', $response['status']);
                if (count($status) > 1 && $status[0] == 'FINISHED')
                {
                    if ($status[1] != 'OK')
                    {
                        if (isset($tempCart))
                        {
                            $tempCart->save();
                        }
                    } 
                }
                else
                {
                    if (isset($cart))
                        {
                            $tempCart = clone $cart;
                            $cart->delete();
                        }
                }
            }
    echo $responseString;

Is it better is to create your own ObjectModel class and table to store the user and order idetifiers? Is there another way? Could you help me?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...