Reputation: 303
I'm working on integrating a custom payment gateway with WooCommerce. The API for the payment gateway works in two parts:
The customer fills the checkout form, hits "order," and the first call to the API is made. The customer is then redirected to the payment gateway to enter their card details. After the customer validates the payment, they are redirected back to the website, and here's where the second call to the API is supposed to be made to confirm the payment. I have created a custom plugin and added the payment method to the WooCommerce payment options. I wrote the API logic in the 'process_payment' function of WooCommerce. The first part is working fine; when the user checks out, they are redirected to the payment gateway to fill in their card details and then redirected back to the website.
However, the second part is not working, and the payment stays "pending." I've tried using WooCommerce hooks 'template_redirect' hook to call the second API after the redirection to the 'formUrl', but nothing seems to be working.
Here's my code:
<?php
add_action('plugins_loaded', 'init_cib_payment_gateway');
function init_cib_payment_gateway() {
class WC_CIB_Payment_Gateway extends WC_Payment_Gateway {
public function __construct() {
$this->id = 'cib_payment_gateway';
$this->method_title = 'CIB Payment Gateway';
$this->title = 'CIB Payment Gateway';
$this->has_fields = true;
$this->init_form_fields();
$this->init_settings();
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
}
public function init_form_fields() {
// Define your form fields here if needed
}
public function process_payment($order_id) {
$order = wc_get_order($order_id);
);
$api_url = '/payment/rest/register.do?';
$username = 'API-Username';
$password = 'API-password';
$amount = $order->get_total()*100;
$currency = '012'; // Change to your currency code
$return_url = 'https://www.my-website.com/checkout/order-received/?confirmPayment=true';
$jsonParams = json_encode(array("force_terminal_id" => "E010900759", "udf1" => "2018105301346", "udf5"=>"ggsf85s42524s5uhgsf"));
$random_string = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'), 0, 5);
// Append credentials and other parameters to the API URL
$api_url = add_query_arg(array(
'timeout' => 50,
'userName' => $username,
'password' => $password,
'orderNumber' => $order_id . $random_string ,
'amount' => $amount,
'currency' => $currency,
'returnUrl' => $return_url . $order_id,
'language' => 'EN',
'jsonParams' => $jsonParams,
// Add any other required parameters
), $api_url);
// Call the CIB payment gateway API to initiate the payment
// Replace this with your actual API call
$api_response = wp_remote_get($api_url);
// Process the API response and redirect to the payment page
if (is_wp_error($api_response)) {
throw new Exception($api_response->get_error_message());
echo $response['formUrl'];
}
$body_response = wp_remote_retrieve_body($api_response);
$response = json_decode($body_response, true);
// Save remote ID transaction
update_post_meta($order_id, 'confirm_cib_payment', true);
// To Gateway page
return array(
'result' => 'success',
'redirect' => $response['formUrl'],
);
}
public function payment_fields() {
// No need to display any additional fields here since we're using the WooCommerce checkout form
}
}
function add_cib_payment_gateway($methods) {
$methods[] = 'WC_CIB_Payment_Gateway';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'add_cib_payment_gateway');
}
add_action('woocommerce_payment_complete', 'confirm_payment');
function confirm_payment($order_id) {
if (!get_post_meta($order_id, 'confirm_cib_payment', true)) {
return; // No confirmation needed or already done
}
if (isset($_GET['confirmPayment']) && $_GET['confirmPayment'] === 'true') {
$orderId = isset($_GET['orderId']) ? sanitize_text_field($_GET['orderId']) : '';
$api_url2 = add_query_arg(array(
'language' => 'EN',
'orderId' => $orderId,
'password' => 'API-password',
'userName' => 'API-username',
), '/payment/rest/confirmOrder.do?');
$api_response2 = wp_remote_get($api_url2);
echo $api_url2;
echo $api_response2;
if (is_wp_error($api_response2)) {
return 'There was an error confirming the payment.';
} else {
$body_response2 = wp_remote_retrieve_body($api_response2);
$response2 = json_decode($body_response2, true);
if (isset($response2['ErrorCode']) && $response2['ErrorCode']==0) {
$order->update_status('completed', __('Payment confirmed', 'cib-pay-woo'));
return 'Payment confirmed successfully.';
} else {
$order->update_status('failed', __('Payment confirmation failed', 'cib-pay-woo'));
return 'Payment confirmation failed.'. $response2['ErrorMessage'];
}
delete_post_meta($order_id, 'confirm_cib_payment'); // Cleanup meta
}
}
}
?>
I'm looking for guidance on how to correctly call the second API after the customer is redirected back to the website. Any help would be appreciated. Thank you!
Upvotes: 0
Views: 85