2014-04-22 161 views
2

我想爲我的商家帳戶製作一個自定義網關插件,但我被卡住了,我不知道如何繼續。WooCommerce自定義支付網關

現在,這裏是我迄今所做的:

<?php 
/* 
    Plugin Name: Paysecure.ro Payment Gateway 
    Plugin URI: http://www.Paysecure.ro 
    Description: Allows you to use Paysecure.ro Payment Gateway with the WooCommerce plugin. 
    Version: 0.0.1 
    Author: Andrei Raileanu 
*/ 

if (! defined('ABSPATH')) exit; // Exit if accessed directly 

add_action('plugins_loaded', 'woocommerce_Paysecure', 0); 

function woocommerce_Paysecure(){ 
    if (!class_exists('WC_Payment_Gateway')) 
     return; // if the WC payment gateway class is not available, do nothing 
    if(class_exists('WC_Paysecure')) 
     return; 

    class WC_Gateway_Paysecure extends WC_Payment_Gateway{ 
     public function __construct(){ 

      $plugin_dir = plugin_dir_url(__FILE__); 

      global $woocommerce; 

      $this->id = 'Paysecure'; 
      $this->icon = apply_filters('woocommerce_Paysecure_icon', ''.$plugin_dir.'Paysecure.png'); 
      $this->has_fields = true; 

      // Load the settings 
      $this->init_form_fields(); 
      $this->init_settings(); 

      // Define user set variables 
      $this->title = "Credit/Debit Card"; 
      $this->description = "You will be redirected to paysecure.ro to complete your purchase."; 
      $this->cui = "XXXXXXXXX"; 
      $this->encryption_key = "XXXXXXXXX"; 
      $this->currency = "USD"; 

      // Logs 
      if ($this->debug == 'yes'){ 
       $this->log = $woocommerce->logger(); 
      } 

      // Actions 
      add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page')); 

      // Save options 
      add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); 

      // Payment listener/API hook 
      add_action('woocommerce_api_wc_' . $this->id, array($this, 'check_ipn_response')); 

     } 

     function init_form_fields() 
      { 
       $this->form_fields = array(
        'enabled' => array(
         'title' => __('Enable/Disable', 'woocommerce'), 
         'type' => 'checkbox', 
         'label' => __('Enable Paysecure', 'woocommerce'), 
         'default' => 'yes' 
        ) 
       ); 
     } 

     public function admin_options() { 

      ?> 
      <h3><?php _e('Paysecure', 'woocommerce'); ?></h3> 
      <p><?php _e('Paysecure Payment Gateway', 'woocommerce'); ?></p> 

      <table class="form-table"> 
        <?php $this->generate_settings_html(); ?> 
       </table> 

      <?php 
     } 

     function payment_fields() { 
      $plugin_dir = plugin_dir_url(__FILE__); 
      // Description of payment method from settings 
      if ($this->description) { ?> 
       <p><?php 
       echo $this->description; ?> 
       </p><?php 
      } ?> 

<?php 
     } 

     function process_payment($order_id) { 
      global $woocommerce; 

      $order = new WC_Order($order_id); 

      // I WILL NEED THESE FIELDS 
      //$this->notify_url; 
     //$order->get_total(); 
      //$order->get_order_number(); 
      //$order->billing_first_name; 
      //$order->billing_email; 

     } 

    } 

    function add_Paysecure_gateway($methods){ 
     $methods[] = 'WC_Gateway_Paysecure'; 
     return $methods; 
    } 

    add_filter('woocommerce_payment_gateways', 'add_Paysecure_gateway'); 

} 

下面是我的商家帳戶提供商的PHP指令:

<?php 
require_once('./functions.php'); 
$key = 'THIS IS THE ENCRYPTION KEY'; 
$data = array('cui' => 'THIS IS THE CUI CODE', 
'AMOUNT' => '200', 
'CURRENCY' => 'USD', 
'ORDER' => '12313', 
'DESC' => ps_clean('SHORT DESCRIPTION'), 
'PENTRU' => 'EMAIL ADDRESS', 
'TRTYPE' => 0,        
'BACKREF' => 'http://www.site.ro/test.php', 
'TIMESTAMP'=>time() 

);

echo '<form action="https://paysecure.ro/order/cgi-bin/" method="post" >'; 

    foreach ($data as $l => $v) echo '<input size="55" readonly type="text" name="'.$l.'" value="'.$v.'" /> : <label for="'.$l.'">'.$l.'</label><br>'; 

    echo '<input size="55" type="text" name="P_SIGN" readonly value="'.calculateSign($data,$key).'" /><br>' ; //se calculeaza P_SIGN - ul , encriptarea datelor 

?> 
<input type="submit" /> 
</form> 

有人可以幫助我的其餘代碼?

回答

1

第一個錯誤我注意到你所做的$ this-> id必須是小寫。 'Paysecure'不會在這裏爲API鉤子工作。

// Payment listener/API hook 
add_action('woocommerce_api_wc_' . $this->id, array($this, 'check_ipn_response')); 

改變這個代替:

// Payment listener/API hook 
add_action('woocommerce_api_wc_paysecure', array($this, 'check_ipn_response')); 

第二個錯誤是,你忘了創建函數check_ipn_response()

// Handles the callbacks received from the payment backend. give this url to your payment processing comapny as the ipn response URL: 
    // USAGE: http://myurl.com/?wc-api=WC_Gateway_Paysecure 
     function check_ipn_response() { 

     $http_contents = file_get_contents("php://input"); 
     $json = json_decode($http_contents, true); 
     $order = new WC_Order($json['custom']); 

       if ($callback_json['status'] == 'Transaction approved') 
       { 
      $order->payment_complete(); //This will ensure stock reductions are made, and the status is changed to the correct value. 
      $order->add_order_note(__('KiwiPay Transaction Approved.', 'kiwipay')); 
       }     
else 
       { 
         $order->update_status('failed'); 
         $order->add_order_note('KiwiPay status received - ' . $callback_json['status']); 
    } 


       //references: 
       //http://docs.woothemes.com/document/payment-gateway-api/ 
       //https://github.com/Paymium/WooCommerce/blob/master/checkout.php 
       //https://github.com/tubiz/voguepay-woocommerce-payment-gateway/blob/master/voguepay-woocommerce-payment-gateway.php 


       /* 
       { 
       "id":1863, 
       "amount":1750, 
       "status":"Transaction approved", 
       "created":1434198776, 
       "reference":"2626", //this is the order number $order->get_order_number() 
       "name":"HamishTest Test", 
       "email":"[email protected]", 
       "address":{"street":"my rd","region":"N/A","city":"tauranga","zip":"3175","country":"NZ"}, 
       "test":true, 
       "price":"17.50", 
       "custom":"6589" //this is the wc order_id passed during process_payment to the paymen processor. 
       } 
       */ 
-1

這個怎麼樣?

add_action('plugins_loaded', 'init_gateway_cash'); 

function init_gateway_cash() { 
    class WC_Gateway_Cash extends WC_Payment_Gateway { 
     function __construct() { 
      $this->id = "Cash Gateway"; 
      $this->title = "Cash"; 
      $this->description = "More later"; 

      $this->init_form_fields(); 
      $this->init_settings(); 

      add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options')); 

     } 
     function init_form_fields(){ 
      $this->form_fields = array(
        'enabled' => array(
          'title' => __('Enable/Disable', 'woocommerce'), 
          'type' => 'checkbox', 
          'label' => __('Enable Cheque Payment', 'woocommerce'), 
          'default' => 'yes' 
        ), 
        'title' => array(
          'title' => __('Title', 'woocommerce'), 
          'type' => 'text', 
          'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'), 
          'default' => __('Cheque Payment', 'woocommerce'), 
          'desc_tip' => true, 
        ), 
        'description' => array(
          'title' => __('Customer Message', 'woocommerce'), 
          'type' => 'textarea', 
          'default' => '' 
        ) 
      ); 
     } 

     function process_payment($order_id) { 
      global $woocommerce; 
      $order = new WC_Order($order_id); 
      $productArray = array(); 
      $x = 0; 
      foreach($order->get_items() as $item_id => $item) { 
       $productArray[$x] = $order->get_product_from_item($item); 
       $x++; 
      } 

      // Mark as on-hold (we're awaiting the cheque) 
      $order->update_status('on-hold', 
      __('Awaiting cheque payment.', 'woocommerce') 
      ); 


      // Remove cart 
      $woocommerce->cart->empty_cart(); 

      // Return thankyou redirect 
      return array(
       'result' => 'success', 
       'redirect' => $this->get_return_url($order) 
      ); 
     } 
    } 

} 
function add_gateway_cash_class ($methods) { 
    $methods[] = 'WC_Gateway_Cash'; 
    return $methods; 
} 

add_filter('woocommerce_payment_gateways', 'add_gateway_cash_class'); 
+0

不能函數中定義的PHP類,只是刪除第一線並在函數外定義WC_Gateway_Cash類。 – ilumin

+1

@ilumin,當然你可以在函數裏面定義這個類,實際上對於這個插件它是強制性的。參見參考例子。 – hamish