gpt4 book ai didi

wordpress - Woocommerce 提交到支付网关

转载 作者:行者123 更新时间:2023-12-01 10:39:20 24 4
gpt4 key购买 nike

我正在为 Woocommerce 支付开发 wordpress 插件。如我所见,我必须扩展 WC_Payment_Gateway 并实现方法“process_payment”。我查看了一些示例,发现此方法应返回如下内容:

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

然后控制权返回给 WC_Checkout,它将重定向到提供的 url。
问题是我们的支付提供商需要提交表单到他的页面,而不是重定向。考虑到 API 限制,我想知道与我的支付提供商联系的最佳方式是什么?
有没有办法提交表单而不是重定向?

最佳答案

您可以通过使用以下方法扩展 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>';
}

关于wordpress - Woocommerce 提交到支付网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31499251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com