Woocommerce API连接到下订单按钮

问题描述 投票:1回答:1

我正在尝试使用下面的代码连接到api,所以当客户点击Woocommerce结帐页面上的“下订单”按钮时,我收到“请再试一次”错误:

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);

我试图通过此功能将此信息传递给api但我没有成功连接。

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;
    }

}

让我知道我做错了什么,也非常感谢这也是另一个脚本

 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');


}
javascript php wordpress woocommerce payment-gateway
1个回答
0
投票

1)在您的第一个代码片段中,您使用的是javascript,您需要获取订单ID,然后才能获得订单总额...您只能在下订单后获得订单ID ...

有一个答案的例子here

2)你的第二个公共功能只涉及PHP ......这段代码中有一些错误和错误。请尝试以下重新访问的代码:

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;
    }

}

它应该更好地工作。

© www.soinside.com 2019 - 2024. All rights reserved.