woocommerce - 支付网关未触发 woocommerce_payment_complete 和 woocommerce_after_checkout_validation 挂钩
问题描述
我使用 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;
}
}
解决方案
按下订单确认按钮后,挂钩woocommerce_after_checkout_validation
被激活。因此,无论选择哪种付款方式,它里面的功能每次肯定都会被激活。作为第一步,检查 API 调用是否在此处正确完成。
然后,由于woocommerce_payment_complete
挂钩仅针对某些订单状态激活,请参阅此答案以获取更多信息,您可以将挂钩替换为woocommerce_pre_payment_complete
.
当然,请确保问题与一个或两个函数中的 API 调用无关。
要进行测试,请尝试contract
使用您之前选择的值更新订单元数据$qover_present = WC()->session->get('qover_created');
。这样你就可以知道钩子是否会被触发,以及问题是否出在 API 调用中。
最后还要检查是否WC()->session->get('qover_created');
包含您期望的数据。
推荐阅读
- python - 返回 PubSub 回调消息
- sphinx - Sphinx(搜索引擎)3 不读取 CSV 文件
- c++ - 我可以在稍后声明的名称空间中定义名称吗?
- api - 将值传递给 api 以返回所有值
- git - git ls-files:仅显示 blob 文件
- snowflake-cloud-data-platform - 雪花任务和流 - 复杂性和可视化
- r - 在小标题的整行中搜索字符串?
- typescript - 升级的 Typescript - 尝试导入错误:“BaseMap”未从“./core”导出。- 这是
- javascript - 在输入更改时选择 datalist 值并使用 jQuery javascript 填充表单
- python - Django manytomany 字段持有用户自动填充