User Tools

Site Tools


how_to_create_a_payment_module

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
how_to_create_a_payment_module [2019/07/30 23:37]
A User Not Logged in [Callbacks]
how_to_create_a_payment_module [2020/05/01 15:59]
admin [Return URLs]
Line 6: Line 6:
  
 <code php /includes/modules/payment/pm_mymodule.inc.php> <code php /includes/modules/payment/pm_mymodule.inc.php>
-class pm_mymodule {+class pm_module {
   public $id = __CLASS__;   public $id = __CLASS__;
-  public $name = 'My Module';+  public $name = 'My Vendor Module';
   public $description = 'Lorem ipsum dolor';   public $description = 'Lorem ipsum dolor';
   public $author = 'ACME Corp.';   public $author = 'ACME Corp.';
Line 21: Line 21:
         array(         array(
           'id' => 'method1',           'id' => 'method1',
-          'icon' => 'images/payment/mymodule-method1.png',+          'icon' => $this->settings['icon'],
           'name' => 'Method 1',           'name' => 'Method 1',
           'description' => 'Select this option for method 1.',           'description' => 'Select this option for method 1.',
Line 59: Line 59:
       array(       array(
         'key' => 'icon',         'key' => 'icon',
-        'default_value' => 'images/payment/'.__CLASS__.'.png',+        'default_value' => 'images/payment/vendor.png',
         'title' => 'Icon',         'title' => 'Icon',
         'description' => 'Web path of the icon to be displayed.',         'description' => 'Web path of the icon to be displayed.',
Line 172: Line 172:
       'action' => 'http://www.paymentgateway.com/form_process.ext',       'action' => 'http://www.paymentgateway.com/form_process.ext',
       'fields' => array(       'fields' => array(
-                    'foo' => 'bar', +        'foo' => 'bar', 
-                    'this' => 'that', +        'this' => 'that', 
-                  ),+      ),
     );     );
   }   }
Line 222: Line 222:
 </code> </code>
  
-==== Using Your Own M2M Transaction ====+==== Using Machine-to-Machine Transactions ==== 
 + 
 +This is an example when we send a JSON request to another machine and return a gateway window.
  
 <code php> <code php>
   public function transfer($order) {   public function transfer($order) {
-     + 
-    ... +    $request = array( 
-     +      ... 
-  // Do whatever +    ); 
-    $client = new someclientyoucreated(); + 
-    $result = $client->methodyoucreated($mydata); +    $headers = array( 
-     +      'Content-Type' => 'application/json; charset=utf-8', 
-    if (empty($result) || !empty($result['error'])) { +    ); 
-      return array('error' => 'There was an error: '$result['error']);+ 
 +    try { 
 + 
 +      $client = new wrap_http(); 
 +      $response = $client->call('POST', 'https://...', json_encode($request, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), $headers); 
 +       
 +      // See the ~/logs/ folder for request logs 
 + 
 +      if (!$result = json_decode($response, true)) { 
 +        throw new Exception('Invalid JSON response'); 
 +      } 
 +       
 +      ... 
 +       
 +      return array( 
 +        'method' => 'get'
 +        'action' => $result['someGatewayUrl']
 +      ); 
 + 
 +    } catch (Exception $e) { 
 +      return array('error' => $e->getMessage();
     }     }
-     
-  // Since we don't need to send the user anywhere we simply don't return anything 
   }   }
 </code> </code>
Line 254: Line 274:
  
 <code php> <code php>
-  $item_no = 0; 
   foreach ($order->data['items'] as $item) {   foreach ($order->data['items'] as $item) {
-    $fields['item_name_'.$item_no= $item['name']; +    $fields['items'][] = array( 
-    $fields['item_number_'.$item_no] = $item['product_id'. (!empty($item['option_id']) ? ':'.$item['product_id': ''); +      'name' =$item['name']
-    $fields['quantity_'.$item_no] = $item['quantity']; +      'sku=> $item['sku'], 
-    $fields['amount_'.$item_no] = currency::format_raw($item['price'], $order->data['currency_code'], $order->data['currency_value']); +      'quantity' =$item['quantity'], 
-    $fields['tax_'.$item_no] = currency::format_raw($item['tax'], $order->data['currency_code'], $order->data['currency_value']); +      'amount' =currency::format_raw($item['price'], $order->data['currency_code'], $order->data['currency_value']), 
-    $item_no++;+      'tax' =currency::format_raw($item['tax'], $order->data['currency_code'], $order->data['currency_value']), 
 +    );
   }   }
      
-  $item_no = 0; 
   foreach ($order->data['order_total'] as $row) {   foreach ($order->data['order_total'] as $row) {
     if (empty($row['calculate'])) continue;     if (empty($row['calculate'])) continue;
-    $fields['item_name_'.$item_no] = $row['title']; +    $fields['items'][] = array( 
-    $fields['item_number_'.$item_no] = $row['module_id']; +      'name' => $row['title'], 
-    $fields['quantity_'.$item_no] = '1'; +      'sku' =$row['module_id'], 
-    $fields['amount_'.$item_no] = currency::format_raw($row['value'], $order->data['currency_code'], $order->data['currency_value']); +      'quantity' ='1', 
-    $fields['tax_'.$item_no] = currency::format_raw($row['tax'], $order->data['currency_code'], $order->data['currency_value']); +      'amount' =currency::format_raw($row['value'], $order->data['currency_code'], $order->data['currency_value']), 
-    $item_no++;+      'tax' =currency::format_raw($row['tax'], $order->data['currency_code'], $order->data['currency_value']), 
 +    );
   }   }
 </code> </code>
Line 301: Line 321:
     try {     try {
          
-      //$order->save(); // Save session order to database before transaction creates an $order->data['id']. Not recommended+      $order->save(); // Save order to database before the transaction. Creates $order->data['id']
          
       $fields = array(       $fields = array(
         ...         ...
-        'cancel_url' => document::ilink('checkout'), +        'cancel_url' => (string)document::ilink('checkout'), 
-        'success_url' => document::ilink('order_process'), +        'success_url' => (string)document::ilink('order_process'), 
-        'callback_url' => document::link(WS_DIR_EXT . 'payment_service_provider/my_external_callback_file.php', array('order_uid' => $order->data['uid'])), // An order always have a uid even though it is not saved+        'callback_url' => (string)document::link(WS_DIR_APP . 'ext/vendorname/callbackfile.php', array('order_id' => $order->data['id'])),
       );       );
              
Line 367: Line 387:
 ===== settings() ===== ===== settings() =====
  
-This method sets up the payment module with a settings structure. The return is an array of the structure.+This method sets up the payment module with a settings structure. The return is an array of the structure.  The functions supported are listed in func_form.inc.php form_draw_function().
  
 <code php> <code php>
Line 439: Line 459:
 <code php /ext/provider/callback.php> <code php /ext/provider/callback.php>
 <?php <?php
-  define('REQUIRE_POST_TOKEN', false); // Allow unsigned external incoming POST data 
-  require_once('../../includes/app_header.inc.php'); 
-   
-// Make sure callback comes from a trusted IP address 
-  if (!in_array($_SERVER['REMOTE_ADDR'], array('123.456.789.0', '123.456.789.1', '123.456.789.2'))) { 
-    error_log('Unauthorized access by '. $_SERVER['REMOTE_ADDR'] .' to file '. __FILE__); 
-    http_response_code(403); 
-    die('Access Denied'); 
-  } 
-   
-// Make sure we have a UID 
-  if (empty($_GET['order_uid'])) { 
-    http_response_code(400); 
-    die('Bad Request'); 
-  } 
  
-// Get Order From Database (In this example using UID) +  try { 
-// To be able to find the order in the database, the order must previously have been saved. +    define('REQUIRE_POST_TOKEN', false)// Allow unsigned external incoming POST data 
-  $orders_query = database::query( +    require_once('../../includes/app_header.inc.php'); 
-    "select id from "DB_TABLE_ORDERS .+     
-    where uid = '"database::input($_GET['order_uid']."' +  // Make sure callback comes from a trusted IP address 
-    limit 1;" +    if (!in_array($_SERVER['REMOTE_ADDR'], array('123.456.789.0', '123.456.789.1', '123.456.789.2'))) { 
-  ); +      error_log('Unauthorized access by '$_SERVER['REMOTE_ADDR'.' to file '. __FILE__); 
-   +      throw new Exception('Access Denied', 403); 
-  if (!$order = database::fetch($orders_query)) { +    } 
-    http_response_code(404); +     
-    die('File Not Found'); +  // Make sure we have a ID 
-  } +    if (empty($_GET['order_id'])) { 
-   +      throw new Exception('Bad Request', 400); 
-// Initiate $order as the order object +    }
-  //$order = new ctrl_order('load'$order['id']); // LiteCart 1.0 +
-  $order = new ctrl_order($order['id']); // LiteCart 2.0 +
-   +
-// Get the order's payment option +
-  list($module_id, $option_id) = explode(':', $order->data['payment_option']['id']);+
  
-// Pass the call to the payment module's method callback() +  // Get Order From Database (In this example using ID) 
-  $payment = new mod_payment(); +  // To be able to find the order in the database, the order must previously have been saved. 
-  $result = $payment->run('callback', $module_id, $order);+    $orders_query = database::query( 
 +      "select id from ". DB_TABLE_ORDERS ." 
 +      where id = '". database::input($_GET['order_id']."' 
 +      limit 1;" 
 +    ); 
 +     
 +    if (!$order database::fetch($orders_query)) { 
 +      throw new Exception('File Not Found', 404); 
 +    } 
 +     
 +  // Initiate $order as the order object 
 +    //$order = new ctrl_order('load', $order['id']); // LiteCart 1.0 
 +    //$order = new ctrl_order($order['id']); // LiteCart 2.0 
 +    $order = new ent_order($order['id']); // LiteCart 2.2
  
-  // The rest is handled inside the payment module +  // Get the order's payment option 
-  // Define the funtion by: public function callback($order) {}+    list($module_id, $option_id) = explode(':', $order->data['payment_option']['id']); 
 + 
 +  // Pass the call to the payment module's method callback() 
 +    $payment = new mod_payment(); 
 +    $result = $payment->run('callback', $module_id, $order); 
 + 
 +    // The rest is handled inside the payment module 
 +    // Define the funtion by: public function callback($order) {
 + 
 +  } catch (Exception $e) { 
 +    http_response_code($e->getCode()); 
 +    die($e->getMessage()); 
 +  }
  
 </code> </code>
  
 We suggest storing the transaction details in the callback and verify them later in the method verify(). We suggest storing the transaction details in the callback and verify them later in the method verify().
how_to_create_a_payment_module.txt · Last modified: 2020/08/13 00:27 by A User Not Logged in