almahmudbd

upay gateway page

Jul 16th, 2023
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 17.77 KB | None | 0 0
  1. <?php // @codingStandardsIgnoreLine
  2. /**
  3. * Upay Functionality.
  4. *
  5. * @package BDPaymentGateways
  6. * @since   1.0.0
  7. */
  8. namespace ultraDevs\BDPG\Gateways;
  9.  
  10. use ultraDevs\BDPG\Helper;
  11. /**
  12. * Upay class.
  13. *
  14. * @package BDPaymentGateways
  15. * @since   1.0.0
  16. */
  17. class Upay extends \WC_Payment_Gateway {
  18.  
  19.     /**
  20.      * Class constructor
  21.      */
  22.     public function __construct() {
  23.  
  24.         $this->id                 = 'woo_upay';
  25.         $this->icon               = apply_filters( 'bdpg_upay_icon', BD_PAYMENT_GATEWAYS_DIR_URL . 'assets/images/gateways.jpg' );
  26.         $this->has_fields         = true;
  27.         $this->method_description = __( 'Multiple Payment Gateway Settings.', 'bangladeshi-payment-gateways' );
  28.         $this->method_title       = __( 'bKash/Rocket/Nagad Multiple', 'bangladeshi-payment-gateways' );
  29.  
  30.         $this->init_form_fields();
  31.  
  32.         // Load the Settings.
  33.         $this->init_settings();
  34.         $this->title               = $this->get_option( 'title' );
  35.         $this->description         = $this->get_option( 'description' );
  36.         $this->enabled             = $this->get_option( 'enabled' );
  37.         $this->instructions        = $this->get_option( 'instructions' );
  38.         $this->upay_charge         = $this->get_option( 'upay_charge' );
  39.         $this->upay_fee            = $this->get_option( 'upay_fee' );
  40.         $this->upay_charge_details = $this->get_option( 'upay_charge_details' );
  41.  
  42.         $this->all_account = array(
  43.             array(
  44.                 'type'    => $this->get_option( 'type' ),
  45.                 'number'  => $this->get_option( 'number' ),
  46.                 'qr_code' => $this->get_option( 'qr_code' ),
  47.             ),
  48.         );
  49.         $this->accounts = get_option( 'bdpg_upay_accounts', $this->all_account );
  50.  
  51.         add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
  52.         add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'save_accounts' ) );
  53.         add_action( 'woocommerce_thankyou_woo_upay', array( $this, 'bdpg_thankyou' ) );
  54.         add_action( 'woocommerce_email_before_order_table', array( $this, 'customer_email_instructions' ), 10, 3 );
  55.  
  56.         add_action( 'woocommerce_checkout_process', array( $this, 'payment_process' ) );
  57.         add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'fields_update' ) );
  58.         add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'admin_order_data' ) );
  59.  
  60.         $upay_settings = get_option( 'woocommerce_woo_upay_settings' );
  61.         if ( isset( $upay_settings['upay_charge'] ) && 'yes' === $upay_settings['upay_charge'] ) {
  62.             add_action( 'woocommerce_cart_calculate_fees', array( $this, 'charge_settings' ), 20, 1 );
  63.         }
  64.  
  65.         add_action( 'woocommerce_order_details_after_customer_details', array( $this, 'data_order_review_page' ) );
  66.         add_filter( 'manage_edit-shop_order_columns', array( $this, 'admin_register_column' ) );
  67.         add_action( 'manage_shop_order_posts_custom_column', array( $this, 'admin_column_value' ), 2 );
  68.     }
  69.  
  70.     /**
  71.      * Gateway Fields
  72.      */
  73.     public function init_form_fields() {
  74.         $this->form_fields = array(
  75.             'enabled'             => array(
  76.                 'title'       => __( 'Enable/Disable', 'bangladeshi-payment-gateways' ),
  77.                 'label'       => __( 'Enable Multiple Gateway', 'bangladeshi-payment-gateways' ),
  78.                 'type'        => 'checkbox',
  79.                 'description' => '',
  80.                 'default'     => 'no',
  81.             ),
  82.             'title'               => array(
  83.                 'title'       => __( 'Title', 'bangladeshi-payment-gateways' ),
  84.                 'type'        => 'text',
  85.                 'default'     => 'Upay',
  86.                 'description' => __( 'Title', 'bangladeshi-payment-gateways' ),
  87.                 'desc_tip'    => true,
  88.             ),
  89.             'description'         => array(
  90.                 'title'   => __( 'Description', 'bangladeshi-payment-gateways' ),
  91.                 'default' => __(
  92.                     '
  93.                     01. Go to your Upay app or Dial *268#
  94.                     02. Choose “Send Money”
  95.                     03. Enter below Upay Account Number
  96.                     04. Enter <b>total amount</b>
  97.                     06. Now enter your Upay Account PIN to confirm the transaction
  98.                     07. Copy Transaction ID from payment confirmation message and paste that Transaction ID below',
  99.                     'bangladeshi-payment-gateways'
  100.                 ),
  101.                 'type'    => 'textarea',
  102.             ),
  103.             'upay_charge'         => array(
  104.                 'title'       => __( 'Upay Charge?', 'bangladeshi-payment-gateways' ),
  105.                 'type'        => 'checkbox',
  106.                 'description' => __( 'Add Upay <b>Send Money</b> charge.', 'bangladeshi-payment-gateways' ),
  107.                 'default'     => 'no',
  108.             ),
  109.  
  110.             'upay_fee'            => array(
  111.                 'title'       => __( 'Upay Fee? (in %)', 'bangladeshi-payment-gateways' ),
  112.                 'type'        => 'text',
  113.                 'default'     => '1.4',
  114.                 'description' => __( 'Don\'t add %.', 'bangladeshi-payment-gateways' ),
  115.             ),
  116.  
  117.             'upay_charge_details' => array(
  118.                 'title'   => __( 'Upay Charge Details', 'bangladeshi-payment-gateways' ),
  119.                 'type'    => 'textarea',
  120.                 'default' => __( 'Upay "Send Money" fee will be added with net price.' ),
  121.             ),
  122.  
  123.             'instructions'        => array(
  124.                 'title'       => __( 'Instructions', 'bangladeshi-payment-gateways' ),
  125.                 'type'        => 'textarea',
  126.                 'description' => __( 'Instructions', 'bangladeshi-payment-gateways' ),
  127.                 'default'     => '',
  128.             ),
  129.             'accounts'             => array(
  130.                 'type' => 'accounts',
  131.             ),
  132.         );
  133.  
  134.     }
  135.  
  136.     /**
  137.      * Payment Fields
  138.      */
  139.     public function payment_fields() {
  140.         global $woocommerce;
  141.  
  142.         $upay_charge_details = ( 'yes' === $this->upay_charge ) ? $this->upay_charge_details : '';
  143.         echo wpautop( wptexturize( __( $this->description, 'bangladeshi-payment-gateways' ) ) . ' ' . $upay_charge_details ); // @codingStandardsIgnoreLine
  144.  
  145.         $total_amount = 'আপনার বিকাশ, রকেট বা নগদের সেন্ড মানি অপশন থেকে <b>01312-278792</b> নাম্বারে <b>' . get_woocommerce_currency_symbol() . $woocommerce->cart->total . '</b> টাকা পে করুন</br>';
  146.         echo '<div class="bdpg-total-amount">' . $total_amount . '</div>';
  147.         ?>
  148.         <div class="bdpg-available-accounts">
  149.         <?php
  150.         foreach ( $this->accounts as $account ) {
  151.             ?>
  152.             <div class="bdpg-s__acc">
  153.             <?php
  154.             if ( '' !== $account['qr_code'] ) {
  155.                 ?>
  156.                 <div class="bdpg-acc__qr-code">
  157.                     <img src="<?php echo $account['qr_code']; ?>" alt="QR Code">
  158.                 </div>
  159.                 <?php
  160.             }
  161.             ?>
  162.                 <div class="bdpg-acc_d" >
  163.                     <p>Account Type: <b><?php echo esc_html( $account['type'] ); ?></b></p>
  164.                     <p>Account Number: <b><?php echo esc_html( $account['number'] ); ?></b></p></div>
  165.                
  166.             <?php
  167.         }
  168.         ?>
  169.             <div class="bdpg-user__acc">
  170.                 <div class="bdpg-user__field">
  171.                     <label for="Upay_acc_no">
  172.                         যে নাম্বার থেকে টাকা পাঠিয়েছেন (পুরোটা বা শেষ ৩ ডিজিট লিখুন)
  173.                     </label>
  174.                     <input type="text" class="widefat" name="Upay_acc_no" placeholder="01XXXXXXXXX">
  175.                 </div>
  176.                 <div class="bdpg-user__field">
  177.                     <label for="Upay_trans_id">
  178.                         ট্রাঞ্জেকশন আইডি (পুরোটা বা শেষ ৩টি লেটার লিখুন)
  179.                     </label>
  180.                     <input type="text" class="widefat" name="Upay_trans_id" placeholder="2M7A5">
  181.                 </div>
  182.             </div>
  183.         </div>
  184.         <?php
  185.     }
  186.  
  187.     /**
  188.      * Accounts Fields
  189.      */
  190.     public function generate_accounts_html() {
  191.         ob_start();
  192.         ?>
  193.         <tr valign="top">
  194.             <th scope="row" class="titledesc"><?php esc_html_e( 'Account Details', 'woocommerce' ); ?>:</th>
  195.             <td class="forminp" id="upay_accounts">
  196.                 <table class="widefat wc_input_table sortable" cellspacing="0">
  197.                     <thead>
  198.                         <tr>
  199.                             <th class="sort">&nbsp;</th>
  200.                             <th><?php esc_html_e( 'Account Type', 'woocommerce' ); ?></th>
  201.                             <th><?php esc_html_e( 'Account Number', 'woocommerce' ); ?></th>
  202.                             <th><?php esc_html_e( 'QR Code', 'woocommerce' ); ?></th>
  203.                         </tr>
  204.                     </thead>
  205.                     <tfoot>
  206.                         <tr>
  207.                             <th colspan="7">
  208.                                 <a href="#" class="add button">
  209.                                 <?php esc_html_e( '+ Add Account', 'woocommerce' ); ?></a>
  210.                                 <a href="#" class="remove_rows button"><?php esc_html_e( 'Remove selected account(s)', 'woocommerce' ); ?>
  211.                                 </a>
  212.                             </th>
  213.                         </tr>
  214.                     </tfoot>
  215.                     <tbody class="accounts ui-sortable">
  216.                         <?php
  217.                         $i = -1;
  218.                         if ( $this->accounts ) {
  219.                             foreach ( $this->accounts as $account ) {
  220.                                 $i++;
  221.                                 echo '<tr class="account">
  222.                                     <td class="sort"></td>
  223.                                     <td><input type="text" value="' . esc_attr( $account['type'] ) . '" name="upay_account_type[' . $i . ']" /></td>
  224.                                     <td><input type="text" value="' . esc_attr( $account['number'] ) . '" name="upay_account_number[' . $i . ']" /></td><td><input type="hidden" value="' . esc_attr( $account['qr_code'] ) . '" name="upay_account_qr_code[' . $i . ']" id="bdpg_qr_code-' . $i . '" />
  225.                                     <input type="button" class="button button-primary add_qr_c_img" value="Edit Image" data-target="#bdpg_qr_code-' . $i . '"  data-qr="#bdpg_qr_img-' . $i . '"><div  id="bdpg_qr_img-' . $i . '"><img src="' . esc_attr( $account['qr_code'] ) . '" alt="QR Code" id="qr_code" /></div>
  226.                                     </td>
  227.                                     </tr>';
  228.                             }
  229.                         }
  230.                         ?>
  231.                     </tbody>
  232.                 </table>
  233.                 <script>
  234.  
  235.                     jQuery(function($) {
  236.                         $('#upay_accounts').on( 'click', 'a.add', function(){
  237.  
  238.                             var size = $('#upay_accounts').find('tbody .account').length;
  239.  
  240.                             $('<tr class="account">\
  241.                                     <td class="sort"></td>\
  242.                                     <td><input type="text" name="upay_account_type[' + size + ']" /></td>\
  243.                                     <td><input type="text" name="upay_account_number[' + size + ']" /></td>\
  244.                                     <td><input type="hidden" id="bdpg_qr_code-' + size + '" name="upay_account_qr_code[' + size + ']" /><input type="button" class="button button-primary add_qr_c_img" value="Add Image" data-target="#bdpg_qr_code-' + size + '" data-qr="#bdpg_qr_img-' + size + '"><div id="bdpg_qr_img-' + size + '"></div>\
  245.                                     </td>\
  246.                                 </tr>').appendTo('#upay_accounts table tbody');
  247.  
  248.                             return false;
  249.                         });
  250.  
  251.                     });
  252.  
  253.                 </script>
  254.             </td>
  255.         </tr>
  256.         <?php
  257.         return ob_get_clean();
  258.     }
  259.  
  260.     /**
  261.      * Save Accounts
  262.      */
  263.     public function save_accounts() {
  264.  
  265.         if ( isset( $_POST['upay_account_type'] ) ) {
  266.             $accounts = array();
  267.  
  268.             $type    = array_map( 'wc_clean', $_POST['upay_account_type'] );
  269.             $number  = array_map( 'wc_clean', $_POST['upay_account_number'] );
  270.             $qr_code = array_map( 'wc_clean', $_POST['upay_account_qr_code'] );
  271.  
  272.             foreach ( $type as $key => $value ) {
  273.                 if ( ! isset( $type[ $key ] ) ) {
  274.                     continue;
  275.                 }
  276.  
  277.                 $accounts[] = array(
  278.                     'type'    => $type[ $key ],
  279.                     'number'  => $number[ $key ],
  280.                     'qr_code' => $qr_code[ $key ],
  281.                 );
  282.             }
  283.             update_option( 'bdpg_upay_accounts', $accounts );
  284.         }
  285.  
  286.     }
  287.  
  288.     /**
  289.      * Process Payment.
  290.      *
  291.      * @param int $order_id Order ID.
  292.      */
  293.     public function process_payment( $order_id ) {
  294.         global $woocommerce;
  295.  
  296.         $order = new \WC_Order( $order_id );
  297.  
  298.         // Mark as on-hold (we're awaiting the cheque).
  299.         $order->update_status( 'on-hold', __( 'পেমেন্টের জন্য অপেক্ষা করা হচ্ছে', 'bangladeshi-payment-gateways' ) );
  300.  
  301.         // Reduce stock levels.
  302.         $order->reduce_order_stock();
  303.  
  304.         // Remove cart.
  305.         $woocommerce->cart->empty_cart();
  306.  
  307.         // Return thankyou redirect.
  308.         return array(
  309.             'result'   => 'success',
  310.             'redirect' => $this->get_return_url( $order ),
  311.         );
  312.     }
  313.  
  314.     /**
  315.      * Thank You Page.
  316.      *
  317.      * @param int $order_id Order ID.
  318.      */
  319.     public function bdpg_thankyou( $order_id ) {
  320.  
  321.         $order = new \WC_Order( $order_id );
  322.  
  323.         if ( $this->id === $order->get_payment_method() ) {
  324.             echo wpautop( $this->instructions );
  325.         } else {
  326.             echo esc_html__( 'Thank you. Your order has been received.', 'woocommerce' );
  327.         }
  328.     }
  329.  
  330.     /**
  331.      * Customer Email.
  332.      *
  333.      * @param Object  $order order.
  334.      * @param [type]  $sent_to_admin Sent to admin.
  335.      * @param boolean $plain_text Plain Text.
  336.      * @return string
  337.      */
  338.     public function customer_email_instructions( $order, $sent_to_admin, $plain_text = false ) {
  339.         if ( $this->id !== $order->get_payment_method() || $sent_to_admin ) {
  340.             return;
  341.         }
  342.  
  343.         if ( $this->instructions ) {
  344.             echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
  345.         }
  346.     }
  347.  
  348.  
  349.     /**
  350.      * Field Validation.
  351.      */
  352.     public function payment_process() {
  353.         if ( 'woo_upay' !== $_POST['payment_method'] ) {
  354.             return;
  355.         }
  356.  
  357.         $number   = sanitize_text_field( $_POST['Upay_acc_no'] );
  358.         $trans_id = sanitize_text_field( $_POST['Upay_trans_id'] );
  359.  
  360.         if ( '' === $number ) {
  361.             wc_add_notice( __( 'অনুগ্রহ করে যে নাম্বার থেকে টাকা পাঠিয়েছেন সেটি অথবা তার শেষ ৪ ডিজিট লিখুন', 'bangladeshi-payment-gateways' ), 'error' );
  362.         }
  363.  
  364.         if ( '' === $number ) {
  365.             wc_add_notice( __( 'অনুগ্রহ করে যে নাম্বার থেকে টাকা পাঠিয়েছেন সেটি অথবা তার শেষ ৪ ডিজিট লিখুন', 'bangladeshi-payment-gateways' ), 'error' );
  366.         }
  367.  
  368.         if ( '' === $trans_id ) {
  369.             wc_add_notice( __( 'অনুগ্রহ করে আপনার পেমেন্টের লেনদেন আইডি লিখুন', 'bangladeshi-payment-gateways' ), 'error' );
  370.         }
  371.     }
  372.  
  373.     /**
  374.      * Field Update.
  375.      *
  376.      * @param int $order_id Order ID.
  377.      */
  378.     public function fields_update( $order_id ) {
  379.  
  380.         if ( 'woo_upay' !== $_POST['payment_method'] ) {
  381.             return;
  382.         }
  383.         $number   = sanitize_text_field( $_POST['Upay_acc_no'] );
  384.         $trans_id = sanitize_text_field( $_POST['Upay_trans_id'] );
  385.  
  386.         update_post_meta( $order_id, 'woo_upay_number', $number );
  387.         update_post_meta( $order_id, 'woo_upay_trans_id', $trans_id );
  388.     }
  389.     /**
  390.      * Display Upay data in admin page.
  391.      *
  392.      * @param Object $order Order.
  393.      */
  394.     public function admin_order_data( $order ) {
  395.         if ( 'woo_upay' !== $order->get_payment_method() ) {
  396.             return;
  397.         }
  398.  
  399.         $number   = ( get_post_meta( $_GET['post'], 'woo_upay_number', true ) ) ? get_post_meta( $_GET['post'], 'woo_upay_number', true ) : '';
  400.         $trans_id = ( get_post_meta( $_GET['post'], 'woo_upay_trans_id', true ) ) ? get_post_meta( $_GET['post'], 'woo_upay_trans_id', true ) : '';
  401.         ?>
  402.         <div class="form-field form-field-wide bdpg-admin-data">
  403.             <img src="<?php echo esc_url( $this->pg_icon ); ?> " alt="Upay">
  404.             <table class="wp-list-table widefat striped posts">
  405.                 <tbody>
  406.                     <tr>
  407.                         <th>
  408.                             <strong>
  409.                                 <?php echo __( 'Upay Number', 'bangladeshi-payment-gateways' ); ?>
  410.                             </strong>
  411.                         </th>
  412.                         <td>
  413.         <?php echo esc_attr( $number ); ?>
  414.                         </td>
  415.                     </tr>
  416.                     <tr>
  417.                         <th>
  418.                             <strong>
  419.                                 <?php echo __( 'Transaction ID', 'bangladeshi-payment-gateways' ); ?>
  420.                             </strong>
  421.                         </th>
  422.                         <td>
  423.         <?php echo esc_attr( $trans_id ); ?>
  424.                         </td>
  425.                     </tr>
  426.                 </tbody>
  427.             </table>
  428.         </div>
  429.         <?php
  430.     }
  431.  
  432.     /**
  433.      * Check if Upay charge status.
  434.      *
  435.      * @param Object $cart Cart.
  436.      */
  437.     public function charge_settings( $cart ) {
  438.         global $woocommerce;
  439.         $upay_settings = get_option( 'woocommerce_woo_upay_settings' );
  440.  
  441.         $av_gateways = $woocommerce->payment_gateways->get_available_payment_gateways();
  442.         if ( ! empty( $av_gateways ) ) {
  443.  
  444.             $payment_method = \WC()->session->get( 'chosen_payment_method' );
  445.  
  446.             if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
  447.                 return;
  448.             }
  449.  
  450.             if ( 'woo_upay' === $payment_method ) {
  451.                 $label  = __( 'Upay Charge', 'bangladeshi-payment-gateways' );
  452.                 $amount = round( $cart->cart_contents_total * ( $upay_settings['upay_fee'] / 100 ) );
  453.                 $cart->add_fee( $label, $amount, true, 'standard' );
  454.             }
  455.         }
  456.     }
  457.     /**
  458.      * Display Upay data in order review page
  459.      *
  460.      * @param Object $order Order.
  461.      */
  462.     public function data_order_review_page( $order ) {
  463.         if ( 'woo_upay' !== $order->get_payment_method() ) {
  464.             return;
  465.         }
  466.         global $wp;
  467.  
  468.         if ( isset( $wp->query_vars['order-received'] ) ) {
  469.             $order_id = (int) $wp->query_vars['order-received'];
  470.         } else {
  471.             $order_id = (int) $wp->query_vars['view-order'];
  472.         }
  473.  
  474.         $number   = ( get_post_meta( $order_id, 'woo_upay_number', true ) ) ? get_post_meta( $order_id, 'woo_upay_number', true ) : '';
  475.         $trans_id = ( get_post_meta( $order_id, 'woo_upay_trans_id', true ) ) ? get_post_meta( $order_id, 'woo_upay_trans_id', true ) : '';
  476.         ?>
  477.         <div class="bdpg-g-details">
  478.             <table class="wp-list-table widefat striped posts">
  479.                 <tbody>
  480.                     <tr>
  481.                         <th>
  482.                             <strong>
  483.                                 <?php echo esc_html__( 'Payment Number', 'bangladeshi-payment-gateways' ); ?>
  484.                             </strong>
  485.                         </th>
  486.                         <td>
  487.         <?php echo esc_attr( $number ); ?>
  488.                         </td>
  489.                     </tr>
  490.                     <tr>
  491.                         <th>
  492.                             <strong>
  493.                                 <?php echo esc_html__( 'Transaction ID', 'bangladeshi-payment-gateways' ); ?>
  494.                             </strong>
  495.                         </th>
  496.                         <td>
  497.         <?php echo esc_attr( $trans_id ); ?>
  498.                         </td>
  499.                     </tr>
  500.                 </tbody>
  501.             </table>
  502.         </div>
  503.         <?php
  504.     }
  505.     /**
  506.      * Register New Column For Payment Info
  507.      *
  508.      * @param array $columns Order.
  509.      */
  510.     public function admin_register_column( $columns ) {
  511.  
  512.         $columns = ( is_array( $columns ) ) ? $columns : array();
  513.  
  514.         $columns['payment_no'] = esc_html__( 'Payment No', 'bangladeshi-payment-gateways' );
  515.         $columns['tran_id']    = esc_html__( 'Tran. ID', 'bangladeshi-payment-gateways' );
  516.  
  517.         return $columns;
  518.  
  519.     }
  520.  
  521.     /**
  522.      * Load Payment Data in New Column
  523.      *
  524.      * @param string $column Column name.
  525.      */
  526.     public function admin_column_value( $column ) {
  527.  
  528.         global $post;
  529.  
  530.         $payment_no = ( get_post_meta( $post->ID, 'woo_upay_number', true ) ) ? get_post_meta( $post->ID, 'woo_upay_number', true ) : '';
  531.         $tran_id    = ( get_post_meta( $post->ID, 'woo_upay_trans_id', true ) ) ? get_post_meta( $post->ID, 'woo_upay_trans_id', true ) : '';
  532.  
  533.         if ( 'payment_no' === $column ) {
  534.             echo esc_attr( $payment_no );
  535.         }
  536.  
  537.         if ( 'tran_id' === $column ) {
  538.             echo esc_attr( $tran_id );
  539.         }
  540.     }
  541.  
  542. }
Add Comment
Please, Sign In to add comment