【发布时间】:2020-03-06 21:38:55
【问题描述】:
我正在为 woocommerce 开发一个自定义支付网关,一切都很好,但支付成功后订单仍然“待付款”
在__construct() 函数中,我检查支付提供商的响应
<?php
//when payment done and redirected with payment reference code
if(isset($_REQUEST['transaction_id'])){
paytabs_set_cookie('transaction_id', $_REQUEST['transaction_id']);
$order = new WC_Order($order_id);
$this->order = $order;
$this->complete_transaction($order->get_id(), $_REQUEST['transaction_id']);
}
这里是complete_transaction() 函数
<?php
/*
When transaction completed it is check the status
is transaction completed or rejected
*/
function complete_transaction($order_id, $transaction_id) {
global $woocommerce;
$order = new WC_Order( $order_id );
$request_string=array(
'secret_key' => $_COOKIE['secret_key'],
'merchant_email' => $_COOKIE['merchant_email'],
'transaction_id' => $transaction_id,
'order_id' => $order_id
);
$gateway_url=$this->liveurl.'apiv2/verify_payment_transaction';
$getdataresponse=$this->sendRequest($gateway_url, $request_string);
$object=json_decode($getdataresponse, true);
if (isset($object->response_code)) {
//if get response successfull
if($object->response_code == '100'){
// thankyou and set error message
$this->msg['class'] = 'woocommerce_message';
$check=$order->payment_complete();
// Reduce stock levels
$order->reduce_order_stock();
// Remove cart
$woocommerce->cart->empty_cart();
wc_add_notice( '' . __( 'Thank you for shopping with us. Your account has been charged and your transaction is successful.
We will be shipping your order to you soon.', 'woocommerce' ), 'success' );
wp_redirect( $this->get_checkout_order_received_url( $order ) );
exit;
}else{
// Change the status to pending / unpaid
$order->update_status('failed', __('Payment Cancelled', 'error'));
// Add error for the customer when we return back to the cart
$message=$object->result;
wc_add_notice( '<strong></strong> ' . __($message, 'error' ), 'error' );
// Redirect back to the last step in the checkout process
wp_redirect( $this->get_cancel_order_url( $order ) );
exit;
}
}
}
但订单状态仍为“待付款”。 现在我需要在检查付款提供商响应后将订单状态更改为“已完成”。
【问题讨论】:
标签: wordpress woocommerce payment-gateway