Jump to content

pass order reference to the payment gateway


pg_dev

Recommended Posts

Hi, I'm working on external type payment module where i need to pass the order reference (that we get on successful order validation & order confirmation page) to payment gateway via POST method. I need order reference to be passed in ' $merchant_ref_number ' but the redirection to payment gateway doesn't work the moment when I use $merchant_ref_number $order_id-> reference;  instead of '$merchant_ref_number = trim( (string) $order_id ); '   becoz $merchant_ref_number is one of the parameters passed in request url to gateway and its expected to pass order_id (i.e need number not characters as we get for 9 digit reference) inorder to successfully redirect to gateway.  I checked in chrome inspect network tab, the 'merchant_ref_number' field remains empty while sending payment request url.image.png.5f8dac157e5544c74e97fc43c34d72d8.png

 Check My code below: 

 $merchant_ref_number = $order_id->reference;



and I'm getting error as below: 
 

log error: Exception on hook paymentOptions for module . Notice: Trying to get property \'reference\' of non-object


AND IF I use this :

$merchant_ref_number = $order->reference;

I get log error as this

Exception on hook paymentOptions for module . Notice: Undefined variable: order



Can anyone suggest me what's the right way to pass order reference so that it can be stored in gateway side? Please!

Edited by pg_dev (see edit history)
Link to comment
Share on other sites

At this moment, the order is still not yet created, that’s why it’s empty.

You have only a cart instance at this time.

When the PaymentModule::validateOrder will be called, one or more order(s) will be created from the cart.

Then you can retrieve the order reference cause now it will exist.

So maybe you could send the cart id instead ?

$merchant_ref_number = $params["cart"]->id; // Get Cart from hooks $params argument
 

Maybe this can interest you https://github.com/PrestaShop/PrestaShop/issues/21995

Link to comment
Share on other sites

@JanettThanks for the reply. I was thinking the same, that order is still not created that is why it's empty. Order reference comes after the validation is done. The code you sent it throws error 

Exception on hook paymentOptions for module . Notice: Undefined variable: params

And the requirement is different here. The gateway team already gets the order id but additionally they have asked for the order reference too. So i need to find a way to capture that order reference just after the validation is done and then pass it to gateway.

I'm thinking of this possible scenarios:
 -> I can just pass '$merchant_ref_number = trim( (string) $order_id );  as it was before becoz it successfully redirects to payment page and then i can capture the order reference after the validation and send to the gateway.  can it be achieved? has anyone done this before? Please help.

If you can help me with this, it would be really great. 🙂

Edited by pg_dev (see edit history)
Link to comment
Share on other sites

1 hour ago, pg_dev said:

The code you sent it throws error 

Exception on hook paymentOptions for module . Notice: Undefined variable: params

Change the method definition:

public function hookPaymentOptions(array $params)

 

Link to comment
Share on other sites

1 hour ago, pg_dev said:

I'm thinking of this possible scenarios:
 -> I can just pass '$merchant_ref_number = trim( (string) $order_id );  as it was before becoz it successfully redirects to payment page and then i can capture the order reference after the validation and send to the gateway.  can it be achieved? has anyone done this before? Please help.

Yeah it should be possible, what is the payment gateway and where can I found the documentation ?

Some gateway auto capture the order and in this case we often cannot update the order anymore.

But if you do the capture manually, you should be able to send a PATCH request before sending the capture request, this should be checked from documentation of the gateway.

Link to comment
Share on other sites

If i understand your question correctly you want to send the reference on order completion correct?

In this case you need to use the DisplayOrderConfirmation hook, for example like we use it in one of our projects:

    public function hookDisplayOrderConfirmation($params) {
        $order = $params['order'];
        $products = $params['order']->getProducts();
        $customer = new Customer((int)$order->id_customer);
        $delivery = new Address((int) $order->id_address_delivery);
        $country = new Country($delivery->id_country);

        require_once($this->getLocalPath() .'crezzurApi.php');
        $api = new CrezzurApi();
        $order_exist = $api->findOrder($order->reference);

        if (!$order_exist) {
            $api->insertOrder($order->reference); // PREVENT MULTIPLE INSERTS

            $xml_customer = "<?xml version='1.0' standalone='yes'?>
                <customer>
                    <email>$customer->email</email>
                    <bedrijfsnaam>$delivery->company</bedrijfsnaam>
                    <btwnummer>$delivery->vat_number</btwnummer>
                    <aanspreek></aanspreek>
                    <name>$delivery->lastname</name>
                    <firstname>$customer->firstname</firstname>
                    <middlename/>
                    <phone>$delivery->phone</phone>
                    <gsm>$delivery->phone_mobile</gsm>
                    <adres>
                        <straat>$delivery->address1</straat>
                        <huisnummer/>
                        <toevoeging/>
                        <land>$country->iso_code</land>
                        <postcode>$delivery->postcode</postcode>
                        <stad>$delivery->city</stad>
                    </adres>
                </customer>";
            $apiuser = $api->createCustomer($xml_customer);
            $xml_order = "<?xml version='1.0' standalone='yes'?>
            <order>
                <customer_id>". $apiuser['id'] ."</customer_id>
                <order_id>Webshop: $order->reference</order_id>
                <delivery_date></delivery_date>
                <base_subtotal>". Tools::ps_round($order->total_paid, 2) ."</base_subtotal>
                <base_shipping_amount>". Tools::ps_round($order->total_shipping_tax_incl, 2) ."</base_shipping_amount>
                <vat_regime>BI</vat_regime>
                <notes>Bestelling geplaatst via webshop.</notes>
                <payment_method>". Tools::substr($order->payment, 0, 32) ."</payment_method>
                <shipping_method></shipping_method>
                <articles>";
                foreach ($products as $key => $product) {
                    $xml_order .= "<article>
                        <sku>". $product['product_ean13'] ."</sku>
                        <count>". $product['product_quantity'] ."</count>
                        <price_ex>". Tools::ps_round($product['product_price'], 2) ."</price_ex>
                        <price>". Tools::ps_round($product['product_price_wt'], 2) ."</price>
                        <articlename/>
                        </article>";
                }
            $xml_order .= "</articles></order>";
            $apiorder = $api->createOrder($xml_order);
            echo '<pre class="prettyprint linenums">Klant aangemaakt met ID '. $apiuser['id'] .' en order ID '. $apiorder['id'] .'</pre>';
        }

 

Link to comment
Share on other sites

On 6/14/2023 at 1:44 PM, Janett said:

Yeah it should be possible, what is the payment gateway and where can I found the documentation ?

Some gateway auto capture the order and in this case we often cannot update the order anymore.

But if you do the capture manually, you should be able to send a PATCH request before sending the capture request, this should be checked from documentation of the gateway.

Hey. It's not popular gateway still you can find docs here https://speca.io/latpay/latpay-api#hosted-payment-wallet_group . I'm newbie in presta so I'm not able to figure out how should i proceed further. If you can help me with what code i need to add further, it would be great.  For more reference, you can check code I posted @Crezzur.com comment. 

Link to comment
Share on other sites

Hi @Crezzur.com, I do need to send order reference after validation. But Priority is I should pass the order reference in merchant_ref_number after the order if not possible before order. I tried your approach but still not sending any reference in gateway DB.  Pls have a look at my codes :

 

  public function hookDisplayOrderConfirmation($params)

 {

    // Retrieve the order ID

    $orderId = $params['order']->id;



    // Load the order object

    $order = new Order($orderId);



    // Retrieve the order reference

    $orderReference = $order->reference;



    // Pass the order reference to the gateway using the appropriate method

    $this->latpayInput($orderReference); // Replace processPayment() with the method that communicates with your payment gateway



    // Return any additional content or modify the display as needed

   // return 'Payment processed successfully.';



  }



  public function hookPaymentOptions($params)

 {

    // Check if this module is enabled

    if (!$this->active) {

        return;

    }

   

    $action = 'https://lateralpayments.com/hps/hps_Payment.aspx';

    $inputs = $this->latpayInput();



    // Set the payment option details

    $externalOption = new PaymentOption();

    $externalOption->setCallToActionText($this->l('Pay with Latpay'))

        ->setAction($action)

        ->setInputs($inputs)

        ->setAdditionalInformation($this->context->smarty->fetch('module:latpayredirect/views/templates/front/payment_infos.tpl'));

       

    return [$externalOption];

   

  }



  protected function latpayInput()

  {

    global $smarty, $cart;



    $Merchant_User_Id = Configuration::get('latpayredirect_Merchant_User_Id');

    $merchantpwd = Configuration::get('latpayredirect_merchantpwd');

    $Purchase_summary = Configuration::get('latpayredirect_Purchase_summary');

    $SecretKey = Configuration::get('latpayredirect_salt');

    $processurl = Configuration::get('latpayredirect_process_url');

    $order_id = (int) $this->context->cart->id;

    $merchant_ref_number = trim( (string) $order_id );

    $currencydesc = $this->context->currency->iso_code;

    $customer_ipaddress = 237429118393;

    $amount = $cart->getOrderTotal();

    $customer = new Customer($cart->id_customer);

    $delivery = new Address($this->context->cart->id_address_delivery);

    $firstName = $customer->firstname;

    $lastName = $customer->lastname;

    $zipcode = $delivery->postcode;

    $email = $customer->email;

    $phone = $delivery->phone;

    $city = $delivery->city;

    $transactionkey = sha1($currencydesc.$amount.$merchant_ref_number.$SecretKey);

    $notifyurl = $this->context->link->getModuleLink($this->name, 'validation', array(), true);

    $process_url = $this->context->link->getModuleLink($this->name, 'validation', array(), true);

    $cancelurl = $this->context->link->getPageLink('order', true, null, array( 'step' => 3 ));

   

    //Merchant Data

    $merchant_data = array();

    $merchant_data['Merchant_User_Id']      = $Merchant_User_Id;

    $merchant_data['merchantpwd']         = $merchantpwd;

    $merchant_data['Purchase_summary']         = $Purchase_summary;

    $merchant_data['currencydesc']           = $currencydesc;

    $merchant_data['merchant_ref_number']     = $merchant_ref_number;      

    $merchant_data['customer_ipaddress']       = $customer_ipaddress;

    $merchant_data['amount']         = $amount;

    $merchant_data['customer_firstname']     = $firstName;

    $merchant_data['customer_lastname']  = $lastName;    

    $merchant_data['customer_phone']     = $phone;

    $merchant_data['customer_email']    = $email;

    $merchant_data['bill_firstname']      = $delivery->firstname;

    $merchant_data['bill_lastname']  = $delivery->lastname;

    $merchant_data['bill_address1']      = $delivery->address1;

    $merchant_data['bill_address2']    = $delivery->address2;

    $merchant_data['bill_city']      = $city;

    $merchant_data['bill_state']  = State::getNameById($delivery->id_state);

    $merchant_data['bill_country']      = Country::getIsoById($delivery->id_country);

    $merchant_data['bill_zip']    = $zipcode;

    $merchant_data['ship_to_address1']    = $delivery->address1;

    $merchant_data['ship_to_address2'] = $delivery->address2;

    $merchant_data['ship_to_city']    = $city;

    $merchant_data['ship_to_country']   = Country::getIsoById($delivery->id_country);

    $merchant_data['ship_to_phone']     = $phone;

    $merchant_data['ship_to_state'] = State::getNameById($delivery->id_state);

    $merchant_data['ship_to_zip']     = $zipcode;

    $merchant_data['transactionkey']  = $transactionkey;

    $merchant_data['processurl']  = $process_url;

    $merchant_data['notifyurl']  = $notifyurl;

    $merchant_data['cancelurl']  = $cancelurl;

     

    $inputs = array();

    foreach ($merchant_data as $k => $v)

    {

      $inputs[$k] = array(

        'name' => $k,

        'type' => 'hidden',

        'value' => $v,            

      );  

    }



    return $inputs;

  }





  public function hookPaymentReturn($params)

  {

      if (isset($params['order']) && $params['order'] instanceof Order) {

          $order = $params['order'];

         

          $this->context->smarty->assign(array(

              'msg' => $this->l('Thank you for the purchase. Your order is confirmed.'),

              'orderId' => $order->id,

             

              // Add more order information as needed

          ));

      }



      return $this->fetch('module:latpayredirect/views/templates/hook/payment_return.tpl');

  }

 

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...