Hello guys. I urgently need help. I have the below code that can load a payment gateway in an iFrame or a link. I am able to get the link to work but the iFrame is throwing an error. I am willing to buy any willing helper a cup of coffee. let’s have a google meet. Thank you.
\+2348136776626 – WhatsApp
[[email protected]](mailto:[email protected]) \- Email
<?php
if (!class_exists(‘WC_Payment_Gateway’)) {
return;
}
class PayInvert extends WC_Payment_Gateway
{
public function __construct()
{
// Define gateway properties
$this->id = ‘payinvert’;
$this->method_title = ‘PayInvert Gateway’;
$this->method_description = ‘Accept payments via the PayInvert Gateway.’;
$this->supports = array(
‘products’,
‘refunds’
);
// Load settings
$this->init_form_fields();
$this->init_settings();
// Define settings
$this->title = $this->get_option(‘title’);
$this->description = $this->get_option(‘description’);
// Hooks
add_action(‘woocommerce_update_options_payment_gateways_’ . $this->id, array($this, ‘process_admin_options’));
}
// Initialize settings fields
public function init_form_fields()
{
$this->form_fields = array(
‘enabled’ => array(
‘title’ => ‘Enable/Disable’,
‘label’ => ‘Enable My Payment Gateway’,
‘type’ => ‘checkbox’,
‘description’ => ”,
‘default’ => ‘yes’,
),
‘title’ => array(
‘title’ => ‘Title’,
‘type’ => ‘text’,
‘description’ => ‘This controls the title which the user sees during checkout.’,
‘default’ => ‘My Payment Gateway’,
‘desc_tip’ => true,
),
‘use_iframe’ => array(
‘title’ => ‘Use iFrame’,
‘label’ => ‘Use iFrame for payment’,
‘type’ => ‘checkbox’,
‘description’ => ‘Check this box to load the payment in an iFrame.’,
‘default’ => ‘yes’,
),
‘description’ => array(
‘title’ => ‘Description’,
‘type’ => ‘textarea’,
‘description’ => ‘This controls the description which the user sees during checkout.’,
‘default’ => ‘Pay securely using My Payment Gateway.’,
‘desc_tip’ => true,
),
);
}
// Process the payment and return the result
public function process_payment($order_id)
{
$order = wc_get_order($order_id);
// Process the payment, either by iFrame or redirect.
// Replace the following line with your specific iFrame or redirect logic.
// Example 1: Load the payment in an iFrame using the provided iFrame script.
if ($this->use_iframe()) {
// Mark the order as “on-hold” to indicate that payment is being processed.
$order->update_status(‘on-hold’, __(‘Payment is being processed.’, ‘your-textdomain’));
// Enqueue the required JavaScript library (ItexPayNS) if not already enqueued
// wp_enqueue_script(‘itex-pay-ns’, ‘https://gateway-dev.payinvert.com/v1.0.0/payinvert.js’, array(), null, true);
// Prepare the data to be passed to the JavaScript as JSON
// $payment_data = array(
// ‘api_key’ => ‘PA-PUBKEY-57f048a9-37d6-4981-b71c-1fd2d7146575’,
// ‘first_name’ => ‘Daniel’,
// ‘last_name’ => ‘Arikawe’,
// ‘phone_number’ => ‘08136776626’,
// ’email’ => ‘[email protected]’,
// ‘amount’ => 10,
// ‘currency’ => ‘NGN’,
// );
// Generate the JavaScript code with the payment data
// $script_data = ‘
// const Pay = new window.ItexPayNS.ItexPay(‘ . json_encode($payment_data) . ‘);
// Pay.init();
// ‘;
// $script_data = ‘alert(“oluwa gba mi o”)’;
// // Add the JavaScript code to be output within the script tags
// wp_add_inline_script(‘itex-pay-ns’, $script_data);
return array(
‘result’ => ‘fail’,
‘redirect’ => $this->get_return_url($order),
);
}
// Example 2: Redirect the customer to the payment checkout page.
else {
// Mark the order as “on-hold” to indicate that payment is being processed.
$order->update_status(‘on-hold’, __(‘Payment is being processed.’, ‘your-textdomain’));
// Redirect the customer to the payment checkout page
return array(
‘result’ => ”,
‘redirect’ => ‘https://payment-checkout-dev.payinvert.com/?order_id=’ . $order_id,
);
}
// If payment is successful (you should update this condition based on your payment gateway response)
// $payment_success = true;
// if ($payment_success) {
// // Mark the order as completed since the payment was successful
// $order->payment_complete();
// }
// // Return success/failure result to WooCommerce
// return array(
// ‘result’ => $payment_success ? ‘success’ : ‘fail’,
// ‘redirect’ => $this->get_return_url($order),
// );
}
// Function to check if the iFrame should be used based on the WooCommerce settings
public function use_iframe()
{
// Get the plugin settings
$settings = get_option(‘woocommerce_’ . $this->id . ‘_settings’);
// Check if the ‘use_iframe’ setting exists and if it’s set to ‘yes’
if (isset($settings[‘use_iframe’]) && $settings[‘use_iframe’] === ‘yes’) {
return true;
}
return false;
}
// Handle webhooks for completed orders
// Webhook listener to handle completed payment notifications from the payment gateway
public function webhook_listener()
{
// Retrieve the raw payload from the webhook request
$raw_payload = file_get_contents(‘php://input’);
// Decode the JSON payload (assuming your payment gateway sends webhook data in JSON format)
$payload = json_decode($raw_payload, true);
if (empty($payload)) {
// Log or handle the error if payload is empty
return;
}
// Retrieve the order ID from the webhook payload (update this based on your payment gateway’s data structure)
$order_id = isset($payload[‘order_id’]) ? $payload[‘order_id’] : null;
if (empty($order_id)) {
// Log or handle the error if the order ID is missing in the webhook payload
return;
}
// Retrieve the WooCommerce order object
$order = wc_get_order($order_id);
if (!$order) {
// Log or handle the error if the order is not found in WooCommerce
return;
}
// Assuming your payment gateway sends a status indicating a successful payment
// Adjust the condition based on your payment gateway’s response
$payment_success = isset($payload[‘status’]) && $payload[‘status’] === ‘success’;
// If payment is successful, update the order status and note
if ($payment_success) {
// Update the order status to “completed”
$order->update_status(‘completed’, __(‘Payment successfully received.’, ‘your-textdomain’));
// Add a note to the order to record the payment
$order->add_order_note(__(‘Payment successfully received.’, ‘your-textdomain’));
} else {
// If the payment is not successful, you may handle the failure accordingly.
// For example, you can update the order status to “failed” and add a note.
$order->update_status(‘failed’, __(‘Payment failed.’, ‘your-textdomain’));
$order->add_order_note(__(‘Payment failed.’, ‘your-textdomain’));
}
}
}
​
#
[ad_2]