简体   繁体   中英

How to parse value from php script to Opencart Module?

I've implemented payment module in Opencart based on BankTransfer module.

Algorithm:

  1. I get values orderId, merchantID, amount, currency. I encrypt them.

  2. I send those values to payment gateway with "GET" response.

  3. Payment gateway then encrypts the values, processes the payment, and encrypts transaction status with its own signature, and sends that to me.

  4. My system using php script decrypt the message, and find the transaction status.

QUESTION: How can i parse the values from php script to Opencart module (banktransfer.php)? Specifically, to public function confirm() ?

Approach: I've naively thought that http://example.com/opencart/index.php?route=checkout/success is the success link. I thought if i redirect to this page from php script, order would be confirmed, it's not.

Explanation of files:

  • banktransfer.php is file opencart gets information from system such order id, amount of the order and etc. OpenCart module.
  • testkzm.php is a simple php script that decrypts the message, and "tries" to send get paramaters to confirm function in opencart module in banktransfer.

banktransfer.php in controller/payment

 class ControllerPaymentBankTransfer extends Controller {
  protected function index() {
   $this->language->load('payment/bank_transfer');

   $this->data['text_instruction'] = $this->language->get('text_instruction');
   $this->data['text_description'] = $this->language->get('text_description');
   $this->data['text_payment'] = $this->language->get('text_payment');

   //Modified things for KZM
   $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
   $this->data['orderIdKZM'] = $this->session->data['order_id'];
   $this->data['amountKZM'] = $order_info['total'];

   $this->data['merchantIdKZM'] = $this->language->get('merchantIdKZM');
   $this->data['currencyKZM'] = $this->language->get('currencyKZM');
   $this->data['titleKZM'] = $this->language->get('titleKZM');
   $this->data['successuUlKZM'] = $this->language->get('successUrlKZM');
   $this->data['errorUrlKZM'] = $this->language->get('errorUrlKZM');
   $this->data['dateKZM'] = $this->language->get('dateKZM');
   $this->data['signstrKZM'] = $this->language->get('signstrKZM');
   $this->data['verKZM'] = $this->language->get('verKZM');
   //KZM

   $this->data['button_confirm'] = $this->language->get('button_confirm');

   $this->data['bank'] = nl2br($this->config->get('bank_transfer_bank_' . $this->config->get('config_language_id')));

   $this->data['continue'] = $this->url->link('checkout/success');

   if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/bank_transfer.tpl')) {
    $this->template = $this->config->get('config_template') . '/template/payment/bank_transfer.tpl';
   } else {
    $this->template = 'default/template/payment/bank_transfer.tpl';
   } 

   $this->render(); 
  }

  public function confirm() {
   $this->language->load('payment/bank_transfer');

   $this->load->model('checkout/order');

   $comment  = $this->language->get('text_instruction') . "\n\n";
   $comment .= $this->config->get('bank_transfer_bank_' . $this->config->get('config_language_id')) . "\n\n";
   $comment .= $this->language->get('text_payment');

   $this->model_checkout_order->confirm($this->session->data['order_id'], $this->config->get('bank_transfer_order_status_id'), $comment, true);
  }
 }

testkzm.php

$message <?php echo $_GET["message"]; ?><br>
$transactionStatus= decrypt($message);

<h2><?php echo $transactionStatus; ?></h2>

<div class="buttons">
  <div class="right">
        <form action="http://www.example.com/index.php?route=payment/bank_transfer/confirm" method="get">
            <input type="hidden" name="transactionStatus" value="<?php echo $transactionStatus; ?>">
            <input type="submit" value="<?php echo $button_confirm; ?>" id="button-confirm" class="button" />

        </form>
    </div>
</div>

Example of how Opencart confirms order, but it is inside the module.

banktransfer.tpl

    <h2><?php echo $text_instruction; ?></h2>
<div class="content">
  <p><?php echo $text_description; ?></p>
  <p><?php echo $bank_transfer; ?></p>
  <p><?php echo $text_payment; ?></p>
</div>
<div class="buttons">
  <div class="right">
    <input type="button" value="<?php echo $button_confirm; ?>" id="button-confirm" class="button" />
  </div>
</div>
<script type="text/javascript"><!--
$('#button-confirm').bind('click', function() {
    $.ajax({ 
        type: 'get',
        url: 'index.php?route=payment/bank_transfer/confirm',
        success: function() {
            location = '<?php echo $continue; ?>';
        }       
    });
});
//--></script> 

I will try to explain how the order is being created and confirmed in OpenCart, so You could adjust Your payment method:

  1. Cart is open, user clicks on Checkout link/button.
  2. User logs in (if he is not), registers or starts a guest checkout.
  3. Fills in or confirms the payment and shipping address.
  4. Chooses the shipping method.
  5. Chooses the payment method.
  6. On confirm page, actually an order is created and stored into a DB.
  7. After clicking on Confirm order a payment method is invoked ( index action).
  8. Usually using an AJAX a submit to a send action is done that sends data/request to a Payment service - after a response is retrieved and the payment was processed then the order is confirmed .

You should accomodate to this workflow when creating a payment method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM