【发布时间】:2021-06-13 00:32:05
【问题描述】:
我使用 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;
}
}
【问题讨论】:
-
您的订单是否只包含一种产品? callAPI() 函数的第二个参数是“url”,它没有按原样设置和写入会产生语法错误。你能修复/更新你的代码吗?如果您删除了“重要”部分,请发布它们。一般来说,您可以通过使用
woocommerce_payment_complete钩子激活的单个函数来解决问题(然后您可以通过从创建的订单对象中获取客户和产品数据来将apiqovercall_verif函数集成到apiqovercall_create中)。另请参阅answer。 -
感谢您的帮助!我已经更新了 URL,我删除了它,因为 API 调用运行良好,但现在它已被编辑。钩子本身似乎根本没有被触发。另外,我在 apiqovercall_verif 和 apiqovercall_create 之间以这种方式拆分的原因是,如果未通过 API 验证,验证部分需要由客户在结帐时更新,因此必须在付款前进行,而创建部分只能发生收到并确认付款后。
-
我添加了以下过滤器: add_filter( 'woocommerce_valid_order_statuses_for_payment_complete', 'add_woocommerce_valid_order_statuses_for_payment_complete', 10, 2 );函数 add_woocommerce_valid_order_statuses_for_payment_complete( $statuses, $order ) { $statuses[] = 'processing';返回$状态;但是仍然遇到同样的问题,使用 paypal 结帐或拆分时,payment_complete 挂钩不会触发。
标签: woocommerce paypal payment-gateway hook-woocommerce