I use the the woocommerce hooks woocommerce_after_checkout_validation and woocommerce_payment_complete to do api calls to a partner site. These calls return a confirmation that the data is valid and then actually send the data on payment complete to create a number which we then save in the order meta. We then also use the woocommerce_before_thankyou hook to display the contract number and instructions on how to use it.
This works perfectly when using Stripe to checkout but when using Paypal or Splitit, both of which take the customer off-site to conduct the payment process and then bring him back, none of these hooks are triggered. the partner does not receive the verification call nor payment_complete call and the before_thankyou text does not fire either. Is there a work around to make sure that the hooks always trigger or maybe different hooks that are more appropriate, here is the code:
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;
}
}