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

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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置