2015-07-19 53 views
2

我開發Woocommerce支付WordPress插件。 我可以看到我不得不延長WC_Payment_Gateway和實現方法「process_payment」。我已經調查了一些例子,發現這個方法應該返回類似:Woocommerce提交給支付網關

array(
    'result' => 'success', 
    'redirect' => $redirectUrl 
); 

,然後控制返回到WC_Checkout將重定向到提供的url。
問題是我們的支付提供商需要提交表格到他的頁面而不是重定向。考慮到API限制,我在問什麼是與我的支付提供商聯繫的最佳方式?
有沒有辦法提交的形式,而不是重定向?

+0

你找到一個解決您的文章嗎?目前面臨同樣的挑戰! – nyedidikeke

+0

我有某種方式finisjhed項目,可能找到解決方案,但在此刻忘了它。 – gandra404

+0

我已經發布了一個描述我如何成功實現它的答案。 :-) – nyedidikeke

回答

0

您可以通過擴展WooCommerce WC_Payment_Gateway類以下方法實現它的說明和評論如下:

public function __construct() { 
    $this->id     = 'my-payment-gateway'; 
    $this->icon     = plugins_url('/path/to/image.extension', __FILE__); 
    $this->has_fields   = true; 
    $this->method_title   = __('My Payment Gateway', 'txdomain'); 
    $this->method_description = __('Add some descriptions here.', 'txdomain'); 
    $this->init_form_fields(); 
    $this->init_settings(); 
    $this->title    = $this->get_option('title'); 
    $this->description   = $this->get_option('description'); 

    add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(
     $this, 
     'process_admin_options' 
    )); 
    // below is the hook you need for that purpose 
    add_action('woocommerce_receipt_' . $this->id, array(
     $this, 
     'pay_for_order' 
    )); 
} 

// here, your process payment method, returning the pay for order page 
// where you will can submit the form to your payment gateway providers' page 
public function process_payment($order_id) { 
    $order = new WC_Order($order_id); 

    return array(
     'result' => 'success', 
     'redirect' => $order->get_checkout_payment_url(true) 
    ); 
} 

// here, prepare your form and submit it to the required URL 
public function pay_for_order($order_id) { 
    $order = new WC_Order($order_id); 
    echo '<p>' . __('Redirecting to payment provider.', 'txtdomain') . '</p>'; 
    // add a note to show order has been placed and the user redirected 
    $order->add_order_note(__('Order placed and user redirected.', 'txtdomain')); 
    // update the status of the order should need be 
    $order->update_status('on-hold', __('Awaiting payment.', 'txtdomain')); 
    // remember to empty the cart of the user 
    WC()->cart->empty_cart(); 

    // perform a click action on the submit button of the form you are going to return 
    wc_enqueue_js('jQuery("#submit-form").click();'); 

    // return your form with the needed parameters 
    return '<form action="' . 'https://example.com' . '" method="post" target="_top"> 
     <input type="hidden" name="merchant_key" value=""> 
     <input type="hidden" name="success_url" value=""> 
     <input type="hidden" name="cancelled_url" value=""> 
     <input type="hidden" name="deferred_url" value=""> 
     <input type="hidden" name="invoice_id" value=""> 
     <input type="hidden" name="total" value=""> 
     <div class="btn-submit-payment" style="display: none;"> 
      <button type="submit" id="submit-form"></button> 
     </div> 
    </form>'; 
}