Javascript Woocommerce API连接到下订单按钮

Javascript Woocommerce API连接到下订单按钮,javascript,php,wordpress,woocommerce,payment-gateway,Javascript,Php,Wordpress,Woocommerce,Payment Gateway,我正在尝试使用下面的代码连接到api,因此当客户单击Woocommerce结帐页面上的“下单”按钮时,我收到一个“请重试”错误: 1) 在您的第一个代码片段中,您使用的是javascript,您需要获得订单Id,然后是订单总数…您只能在下订单后获得订单Id 有一个答案示例 2) 您的第二个公共函数只涉及PHP…此代码中有一些错误和错误。请尝试以下重新访问的代码: public function process_payment( $order_id ) { // Get The WC_O

我正在尝试使用下面的代码连接到api,因此当客户单击Woocommerce结帐页面上的“下单”按钮时,我收到一个“请重试”错误:

1) 在您的第一个代码片段中,您使用的是javascript,您需要获得订单Id,然后是订单总数…您只能在下订单后获得订单Id

有一个答案示例

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

}

它应该能更好地工作。

感谢您的帮助,在上发布了她的问题,并发布了插件代码,以进一步澄清,我们将向您捐赠页面,以感谢您的帮助。
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');


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

}