← Back
Editing: class.payzeep-settings.php
<?php class Payzeep_Settings { public function __construct() { add_action('admin_menu', [$this, 'add_settings_page']); add_action('admin_init', [$this, 'register_settings']); add_shortcode('pzee_button', [$this, 'render_shortcode_button']); add_shortcode('pzee_payment_form', [$this, 'render_payment_form_shortcode']); add_shortcode('pzee_payment_redirect', [$this, 'render_payment_redirect_shortcode']); add_action('admin_post_pzee_process_payment', [$this, 'handle_payment_form']); add_action('admin_post_nopriv_pzee_process_payment', [$this, 'handle_payment_form']); add_action( 'rest_api_init', [$this, 'pzeep_register_webhook_route']); add_action('wp_ajax_payzeep_gateway_payment_confirm', [$this, 'handle_payzeep_gateway_payment_confirm']); add_action('wp_ajax_nopriv_payzeep_gateway_payment_confirm', [$this, 'handle_payzeep_gateway_payment_confirm']); add_action('admin_post_payzeep_gateway_payment_confirm', [$this, 'handle_payzeep_gateway_payment_confirm']); add_action('admin_post_nopriv_payzeep_gateway_payment_confirm', [$this, 'handle_payzeep_gateway_payment_confirm']); add_action('wp_enqueue_scripts', function () { if (is_page(get_option('pzee_payment_redirect')) || is_page(get_option('zeep_redirect')) || is_page(get_option('pzee_payment_form'))) { wp_enqueue_style( 'zeep-style', // handle plugin_dir_url(__FILE__) . 'assets/css/zeep.css', // path to your CSS array(), // dependencies // filemtime( plugin_dir_path( __FILE__ ) . 'assets/css/zeep.css' ) // version ); wp_enqueue_script( 'zeep-loader', plugin_dir_url(__FILE__) . 'assets/js/zeep.js', array('jquery'), null, true ); wp_enqueue_script( 'checkout-script', 'https://js.zeepway.com/checkout.js', [], null, true ); } }); add_filter('template_include', function ($template) { global $post; if (is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'pzee_payment_redirect')) { // Return our own bare template return plugin_dir_path(__FILE__) . 'templates/zeep-basic.php'; } return $template; }); } public function add_settings_page() { if ($this->woocommerce_active()) { return; } // add_options_page( // 'Payzeep Settings', // 'Payzeep', // 'manage_options', // 'pzee-settings', // [$this, 'render_settings_page'] // ); add_menu_page( 'Payzeep Settings', 'Payzeep', 'manage_options', 'pzee-settings', [$this, 'render_settings_page'], plugin_dir_url(__DIR__) . 'payzeep-pg/assets/zeep-icon.png', 56 ); // add_submenu_page( // 'woocommerce', // 'Payzeep Settings', // 'Payzeep', // 'manage_woocommerce', // 'pzee-settings', // [$this, 'render_settings_page'], // plugin_dir_url( __DIR__ ) . 'payzeep/assets/zeep-icon.png', // // plugins_url( 'payzeep/assets/zeep-icon.png', __FILE__ ). // 56 // ); } public function render_settings_page() { if ($this->woocommerce_active()) { return; } ?> <div class="wrap"> <div class="notice notice-info"> <p>Please sign-up and login to the <a href="https://portal.zeepway.com/">Payzeep</a> merchant portal to get you keys and set the callback url</p> </div> <h1>Payzeep Config Settings <?php echo class_exists('WooCommerce') ?></h1> <form method="post" action="options.php"> <?php settings_fields('pzee_settings_group'); do_settings_sections('pzee-settings'); submit_button(); ?> </form> </div> <?php } public function register_settings() { register_setting('pzee_settings_group', 'pzee_footer_message', [ 'sanitize_callback' => 'sanitize_text_field',]); register_setting('pzee_settings_group', 'pzee_btn_pubtest', [ 'sanitize_callback' => 'sanitize_text_field',]); register_setting('pzee_settings_group', 'pzee_btn_sectest', [ 'sanitize_callback' => 'sanitize_text_field',]); register_setting('pzee_settings_group', 'pzee_btn_publive', [ 'sanitize_callback' => 'sanitize_text_field',]); register_setting('pzee_settings_group', 'pzee_btn_seclive', [ 'sanitize_callback' => 'sanitize_text_field',]); register_setting('pzee_settings_group', 'pzee_btn_mode', [ 'sanitize_callback' => 'sanitize_text_field',]); register_setting('pzee_settings_group', 'pzee_btn_feebearer', [ 'sanitize_callback' => 'sanitize_text_field',]); register_setting('pzee_settings_group', 'zeep_redirect', [ 'sanitize_callback' => 'sanitize_text_field',]); add_settings_section('pzee_main_section', 'API Keys and Environment Settings', null, 'pzee-settings'); add_settings_field( 'pzee_btn_mode', 'Environment', [$this, 'btn_mode_key_callback'], 'pzee-settings', 'pzee_main_section' ); add_settings_field( 'pzee_btn_pubtest', 'Test Public Key', [$this, 'btn_test_pub_key_callback'], 'pzee-settings', 'pzee_main_section' ); add_settings_field( 'pzee_btn_sectest', 'Test Secrete Key', [$this, 'btn_test_sec_key_callback'], 'pzee-settings', 'pzee_main_section' ); add_settings_field( 'pzee_btn_publive', 'Live Public Key', [$this, 'btn_live_pub_key_callback'], 'pzee-settings', 'pzee_main_section' ); add_settings_field( 'pzee_btn_seclive', 'Live Secrete Key', [$this, 'btn_live_sec_key_callback'], 'pzee-settings', 'pzee_main_section' ); add_settings_field( 'pzee_btn_feebearer', 'Fee Bearer', [$this, 'btn_feebearer_callback'], 'pzee-settings', 'pzee_main_section' ); // add_settings_field( // 'zeep_redirect', // 'Payzeep Checkout Page', // [$this, 'zeep_page_url'], // 'pzee-settings', // 'pzee_main_section' // ); add_settings_field( 'pzee_btn_callback', 'Callback URL', [$this, 'redirect_page_url_callback'], 'pzee-settings', 'pzee_main_section' ); add_settings_field( 'pzee_btn_webhook', 'Webhook URL', [$this, 'zeep_page_url_webhook'], 'pzee-settings', 'pzee_main_section' ); } public function zeep_page_url_webhook() { $page_id = get_option('zeep_redirect_id'); $page = get_post($page_id); echo '<p style="font-weight:bold; color:red">' . esc_url( rest_url('payzeep-pg/webhook') ) . '</p>'; } public function redirect_page_url_callback() { $page_id = get_option('zeep_redirect_id'); $page = get_post($page_id); echo '<p style="font-weight:bold; color:red">' . get_permalink($page->ID) . '</p>'; } public function zeep_page_url() { $current_value = get_option('zeep_redirect', ''); $pages = get_pages(); $options = []; foreach ($pages as $page) { $options[$page->ID] = $page->post_title; } $value = get_option('zeep_redirect'); ?> <select name="zeep_redirect" class="select wc-page-search" style="width: 400px;"> <?php foreach ((array) $options as $option_key => $option_value) : ?> <?php if (is_array($option_value)) : ?> <optgroup label="<?php echo esc_attr($option_key); ?>"> <?php foreach ($option_value as $option_key_inner => $option_value_inner) : ?> <option value="<?php echo esc_attr($option_key_inner); ?>" <?php selected($current_value, esc_attr($value)); ?>><?php echo esc_html($option_value_inner); ?></option> <?php endforeach; ?> </optgroup> <?php else : ?> <option value="<?php echo esc_attr($option_key); ?>" <?php selected($current_value, esc_attr($value)); ?>><?php echo esc_html($option_value); ?></option> <?php endif; ?> <?php endforeach; ?> </select> <?php // // $pages = get_pages(); // $options = []; // foreach ($pages as $page) { // $options[$page->ID] = $page->post_title; // } } public function btn_test_pub_key_callback() { $value = get_option('pzee_btn_pubtest', ''); echo '<input type="text" name="pzee_btn_pubtest" value="' . esc_attr($value) . '" style="width: 400px;" />'; } public function btn_test_sec_key_callback() { $value = get_option('pzee_btn_sectest', ''); echo '<input type="text" name="pzee_btn_sectest" value="' . esc_attr($value) . '" style="width: 400px;" />'; } public function btn_live_pub_key_callback() { $value = get_option('pzee_btn_publive', ''); echo '<input type="text" name="pzee_btn_publive" value="' . esc_attr($value) . '" style="width: 400px;" />'; } public function btn_live_sec_key_callback() { $value = get_option('pzee_btn_seclive', ''); echo '<input type="text" name="pzee_btn_seclive" value="' . esc_attr($value) . '" style="width: 400px;" />'; } public function btn_mode_key_callback() { $value = get_option('pzee_btn_mode'); ?> <select name="pzee_btn_mode" style="width: 400px;"> <option value="test" <?php selected($value, 'test'); ?>>Test Mode</option> <option value="live" <?php selected($value, 'live'); ?>>Live Mode</option> </select> <?php } public function btn_feebearer_callback() { $value = get_option('pzee_btn_feebearer'); ?> <select name="pzee_btn_feebearer" style="width: 400px;"> <option value="customer" <?php selected($value, 'customer'); ?>>Customer</option> <option value="merchant" <?php selected($value, 'merchant'); ?>>Merchant</option> </select> <?php } public function render_shortcode_button() { ob_start(); ?> <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>"> <?php wp_nonce_field('pzee_front_button_action'); ?> <input type="hidden" name="action" value="pzee_front_button_click" /> <button type="submit">Click Me (Frontend)</button> </form> <?php return ob_get_clean(); } public function handle_front_button_click() { if (!check_admin_referer('pzee_front_button_action')) { wp_die('Invalid request'); } // Logic to run when button is clicked update_option('pzee_last_front_click', current_time('mysql')); wp_redirect(home_url('/thank-you')); exit; } public function render_payment_form_shortcode() { $key = get_option('pzee_btn_mode') == 'live' ? get_option('pzee_btn_publive', '') : get_option('pzee_btn_pubtest', ''); $feeBearer = get_option('pzee_btn_feebearer', '') != 'merchant'; ob_start(); ?> <!-- <link rel="stylesheet" href="https://paymi-tech.github.io/zeepway-checkout-plugin/checkout-modal.min.css"> <script src="https://paymi-tech.github.io/zeepway-checkout-plugin/checkout-modal.min.js"> </script> --> <style> .pzee-payment-form { max-width: 400px; margin: 2em auto; padding: 20px; border: 1px solid #ddd; border-radius: 10px; background-color: #fafafa; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05); font-family: Arial, sans-serif; } .pzee-payment-form h3 { margin-bottom: 1em; text-align: center; color: #333; } .pzee-payment-form label { display: block; margin-bottom: 0.3em; font-weight: bold; } .pzee-form-group { text-align: left; margin-bottom: 1em; margin-right: 0.5em; margin-left: 0.5em; } .pzee-form-group label { display: block; margin-bottom: 0.3em; font-weight: bold; } .pzee-form-group input { width: 100%; padding: 10px; display: block; border: 1px solid #ccc; border-radius: 5px; font-size: 14px; } .pzee-payment-form input[type="text"], .pzee-payment-form input[type="email"], .pzee-payment-form input[type="tel"], .pzee-payment-form input[type="number"] { width: 100%; padding: 10px; margin-bottom: 1em; border: 1px solid #ccc; border-radius: 5px; font-size: 14px; display: block; margin-left: auto; margin-right: auto; } .pzee-payment-form button { width: 100%; padding: 12px; background-color: #0073aa; color: #fff; border: none; border-radius: 6px; font-size: 16px; cursor: pointer; } .pzee-payment-form button:hover { background-color: #005f8d; } </style> <form id="pzee-payment-form" class="pzee-payment-form"> <!-- <form class="pzee-payment-form" method="post" action="<php echo esc_url(admin_url('admin-post.php')); ?>"> --> <!-- <?php wp_nonce_field('pzee_payment_action'); ?> --> <input type="hidden" name="action" value="pzee_process_payment" /> <h3>Pay Invoice</h3> <div class="pzee-form-group"> <label for="firstname">Full Name</label> <input type="text" name="fullname" id="fullname" required placeholder="John Doe"> </div> <!-- <div class="pzee-form-group"> <label for="lastname">Last Name</label> <input type="text" name="lastname" required placeholder="John Doe"> </div> --> <div class="pzee-form-group"> <label for="email">Email</label> <input type="email" name="email" id="email" required placeholder="you@example.com"> </div> <div class="pzee-form-group"> <label for="phone">Phone</label> <input type="tel" name="phone" id="phone" required placeholder="08012345678"> </div> <div class="pzee-form-group"> <label for="amount">Amount (NGN)</label> <input type="number" step="0.01" id="amount" name="amount" required placeholder="0.00"> </div> <div class="pzee-form-group"> <label for="invoiceid">Description/Service</label> <input type="text" name="narration" id="narration" required placeholder="e.g. Purpose of payment"> </div> <input type="hidden" name="apikey" id="apikey" value="<?php echo esc_attr($key); ?>"> <input type="hidden" name="feebearer" id="feebearer" value="<?php echo esc_attr($feeBearer); ?>"> <button type="submit">Pay Now</button> <!-- <button type="button" onclick="openCheckout()">Pay Now</button> --> </form> <script> document.getElementById("pzee-payment-form").addEventListener("submit", function(e) { e.preventDefault(); // if (typeof CheckoutModal === "undefined") { // alert("CheckoutModal is not loaded yet. Please try again."); // return; // } // Extract values const feebearer = document.getElementById("feebearer").value; const apikey = document.getElementById("apikey").value; const name = document.getElementById("fullname").value; const email = document.getElementById("email").value; const phone = document.getElementById("phone").value; const amount = parseFloat(document.getElementById("amount").value); // * 100; // convert to smallest currency unit console.log(name + email + amount); // Initialize and open the modal // const checkout = new CheckoutModal(); const checkout = window.Checkout({ key: apikey, // Replace with your API key TEST_PUK_tstkdlsflksdjflkdsjfklsd" amount: amount, // Amount in the smallest currency unit (e.g., cents) customer: { email: email, name: name, phone: phone, }, // email: email, // Customer email // name: name, // Customer name reference: `fm_ref_${Date.now()}`, // Unique client reference chargeUser: feebearer, // Whether to charge the user onSuccess: (data) => { alert("Payment Success! Transaction ID: " + data.transactionId); }, onClose: () => { alert("Checkout Closed"); }, }); checkout.setup(); return checkout.open(); }); </script> <?php return ob_get_clean(); } public function x_render_payment_form_shortcode() { ob_start(); ?> <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>"> <?php wp_nonce_field('pzee_payment_action'); ?> <input type="hidden" name="action" value="pzee_process_payment" /> <p> <label>First Name:</label><br> <input type="text" name="firstname" required> </p> <p> <label>Last Name:</label><br> <input type="text" name="lastname" required> </p> <p> <label>Email:</label><br> <input type="email" name="email" required> </p> <p> <label>Phone:</label><br> <input type="tel" name="phone" required> </p> <p> <label>Amount (NGN):</label><br> <input type="number" step="0.01" name="amount" required> </p> <p> <label>Description/Service:</label><br> <input type="text" name="narration" required> </p> <!-- <p> <label>Invoice ID:</label><br> <input type="text" name="invoiceid" required> </p> --> <button type="submit">Pay Now</button> </form> <?php return ob_get_clean(); } public function handle_payment_form() { if (!check_admin_referer('pzee_payment_action')) { wp_die('Invalid request.'); } $invoiceId = sanitize_text_field($_POST['invoiceid'] ?? ''); $invoice = (object)['hash' => 'fakehash', 'clientid' => 1]; // Replace with real data $data = [ 'invoiceid' => $invoiceId, 'amount' => 5000, 'invoice' => $invoice, ]; $this->process_payment($data); exit; } /** * REQUIRED FUNCTION * @param array $data * @return mixed */ public function process_payment($data) { $refere = time(); $reference = str_replace('/', '', $refere); $calurl = site_url('pzeepcall/verify?invoiceid=' . $data['invoiceid'] . '&hash=' . $data['invoice']->hash); $email = $data['email']; $phone = $data['phone']; $pamount = number_format($data['amount'], 2, '.', ''); // $fullname = explode(' ', $data['fullname']); // $firstName = $fullname[0] ?? ''; // $lastName = $fullname[1] ?? ''; $firstName = $data['firstname']; $lastName = $$data['lastname']; $narration = $$data['narration']; $pubKey = "live_YzM1ODg3YzY5MWVjZjFlYzhkOTQxMDU3NmMzM2NlYjc4YzQwYTU1M2ZkZjRmNjI5ZjQzOGQzZmM4ZmY3NzZmYQ"; echo "<script src=\"https://unpkg.com/react@18/umd/react.production.min.js\"></script>"; echo "<script src=\"https://unpkg.com/react-dom@18/umd/react-dom.production.min.js\"></script>"; echo "<link rel=\"stylesheet\" href=\"https://paymi-tech.github.io/zeepway-checkout-plugin/checkout-modal.min.css\">\n"; echo "<script src=\"https://paymi-tech.github.io/zeepway-checkout-plugin/checkout-modal.min.js\"> </script>\n"; echo "<script> const checkout = new CheckoutModal(); checkout.open({ apiKey: \"TEST_PUK_\", // Replace with your API key amount: 5000, // Amount in the smallest currency unit (e.g., cents) email: \"test@example.com\", // Customer email name: \"John Doe\", // Customer name clientRef: `test_ref_4444444444444`, // Unique client reference chargeUser: true, // Whether to charge the user onSuccess: (data) => { alert(\"Payment Success! Transaction ID: \" + data.transactionId); }, onClose: () => { alert(\"Checkout Closed\"); }, }); </script>"; } public function handle_custom_pg_payment($booking, $request) { $key = get_option('pzee_btn_mode') == 'live' ? get_option('pzee_btn_publive', '') : get_option('pzee_btn_pubtest', ''); ob_start(); ?> <form> <input type="hidden" name="apikey" id="apikey" value="<?php echo esc_attr($key); ?>"> </form> <script> // Extract values const apikey = document.getElementById("apikey").value; const name = document.getElementById("fullname").value; const email = document.getElementById("email").value; const phone = document.getElementById("phone").value; const amount = parseFloat(document.getElementById("amount").value); // * 100; // convert to smallest currency unit console.log(); console.log(); console.log(name + email + amount); // Initialize and open the modal // const checkout = new CheckoutModal(); const checkout = window.Checkout({ // apiKey: "", // Replace with your API key key: apikey, // Replace with your API key TEST_PUK_tstkdlsflksdjflkdsjfklsd" amount: amount, // Amount in the smallest currency unit (e.g., cents) customer: { email: email, name: name, phone: phone, }, // email: email, // Customer email // name: name, // Customer name reference: `test_ref_${Date.now()}`, // Unique client reference chargeUser: true, // Whether to charge the user onSuccess: (data) => { alert("Payment Success! Transaction ID: " + data.transactionId); }, onClose: () => { alert("Checkout Closed"); }, }); checkout.setup(); return checkout.open(); </script> <?php return ob_get_clean(); } public function render_payment_redirect_shortcode() { if (!isset( $_GET['currency_code'], $_GET['return'], $_GET['invoice'], $_GET['country'], $_GET['email'], $_GET['first_name'], $_GET['last_name'], $_GET['item_name'], $_GET['amount'] )) { if (isset($_GET['reference'])) { $payment = $this->verify_payment($_GET['reference']); if ($payment == null) { error_log('Request failed: verify failed'); wc_add_notice('Payment not found', 'error'); wp_safe_redirect(wc_get_checkout_url()); exit; } else { if ($payment['transaction']['status'] == 'SUCCESSFUL') { $order_id = intval($payment['transaction']['client_ref']); $order = wc_get_order($order_id); if (!$order) { // wp_send_json_error(['message' => 'Order not found']); // wc_add_notice('Something went wrong', 'error'); wc_add_notice('Order not found', 'error'); wp_safe_redirect(wc_get_checkout_url()); exit; } $order->payment_complete(); $order->add_order_note('Payment confirmed via Payzeep.'); // wp_send_json_success(['redirect' => $order->get_checkout_order_received_url()]); wp_safe_redirect($order->get_checkout_order_received_url()); exit; } else { wc_add_notice('Payment failed', 'error'); wp_safe_redirect(wc_get_checkout_url()); exit; } } } } $name = $_GET['first_name'] . ' ' . $_GET['last_name']; $email = $_GET['email']; $amount = $_GET['amount']; $invoice = $_GET['invoice']; $item = $_GET['item_name']; // $phone = $_GET['phone']; $cancel = $_GET['return']; $key = get_option('pzee_btn_mode') == 'live' ? get_option('pzee_btn_publive', '') : get_option('pzee_btn_pubtest', ''); $feeBearer = get_option('pzee_btn_feebearer', '') != 'merchant'; ob_start(); ?> <!-- <p><?php echo esc_attr($key); ?></p> --> <!-- Loader --> <div id="zeep-loader"> <div class="spinner"></div> <p>Loading, please wait…</p> </div> <form> <!-- <form class="pzee-payment-form" method="post" action="<php echo esc_url(admin_url('admin-post.php')); ?>"> --> <!-- < wp_nonce_field('pzee_payment_action'); ?> --> <!-- <input type="hidden" name="complete" id="complete" required value="<?php echo esc_attr($cancel); ?>"> --> <input type="hidden" name="cancel" id="cancel" required value="<?php echo esc_attr($cancel); ?>"> <input type="hidden" name="fullname" id="fullname" required value="<?php echo esc_attr($name); ?>"> <input type="hidden" name="email" id="email" required value="<?php echo esc_attr($email); ?>"> <!-- <input type="hidden" name="phone" id="phone" required value="< echo esc_attr($phone); ?>"> --> <input type="hidden" step="0.01" id="amount" name="amount" required value="<?php echo esc_attr($amount); ?>"> <input type="hidden" id="invoice" name="invoice" required value="<?php echo esc_attr($invoice); ?>"> <input type="hidden" name="narration" id="narration" required value="<?php echo esc_attr($item); ?>"> <input type="hidden" name="apikey" id="apikey" value="<?php echo esc_attr($key); ?>"> <input type="hidden" name="feebearer" id="feebearer" value="<?php echo esc_attr($feeBearer); ?>"> <!-- <button type="submit">Pay Now</button> --> <!-- <button type="button" onclick="openCheckout()">Pay Now</button> --> </form> <script> // document.getElementById("pzee-payment-form").addEventListener("submit", function (e) { // e.preventDefault(); document.addEventListener("DOMContentLoaded", function() { console.log("script exec started...!"); // const complete = document.getElementById("complete").value; const cancel = document.getElementById("cancel").value; const apikey = document.getElementById("apikey").value; const feebearer = document.getElementById("feebearer").value; const name = document.getElementById("fullname").value; const email = document.getElementById("email").value; const invoice = document.getElementById("invoice").value; const naration = document.getElementById("narration").value; const phone = '909999999999'; const amount = parseFloat(document.getElementById("amount").value); // * 100; // convert to smallest currency unit let payStatus = 'Pending'; console.log(name + email + amount); console.log(invoice); // Initialize and open the modal // const checkout = new CheckoutModal(); const checkout = window.Checkout({ // apiKey: "", // Replace with your API key key: apikey, // Replace with your API key TEST_PUK_tstkdlsflksdjflkdsjfklsd" amount: amount, // Amount in the smallest currency unit (e.g., cents) customer: { email: email, name: name, phone: phone, }, reference: invoice, chargeUser: feebearer, // Whether to charge the user onFailure: (data) => { // window.location.replace(cancel); if (payStatus !== 'SUCCESSFUL') { window.history.back(); } }, onClose: () => { document.getElementById('zeep-loader').style.display = 'flex'; // window.location.replace(cancel); // console.log(payStatus); if (payStatus !== 'SUCCESSFUL') { window.history.back(); } }, onSuccess: (data) => { // alert("Payment Success! Transaction ID: " + data.transaction.Id); // alert("Payment Success! Transaction ID: " + JSON.stringify(data)); // payStatus = data.status; payStatus = data.transaction.status; // console.log(cancel); console.log(JSON.stringify(data)); console.log(data.transaction.status); //client_ref const formData = new FormData(); formData.append('action', 'payzeep_gateway_payment_confirm'); formData.append('order_id', invoice); formData.append('payment_ref', data.transaction.reference); formData.append('_ajax_nonce', '<?php echo wp_create_nonce('payzeep_gateway_nonce'); ?>'); // fetch('<?php //echo esc_url(admin_url('admin-post.php')); ?>', { fetch('<?php echo admin_url('admin-ajax.php'); ?>', { method: 'POST', body: formData, credentials: 'same-origin' }) .then(response => response.json()) .then(data => { console.error('Payment confirmation data:', data); if (data.success && data.data.redirect) { window.location.replace(data.data.redirect); // console.error('Redirecting to:', data); } else { console.error('Payment confirmation failed:', data); } }) .catch(error => console.error('AJAX error:', error.toString())); }, }); checkout.setup(); document.getElementById('zeep-loader').style.display = 'none'; return checkout.open(); }); </script> <!-- </body> </html> --> <?php return ob_get_clean(); } public function handle_payzeep_gateway_payment_confirm() { try { check_ajax_referer('payzeep_gateway_nonce'); if (!isset($_POST['order_id'])) { wp_send_json_error(array('message' => 'No order ID.')); } if (!isset($_POST['payment_ref'])) { wp_send_json_error(array('message' => 'Payment not found.')); } $order_id = intval($_POST['order_id']); $payment = $this->verify_payment($_POST['payment_ref']); if ($payment != null) { if ($payment['transaction']['status'] == 'SUCCESSFUL') { $order = wc_get_order($order_id); if ($order && $order->has_status('on-hold')) { $order->payment_complete(); $order->add_order_note('Payment confirmed via Payzeep.'); wp_send_json_success(array( // 'message' => $order 'redirect' => $order->get_checkout_order_received_url() )); } else { wp_send_json_error(array('message' => 'Order not found. Please contact support.')); } } else { wp_send_json_error(array('message' => 'Payment failed.')); } } else { wp_send_json_error(array('message' => 'Payment not found.')); } } catch (\Throwable $th) { throw $th; // wc_add_notice($th, 'error'); } // if (!isset($_POST['order_id']) || empty($_POST['order_id'])) { // wp_send_json_error(['message' => 'Missing order ID']); // wp_die(); // } // $order_id = intval($_POST['order_id']); // $order = wc_get_order($order_id); // if (!$order) { // wp_send_json_error(['message' => 'Order not found']); // wp_die(); // } // // Verify nonce here if sent and required // $order->payment_complete(); // $order->add_order_note('Payment confirmed via Payzeep.'); // wp_send_json_success(['redirect' => $order->get_checkout_order_received_url()]); // wp_die(); } public function pzeep_register_webhook_route() { register_rest_route('payzeep-pg', '/webhook', [ 'methods' => 'POST', 'callback' => [$this, 'handle_webhook'], 'permission_callback' => '__return_true', ]); } public function handle_webhook(WP_REST_Request $request) { $data = $request->get_json_params(); if ( isset($data['status']) && $data['status'] === 'SUCCESSFUL' ) { $payment = $this->verify_payment($data['reference']); if ($payment != null) { if ($payment['transaction']['status'] == 'SUCCESSFUL') { $order_id = intval($payment['transaction']['client_ref']); $order = wc_get_order($order_id); if ($order) { $order->payment_complete(); $order->add_order_note('Payment confirmed via Payzeep.'); } } } } return new WP_REST_Response(['status' => 'ok', 'received' => $data], 200); } public function verify_payment(string $reference) { $key = get_option('pzee_btn_mode') == 'live' ? get_option('pzee_btn_seclive', '') : get_option('pzee_btn_sectest', ''); $response = wp_remote_get('https://merchant-api.zeepway.com/v1/transactions/query?reference=' . $reference, [ 'headers' => [ 'Authorization' => 'Bearer ' . $key, 'Content-Type' => 'application/json' ], 'timeout' => 30, ]); if (is_wp_error($response)) { error_log('Request failed: ' . $response->get_error_message()); var_dump($response); return null; } else { $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); return $data; } } public function handle_payzeep_gateway_payment_redirect() { try { // if (!check_admin_referer('pzee_payment_action')) { // wp_die('Invalid request.'); // } check_ajax_referer('payzeep_gateway_nonce'); if (!isset($_POST['order_id'])) { wp_send_json_error(array('message' => 'No order ID.')); } $order_id = intval($_POST['order_id']); $order = wc_get_order($order_id); if ($order && $order->has_status('on-hold')) { $order->payment_complete(); $order->add_order_note('Payment confirmed via Payzeep.'); wp_send_json_success(array( // 'message' => $order 'redirect' => $order->get_checkout_order_received_url() )); } else { wp_send_json_error(array('message' => 'Order not found. Please contact support.')); } } catch (\Throwable $th) { throw $th; // wc_add_notice($th, 'error'); } // if (!isset($_POST['order_id']) || empty($_POST['order_id'])) { // wp_send_json_error(['message' => 'Missing order ID']); // wp_die(); // } // $order_id = intval($_POST['order_id']); // $order = wc_get_order($order_id); // if (!$order) { // wp_send_json_error(['message' => 'Order not found']); // wp_die(); // } // // Verify nonce here if sent and required // $order->payment_complete(); // $order->add_order_note('Payment confirmed via Payzeep.'); // wp_send_json_success(['redirect' => $order->get_checkout_order_received_url()]); // wp_die(); } public static function zeep_check_woocommerce_activation($plugin, $network_wide) { if ($plugin === 'woocommerce/woocommerce.php') { // WooCommerce has just been activated // $this->copy_zeep_options_to_woocommerce_settings(); $wc_gateway_option_key = 'woocommerce_pzeep_settings'; try { if (class_exists('WooCommerce')) { $wcgateway_settings = get_option($wc_gateway_option_key, []); $wcgateway_settings['test_pk'] = get_option('pzee_btn_pubtest', ''); $wcgateway_settings['live_pk'] = get_option('pzee_btn_publive', ''); $wcgateway_settings['test_sk'] = get_option('pzee_btn_sectest', ''); $wcgateway_settings['live_sk'] = get_option('pzee_btn_seclive', ''); $wcgateway_settings['environment'] = get_option('pzee_btn_mode', ''); $wcgateway_settings['feebearer'] = get_option('pzee_btn_feebearer', ''); // var_dump($wcgateway_settings); update_option($wc_gateway_option_key, $wcgateway_settings); } } catch (\Throwable $th) { var_dump($th); // throw $th; } } } public function copy_zeep_options_to_woocommerce_settings() { try { if ($this->woocommerce_active()) { $wcgateway_settings = get_option($this->wc_gateway_option_key(), []); $wcgateway_settings['test_pk'] = get_option('pzee_btn_pubtest', ''); $wcgateway_settings['live_pk'] = get_option('pzee_btn_publive', ''); $wcgateway_settings['test_sk'] = get_option('pzee_btn_sectest', ''); $wcgateway_settings['live_sk'] = get_option('pzee_btn_seclive', ''); $wcgateway_settings['environment'] = get_option('pzee_btn_mode', ''); $wcgateway_settings['feebearer'] = get_option('pzee_btn_feebearer', ''); var_dump($wcgateway_settings); update_option($this->wc_gateway_option_key(), $wcgateway_settings); } } catch (\Throwable $th) { var_dump($th); // throw $th; } } private function woocommerce_active() { return class_exists('WooCommerce'); } private function wc_gateway_option_key() { return 'woocommerce_pzeep_settings'; } }
Save File
Cancel