【问题标题】:Update order after success or failed payment in woocommerce在woocommerce中付款成功或失败后更新订单
【发布时间】:2023-03-30 06:21:01
【问题描述】:

我为 woocommerce 集成了一个自定义支付网关,我已经成功完成它并且能够输出成功/失败并正确重定向以及清空购物车.. 正确重定向 空车 不更新 order_status 或 add_order_note

function check_pg_response($order_id)
{
global $woocommerce;
    $msg['class']   = 'error';
    $msg['message'] = $this->settings['error_msg']." - Error: No Response From Payment Gateway.";

if (isset($_REQUEST['msg'])){
$response = $_REQUEST['msg'];
$response_data = array();
$resp = explode("|", $response);
$dataSize=sizeof($resp);
    for($i = 0; $i < $dataSize; $i++) 
                {
                    $information=explode('=',$resp[$i]);
                    if($i==0)   $txn_status=$information[1]; //001   
                    if($i==1)   $txn_msg=$information[1]; // success or failure
                    if($i==2)   $txn_err_msg=$information[1]; // canceled by user   
                    if($i==3)   $txn_ref=$information[1];
                    if($i==4)   $bank_cd=$information[1];
                    if($i==5)   $txn_id=$information[1];    
                    if($i==6)   $txn_amt=$information[1];
                    if($i==7)   $txn_time=$information[1];    
                }
        }
if($order -> status !=='completed'){
$order_id = (int) $order_id;
$order = new WC_Order( $order_id );

    if ($txn_msg=='success'){
        $transauthorised = true;
        $msg['message']  = $this->settings['success_msg'];
        $msg['class']    = 'success';
if ($order->status != 'processing') {
        $order->payment_complete();
        $order->update_status('completed', __('Payment Successful.', 'wptut'));
        $order->add_order_note('PG ID: '.$clnt_txn_ref.'<br/> Transaction ID: '.$tpsl_txn_id.'<br/>Bank CD: '.$tpsl_bank_cd);
        $woocommerce -> cart -> empty_cart();}

        } 
        else if($txn_msg=='failure'){
        $msg['class']   = 'error';
        $msg['message'] = $this->settings['declined_msg']." - Error: Payment Gateway Declined order.";
        $order->update_status('failed', __('Payment has been cancelled.', 'wptut'));
        $order->add_order_note('PG payment failed<br/>Techprocess ID: '.$txn_ref.'<br/>Payment Gateway Message: '.$txn_err_msg);
        $woocommerce -> cart -> empty_cart();
    }else{
        $msg['class']   = 'error';
        $msg['message'] = $this->settings['error_msg']." - Error: Unknown Error";
    }

    if ($transauthorised == false) {
        $order->update_status('failed');
        $order->add_order_note($msg['message']);
    }
    }


if (function_exists('wc_add_notice')) {
    wc_add_notice($msg['message'], $msg['class']);

} else {
    if ($msg['class'] == 'success') {
        $woocommerce->add_message($msg['message']);
    } else {
        $woocommerce->add_error($msg['message']);

    }
    $woocommerce->set_messages();
}
$redirect_url = get_permalink(woocommerce_get_page_id('myaccount'));
wp_redirect($redirect_url);
exit;

} }

【问题讨论】:

  • 您是否将 woocommerce WC_Payment_Gateway 扩展到您的支付网关类?
  • 是的.. add_action('plugins_loaded', 'woocommerce_pg_init', 0);函数 woocommerce_pg_init(){ if(!class_exists('WC_Payment_Gateway')) 返回; WC_pg 类扩展 WC_Payment_Gateway{

标签: php wordpress payment-gateway woocommerce-rest-api


【解决方案1】:

在 Woocommerce 中,要制作自定义支付网关,您必须重写 WC_Payment_Gateway's 类函数。

do_order_complete_tasks函数处理订单完成前的动作。

 protected function do_order_complete_tasks()
     {

          global $woocommerce;

          // return if order status id already completed.
          if ($this->order->status == 'completed')
               return;

          $this->order->payment_complete();
          $woocommerce->cart->empty_cart();

          // add order note 
          $this->order->add_order_note(sprintf("Order completed with Transaction Id of '%s'", $this->transactionId));

     }

process_payment函数处理支付的订单流程

    function process_payment($order_id)
    {
        global $woocommerce;
        $this->order        = new WC_Order($order_id);

            // calling do_order_complete_tasks() function. 
            $this->do_order_complete_tasks();

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

    }

在您的自定义 WC 支付网关类中添加这两个函数并根据您的需要进行修改。

希望对您有所帮助!

【讨论】:

  • 出现错误:SyntaxError:JSON 中位置 1 的意外标记 F
  • @PSebastian 从我的答案中复制process_payment 函数并在您的代码中替换,我刚刚编辑过。它现在正在更新和添加订单注释两者都工作正常。
  • 对不起,我还是不能 add_order_note ?并再次结帐..它不会将其作为“新订单”处理,而是将其记录为付款完成..无需前往 PG ..
  • @PSebastian 哎呀,我添加了get_return_url() 而不是结帐付款网址。因此,在处理函数中将重定向值替换为'redirect' =&gt; $this-&gt;order-&gt;get_checkout_payment_url(true),以便它可以重定向到收据页面进行付款。
  • 我也尝试使用更改后的值“此订单的状态为“处理中”-无法付款。如果您需要帮助,请联系我们。”
猜你喜欢
  • 2020-03-15
  • 1970-01-01
  • 2017-03-17
  • 2016-08-26
  • 2013-04-10
  • 2019-09-18
  • 2019-05-18
  • 2021-12-09
  • 2017-04-08
相关资源
最近更新 更多