Jump to content

How to load order data and set order as paid in PHP script that is loaded by AJAX?


Recommended Posts

I am creating payment module for PrestaShop. I have a class in "controllers/front/payment.php":

class paymentModuleFrontController extends ModuleFrontController
{
    public function initContent()
    {
        $this->display_column_left = false;
        parent::initContent();
        $control=(int)Tools::getValue('control');
        $cart = $this->context->cart;
        if (!empty($control))
            $cart = new Cart($control);
        if ($cart->id_customer == 0 || $cart->id_address_delivery == 0 || $cart->id_address_invoice == 0 || !$this->module->active)
                Tools::redirect('index.php?controller=order&step=1');
        $customer = new Customer($cart->id_customer);
        if (!Validate::isLoadedObject($customer))
                Tools::redirect('index.php?controller=order&step=1');
        $address = new Address($cart->id_address_invoice);
        $params = null;
        $template = "payment_return";
        if ($cart->OrderExists() == true)
            Tools::redirect('index.php?controller=order-confirmation&id_cart='.$cart->id.'&id_module='.$this->module->id.'&id_order='.Order::getOrderByCartId($cart->id).'&key='.$customer->secure_key);
        elseif (Tools::getValue("status") == "OK")
            // (...)
        {
            // (...)
            $template = "payment";
            $form_url = "";
            $currency = Currency::getCurrency($cart->id_currency);
            if (Configuration::get('DP_TEST')==1) $form_url.="test_payment/";
            $params = array(
                    'id' => Configuration::get('DP_ID'),
                    'amount' => (float)$cart->getOrderTotal(true, Cart::BOTH),
                    'currency' => $currency["iso_code"],
                    'description' => Configuration::get('PS_SHOP_NAME'),
                    //(...)
                    'type' => 0,
                    // (...)
                    'control' => $cart->id,
                    'firstname' => $customer->firstname,
                    'lastname' => $customer->lastname,
                    'email' => $customer->email,
                    'street' => $address->address1,
                    'city' => $address->city,
                    'postcode'=> $address->postcode,
                    'api_version' => 'legacy'
            );
            $chk = $params['id'].$params['amount'].$params['currency'].$params['description'].$params['control'].Configuration::get('DP_PIN');
            $chk = rawurlencode($chk);
            if(Configuration::get('DP_CHK'))
                $params['chk']=hash('md5', $chk);
        }
        $this->context->smarty->assign(array(
            'params' => $params,
            'module_dir' => $this->module->getPathUri(),
            'form_url' => $form_url,
            ));
        $this->setTemplate($template.".tpl");
    }
}

I have created JavaScript script, which send AJAX request with "{$cart_id}", in template 'views/templates/front/payment.tpl''. The module creates a type of payment and runs JavaScript script after choosing this type of payment during making an order.

I would like to create PHP script that process AJAX request, loads information about the order and executes it. I created script "payment.php":

require_once(dirname(__FILE__).'../../../config/config.inc.php');
//require_once(dirname(__FILE__).'../../../init.php'); // I have problem with flushing output when I am including this script
require "processing.inc.php";
$myShop->paymentInitiation();

and "processing.inc.php":

//require ...
//$myShop = new ...
$myShop->user(function ($requestData, $cb)
{
	$cb($requestData->user);
}
)->products(function ($requestData, $cb)
{
	$cb(array(18, 30));
}
)->product(function ($requestData, $itemId, $cb)
{
	if ($itemId == 18) 
		   {
		$cb(array(
				          "item_id"=> "18",
// (...)
		));
	}
	else if ($itemId == 30)
		   {
		$cb(array(
				             "item_id"=>  "30",
// (...)
		));
	}
}
// (...)
)->productDelivery(function ($requestData, $items, $cb) 
{
	$cb(array(
		        "delivery_id"=> "",
// (...)
	));
})->currency(function ($requestData, $cb)
{
	$cb('PLN');
}
)->paymentTitle(function ($requestData, $items, $delivery, $cb) {
	$cb('Tytuł płatności');
}
// (...)
)->merchant(function ($requestData, $items, $delivery, $cb) {
	$cb(array("company" => array("name" => "Default", "NIP" => "", "REGON" => "", "KRS" => ""), "person" => array("first_name" => "Default", "last_name" => "", "PESEL" => ""), "address" => array("street" => "Default", "city" => "Default", "zip" => "00-000", "country" => ""), "fax" => "", "www" => "", "email" => ""));
}
)->documentParse(function ($requestData, $document, $cb) {
$cb(false);
}
)->onFinishedPayment(function ($items, $delivery_option, $document, $requestData) {

}
);

How to load order data in PHP script "processing.inc.php", which is run by AJAX, having "cart_id"? How to set order as paid in functions that is argument of my method "onFinishedPayment"? 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...