Plugin Name: WooCommerce QR Code Payment Gateway

id = 'qr_code_payment'; $this->icon = ''; $this->has_fields = true; $this->method_title = 'QR Code Payment'; $this->method_description = 'Allows payments with QR Code, Google Pay, PhonePe, and Paytm'; $this->init_form_fields(); $this->init_settings(); $this->title = $this->get_option('title'); $this->description = $this->get_option('description'); add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); add_action('woocommerce_thankyou_' . $this->id, array($this, 'thankyou_page')); add_action('wp_enqueue_scripts', array($this, 'payment_scripts')); add_action('woocommerce_email_before_order_table', array($this, 'email_transaction_id'), 10, 4); } public function init_form_fields() { $this->form_fields = array( 'enabled' => array( 'title' => 'Enable/Disable', 'type' => 'checkbox', 'label' => 'Enable QR Code Payment Gateway', 'default' => 'yes' ), 'title' => array( 'title' => 'Title', 'type' => 'text', 'description' => 'This controls the title which the user sees during checkout.', 'default' => 'QR Code Payment', 'desc_tip' => true, ), 'description' => array( 'title' => 'Description', 'type' => 'textarea', 'description' => 'Payment method description that the customer will see on your checkout.', 'default' => 'Pay with a QR code, Google Pay, PhonePe, or Paytm.', ), 'upi_id' => array( 'title' => 'UPI ID', 'type' => 'text', 'description' => 'Enter your UPI ID.', 'default' => '', 'desc_tip' => true, ), 'name' => array( 'title' => 'Name', 'type' => 'text', 'description' => 'Enter your name.', 'default' => '', 'desc_tip' => true, ), 'email' => array( 'title' => 'Email', 'type' => 'text', 'description' => 'Enter your email address to receive payment transaction IDs.', 'default' => '', 'desc_tip' => true, ), 'enable_gpay' => array( 'title' => 'Enable Google Pay', 'type' => 'checkbox', 'label' => 'Enable Google Pay button on checkout', 'default' => 'yes', ), 'enable_phonepe' => array( 'title' => 'Enable PhonePe', 'type' => 'checkbox', 'label' => 'Enable PhonePe button on checkout', 'default' => 'yes', ), 'enable_paytm' => array( 'title' => 'Enable Paytm', 'type' => 'checkbox', 'label' => 'Enable Paytm button on checkout', 'default' => 'yes', ), 'gpay_icon' => array( 'title' => 'Google Pay Icon URL', 'type' => 'text', 'description' => 'Enter the URL of the Google Pay button icon.', 'default' => '', 'desc_tip' => true, ), 'phonepe_icon' => array( 'title' => 'PhonePe Icon URL', 'type' => 'text', 'description' => 'Enter the URL of the PhonePe button icon.', 'default' => '', 'desc_tip' => true, ), 'paytm_icon' => array( 'title' => 'Paytm Icon URL', 'type' => 'text', 'description' => 'Enter the URL of the Paytm button icon.', 'default' => '', 'desc_tip' => true, ), ); } public function payment_fields() { if ($this->description) { echo wpautop(wp_kses_post($this->description)); } echo '
'; echo '

Scan the QR code below to make the payment.

'; $amount = WC()->cart->total; $qr_code_url = $this->generate_qr_code($amount); echo '
'; echo 'QR Code'; echo '
'; echo '
'; echo '
'; echo '

Or Pay with:

'; if ($this->get_option('enable_gpay') === 'yes') { $gpay_icon = $this->get_option('gpay_icon'); echo ''; } if ($this->get_option('enable_phonepe') === 'yes') { $phonepe_icon = $this->get_option('phonepe_icon'); echo ''; } if ($this->get_option('enable_paytm') === 'yes') { $paytm_icon = $this->get_option('paytm_icon'); echo ''; } echo '
'; echo '
'; echo '

Enter Transaction ID:

'; echo ''; echo '
'; } public function payment_scripts() { if (!is_checkout()) { return; } wp_enqueue_script('qr-code-payment-script', plugin_dir_url(__FILE__) . 'payment-script.js', array('jquery'), '1.0.0', true); wp_localize_script('qr-code-payment-script', 'wcqr_options', array( 'upi_id' => $this->get_option('upi_id'), 'name' => $this->get_option('name'), 'ajax_url' => admin_url('admin-ajax.php') )); } public function generate_qr_code($amount) { $upi_id = $this->get_option('upi_id'); $name = $this->get_option('name'); $upi_url = "upi://pay?pa=$upi_id&pn=$name&am=$amount&cu=INR"; $qr_code_url = "https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=" . urlencode($upi_url); return $qr_code_url; } public function generate_google_pay_link($amount) { $upi_id = $this->get_option('upi_id'); $name = $this->get_option('name'); return "intent://pay?pa=$upi_id&pn=$name&am=$amount&cu=INR#Intent;scheme=upi;package=com.google.android.apps.nbu.paisa.user;end"; } public function generate_phonepe_link($amount) { $upi_id = $this->get_option('upi_id'); $name = $this->get_option('name'); return "intent://pay?pa=$upi_id&pn=$name&am=$amount&cu=INR#Intent;scheme=upi;package=com.phonepe.app;end"; } public function generate_paytm_link($amount) { $upi_id = $this->get_option('upi_id'); $name = $this->get_option('name'); return "intent://pay?pa=$upi_id&pn=$name&am=$amount&cu=INR#Intent;scheme=upi;package=net.one97.paytm;end"; } public function process_payment($order_id) { $order = wc_get_order($order_id); $transaction_id = sanitize_text_field($_POST['transaction_id']); if (empty($transaction_id)) { wc_add_notice('Please enter a transaction ID.', 'error'); return; } if (strlen($transaction_id) < 12) { wc_add_notice('The transaction ID must be at least 12 characters long.', 'error'); return; } $order->update_meta_data('_transaction_id', $transaction_id); $order->save(); $email = $this->get_option('email'); $subject = 'New Payment Transaction ID'; $body = 'A new payment has been made with the following transaction ID: ' . $transaction_id; $headers = array('Content-Type: text/html; charset=UTF-8'); wp_mail($email, $subject, $body, $headers); $order->payment_complete(); $order->reduce_order_stock(); WC()->cart->empty_cart(); return array( 'result' => 'success', 'redirect' => $this->get_return_url($order) ); } public function email_transaction_id($order, $sent_to_admin, $plain_text, $email) { if ($order->get_payment_method() === $this->id) { $transaction_id = $order->get_meta('_transaction_id'); if ($transaction_id) { if ($plain_text) { echo "\nTransaction ID: " . $transaction_id . "\n"; } else { echo '

Transaction ID: ' . $transaction_id . '

'; } } } } } function add_wc_qr_code_payment_gateway($methods) { $methods[] = 'WC_QR_Code_Payment_Gateway'; return $methods; } add_filter('woocommerce_payment_gateways', 'add_wc_qr_code_payment_gateway'); } }

Popular posts from this blog