doujiurong7210 2019-03-15 20:07
浏览 94
已采纳

Woocommerce API连接到下订单按钮

I am trying to connect to an api using the code below, so when the customer clicks on the "place order" button on the Woocommerce checkout page, I am getting a "please try again" error:

var amount = <?php global $woocommerce; print WC()->cart->total; ?>;
var merchantOrderId = '<?php echo print time(); ?>';
var apiKey = 'm85BXXLpf_icrSvqbElR11xquEgmKZ8wfeRb2ly3-G7pIwCKDuytgplB7AQGi-5t';

renderMMoneyPaymentButton(amount, merchantOrderId, apiKey);

I am trying to pass this information to the api via this function but I am not getting a successful connection.

public function process_payment( $order_id ) {

    global $woocommerce;

    // we need it to get any order detailes
    $order = new WC_Order($order_id);


    /*
     * Array with parameters for API interaction
     */
    $args = array(
     'amount' => '<?php global $woocommerce; print WC()->cart->total; ?>',
     'merchant_order_id' => '<?php print time(); ?>',
     'api_Key' => 'm85BXXLpf_icrSvqbElR11xquEgmKZ8wfeRb2ly3-G7pIwCKDuytgplB7AQGi-5t',
     'currency' => 'BBD',

    );
    /*
     * Your API interaction could be built with wp_remote_post()
     */
     $response = wp_remote_post( 'https://api.mmoneybb.com/merchant/js/mmoney-payment.js', $args );


     if( !is_wp_error( $response ) ) {

         $body = json_decode( $response['body'], true );

         // it could be different depending on your payment processor
         if ( $body ['$response'] == 'APPROVED') {

            // we received the payment
            $order->payment_complete();
            $order->reduce_order_stock();

            // some notes to customer (replace true with false to make it private)
            $order->add_order_note( 'Thanks for your payment!!!!', true );

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

            // Redirect to the thank you page
            return array(
                'result' => 'success',
                'redirect' => $this->get_return_url( $order )
            );

         } else {
            wc_add_notice(  'Please try again.', 'error' );
            return;
        }

    } else {
        wc_add_notice(  'Connection error.', 'error' );
        return;
    }

}

let me know what i am doing wrong much appreciated also this is the other script as well

 function renderMMoneyPaymentButton(amount, merchantOrderId, apiKey) {
  let paymentParams = {
    amount: amount,
    api_key: apiKey,
    currency: 'BBD',
    merchant_order_id: merchantOrderId,
    onCancel: function () { console.log('Modal closed'); },
    onError: function(error) { console.log('Error', error); },
    onPaid: function (invoice) { console.log('Payment complete', invoice); }
  };

  // "mMoney" window global provided by sourcing mmoney-payment.js script.
  // Attach the button to the empty element.
  mMoney.payment.button.render(paymentParams, '#mmoney-payment-button');


}
  • 写回答

1条回答 默认 最新

  • duanmianzhou5353 2019-03-16 00:44
    关注

    1) In your first snippet code you are using javascript and you need to get the order Id and then the order total… You can only get the Order ID after the order is placed…

    There is an answer example here.

    2) Your 2nd public function involves only PHP… There are some errors and mistakes in this code. Try the following revisited code instead:

    public function process_payment( $order_id ) {
    
        // Get The WC_Order Object instance
        $order = wc_get_order( $order_id );
    
        /*
         * Array with parameters for API interaction
         */
        $args = array(
            'amount'            => $order->get_total(),
            'merchant_order_id' => $order_id,
            'api_Key'           => 'm85BXXLpf_icrSvqbElR11xquEgmKZ8wfeRb2ly3-G7pIwCKDuytgplB7AQGi-5t',
            'currency'          => $order->get_currency(),
        );
    
        /*
         * Your API interaction could be built with wp_remote_post()
         */
         $response = wp_remote_post( 'https://api.mmoneybb.com/merchant/js/mmoney-payment.js', $args );
    
         if( !is_wp_error( $response ) ) {
    
             $body = json_decode( $response['body'], true );
    
             // it could be different depending on your payment processor
             if ( $body ['$response'] == 'APPROVED') {
    
                // we received the payment
                $order->payment_complete();
                $order->reduce_order_stock();
    
                // some notes to customer (replace true with false to make it private)
                $order->add_order_note( 'Thanks for your payment!!!!', true );
    
                // Empty cart
                $woocommerce->cart->empty_cart();
    
                // Redirect to the thank you page
                return array(
                    'result' => 'success',
                    'redirect' => $this->get_return_url( $order )
                );
    
             } else {
                wc_add_notice(  'Please try again.', 'error' );
                return;
            }
    
        } else {
            wc_add_notice(  'Connection error.', 'error' );
            return;
        }
    
    }
    

    It should better work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下