支付网关不触发 woocommerce_payment_complete 和 woocommerce_after_checkout_validation 挂钩

时间:2021-03-15 17:56:51

标签: woocommerce paypal payment-gateway hook-woocommerce

我使用 woocommerce 挂钩 woocommerce_after_checkout_validation 和 woocommerce_payment_complete 对合作伙伴网站进行 api 调用。这些调用返回数据有效的确认,然后实际发送付款完成数据以创建一个数字,然后我们将其保存在订单元数据中。然后,我们还使用 woocommerce_before_thankyou 挂钩来显示合同编号和使用说明。

这在使用 Stripe 结账时非常有效,但在使用 Paypal 或 Splitit 时,这两种方法都将客户带到异地进行付款流程,然后将他带回来,这些钩子都不会被触发。合作伙伴不会收到验证调用或 payment_complete 调用,并且 before_thankyou 文本也不会触发。有没有办法确保钩子总是触发或者更合适的不同钩子,这里是代码:

add_action('woocommerce_after_checkout_validation', 'apiqovercall_verif');

// handle the ajax request
function apiqovercall_verif() {
    $insurance_ids  = array(24027,24031,24034,24035,24033,24032);
    $ebike_ids      = array(17386,17385,17382,17378,17375,17372,17370,17369,17364,16132,16130,4561,4550,3490,2376);

    $fields = [
      'billing_first_name'                       => '',
      'billing_last_name'     => '',
      'billing_email'                => '',
      'billing_phone'    => '',
      'insurance-birthdate'    => '',
      'gender-selection'    => '',
        'billing_address_1'    => '',
        'billing_address_2'    => '',
        'billing_postcode'    => '',
        'billing_city'    => ''  
    ];
    
    foreach( $fields as $field_name => $value ) {
      if( !empty( $_POST[ $field_name ] ) ) {
        $fields[ $field_name ] = sanitize_text_field( $_POST[ $field_name ] );
      } 
    }
     
      foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( in_array( $cart_item['product_id'],  $ebike_ids ) ) {
                $_product_id = $cart_item['product_id'];
            }
        }
     $_product =  wc_get_product( $_product_id );
$billingcountry = WC()->customer->get_billing_country();
$cur_lang = pll_current_language();

$data_qover_verif =  array(
      "refs"        =>  array(
            "country"         => $billingcountry,
            "product"        => "BIKE"
      ),
      "settings"        =>  array(
            "language"         => $cur_lang
      ),
      "policyholder" =>  array(
             "firstName"        => $fields[ 'billing_first_name'] ,
             "lastName"     => $fields[ 'billing_last_name'],
             "email"        => $fields[ 'billing_email'],
             "phone"        => $fields[ 'billing_phone'],
             "birthdate"        => $fields[ 'insurance-birthdate'],
             "gender"       => $fields[ 'gender-selection' ],
             "address"        =>  array(
                     "country"      => $billingcountry,
                     "zip"      => $fields[ 'billing_postcode'],
                     "city"     => $fields[ 'billing_city'],
                     "street"       => $fields[ 'billing_address_1'],
                     "number"       => ' ',
                     "box"      => "box"),
          "entityType"      => "ENTITY_TYPE_PERSON"

      ),
      "risk"         => array(
            
             "model"         => $_product -> get_title(),
             "originalValue"         =>  $_product -> get_price() * 100,
      ),
    );

        $call_verif = callAPI('POST', 'https://dojo-production-bike-api.production.cluster.qover.io/v1/ancillary/validate', json_encode($data_qover_verif));
        $response_verif = json_decode($call_verif, true);
        
        if ($response_verif["status"] != "STATUS_OPEN" ) {
            wc_add_notice(pll__('There was an error'),'error')  ;
        } else {
            WC()->session->set('data_qover', $data_qover_verif);
            WC()->session->set('qover_created', "1");

        }
    }
}


add_action( 'woocommerce_payment_complete', 'apiqovercall_create' );
function apiqovercall_create( $order_id ) {
        $qover_present = WC()->session->get('qover_created');
        if ($qover_present === "1") {
        $data_qover_creation = WC()->session->get('data_qover');
        $call_create = callAPI('POST', 'https://dojo-production-bike-api.production.cluster.qover.io/v1/ancillary/', json_encode($data_qover_creation));
        $response_create = json_decode($call_create, true);
        update_post_meta( $order_id, 'contract', $response_create);
        WC()->session->__unset('data_qover');
        WC()->session->__unset('qover_created');
        }
    
}

add_action( 'woocommerce_before_thankyou', 'display_qover_contract' );
function display_qover_contract( $order_id ) {
    $contract_id = get_post_meta($order_id, 'contract', true);
    if ( ! empty( $contract_id ) ) {
        $contract_message = pll__('<div class="qover_notice">Your contract has been created successfully.  Your contract id is: ') . $contract_id['contractId'] . "</div>";
    echo $contract_message;
    }
}

1 个答案:

答案 0 :(得分:0)

在按下订单确认按钮后,woocommerce_after_checkout_validation 钩子被激活。因此,无论选择哪种付款方式,每次都肯定会激活其中的功能。作为第一步,请检查此处的 API 调用是否正确完成。

然后,由于 woocommerce_payment_complete 挂钩仅针对某些订单状态激活,请参阅此 answer 以了解更多信息,您可以将挂钩替换为 woocommerce_pre_payment_complete

<块引用>

当然要确保问题与 API 调用无关 一种或两种功能。

要进行测试,请尝试在 contract 之前使用您选择的值更新 $qover_present = WC()->session->get('qover_created'); 订单元数据。这样您就可以知道是否会触发钩子,以及问题是否出在 API 调用中。

最后还要检查 WC()->session->get('qover_created'); 是否包含您期望的数据。

相关问题