Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php /** * Plugin Name: Paybotic Payment Gateway * Plugin URI: https://yo..

Decoded Output download

<?php 
 
/** 
 * Plugin Name:     Paybotic Payment Gateway 
 * Plugin URI:      https://yourwebsite.com/ 
 * Description:     This plugin adds a custom option to WooCommerce payments. 
 * Author:          Paybotic 
 * Author URI:      https://yourwebsite.com/ 
 * Text Domain:     wc-button-adder 
 * Domain Path:     /languages 
 * Version:         1.0.0 
 * 
 * @package WooCommerceButtonAdder 
 */ 
 
/* 
 * This action hook registers our PHP class as a WooCommerce payment gateway 
 */ 
 
add_filter('woocommerce_payment_gateways', 'paybotic_add_gateway_class'); 
function paybotic_add_gateway_class($gateways) 
{ 
  $gateways[] = 'WC_Paybotic_Gateway'; // your class name is here 
  return $gateways; 
} 
 
/* 
 * The class itself, please note that it is inside plugins_loaded action hook 
 */ 
add_action('plugins_loaded', 'paybotic_init_gateway_class'); 
 
function paybotic_init_gateway_class() 
{ 
 
  class WC_Paybotic_Gateway extends WC_Payment_Gateway 
  { 
 
    public $testmode; 
    public $get_testmode; 
    public $paybotic_custom_fees; 
    public $init_payment_endpoint; 
    public $check_payment_status_endpoint; 
    public $check_fees_order_endpoint; 
 
    /** 
     * Class constructor 
     */ 
    public function __construct() 
    { 
 
      $this->id = 'paybotic'; // payment gateway plugin ID 
      $this->icon = ''; // URL of the icon that will be displayed on checkout page near your gateway name 
      $this->has_fields = true; // in case you need a custom credit card form 
      $this->method_title = 'Paybotic Payment Gateway'; 
      $this->method_description = 'Description of Paybotic payment gateway'; // will be displayed on the options page 
 
      // gateways can support subscriptions, refunds, saved payment methods, 
      // but in this tutorial we begin with simple payments 
      $this->supports = array( 
        'products' 
      ); 
 
      // Method with all the options fields 
      $this->init_form_fields(); 
 
      // Load the settings. 
      $this->init_settings(); 
      $this->title = $this->get_option('title'); 
      $this->description = $this->get_option('description'); 
      $this->enabled = $this->get_option('enabled'); 
      $this->testmode = 'yes' === $this->get_option('testmode'); 
      $this->paybotic_custom_fees = $this->get_option('paybotic_custom_fees'); 
 
      $options = get_option('woocommerce_paybotic_settings'); 
 
      $this->get_testmode = $options['testmode']; 
 
 
      // path api 
      $endpoint = 'yes' === $this->get_testmode 
        ? 'https://paybotic-banking-be-stage-iwwpe3yduq-uc.a.run.app/' 
        : 'https://paybotic-banking-backend-lwmbsw7i6q-uc.a.run.app/'; 
 
      $this->init_payment_endpoint = $endpoint . 'v1/marketplace/init-payment'; 
      $this->check_payment_status_endpoint = $endpoint . 'v1/buyer-transaction/:ecommercePlatformId/:orderId/detail'; 
      $this->check_fees_order_endpoint = $endpoint . 'v1/buyer-transaction/calculate/fees'; 
 
      // This action hook saves the settings 
      add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); 
 
      // We need custom JavaScript to obtain a token 
      add_action('wp_enqueue_scripts', array($this, 'payment_scripts')); 
 
      // You can also register a webhook here 
      // add_action( 'woocommerce_api_{webhook name}', array( $this, 'webhook' ) ); 
    } 
 
    /** 
     * Plugin options, configurable via WooCommerce 
     */ 
    public function init_form_fields() 
    { 
 
      $this->form_fields = array( 
        'enabled' => array( 
          'title'       => 'Enable/Disable', 
          'label'       => 'Enable Paybotic Gateway', 
          'type'        => 'checkbox', 
          'description' => '', 
          'default'     => 'no' 
        ), 
        'title' => array( 
          'title'       => 'Title', 
          'type'        => 'text', 
          'description' => 'This controls the title which the user sees during checkout.', 
          'default'     => ' Pay with Paybotic Financial', 
          'desc_tip'    => true, 
        ), 
        'description' => array( 
          'title'       => 'Description', 
          'type'        => 'textarea', 
          'description' => 'This controls the description which the user sees during checkout.', 
          'default'     => 'Pay with your credit card via our super-cool payment gateway.', 
        ), 
        'testmode' => array( 
          'title'       => 'Test mode', 
          'label'       => 'Enable Test Mode', 
          'type'        => 'checkbox', 
          'description' => 'Place the payment gateway in test mode using test API keys.', 
          'default'     => 'yes', 
          'desc_tip'    => true, 
        ), 
        'paybotic_api_key' => array( 
          'title'       => 'Api key', 
          'type'        => 'text', 
        ), 
        'paybotic_merchant_id' => array( 
          'title'       => 'Merchant ID', 
          'type'        => 'text', 
        ), 
        'paybotic_ecommerce_platform_id' => array( 
          'title'       => 'E-commerce Platform ID', 
          'type'        => 'text', 
        ), 
        'paybotic_custom_fees' => array( 
          'title'       => 'Transaction Fee', 
          'type'        => 'select', 
          'options'     => array( 
            'charge_merchant' => 'Merchant pay', 
            'charge_buyer'  => 'Buyer pay', 
          ), 
          'default'     => 'charge_merchant' 
        ), 
      ); 
    } 
 
    public function payment_scripts() 
    { 
 
      // let's suppose it is our payment processor JavaScript that allows to obtain a token 
      wp_enqueue_script('paybotic_js', 'some payment processor site/api/token.js'); 
 
      // and this is our custom JS in your plugin directory that works with token.js 
      wp_register_script('woocommerce_paybotic', plugins_url('assets/js/popup.js', __FILE__), array('jquery', 'paybotic_js')); 
 
      wp_enqueue_script('woocommerce_paybotic'); 
    } 
 
    // this function wraps the api post init payment 
    public function execPost($url, $data, $bearerToken) 
    { 
      $body = json_encode($data); 
      $args = array( 
        'method'      => 'POST', 
        'headers'     => array( 
          'Content-Type'  => 'application/json', 
          'Authorization' => 'Bearer ' . $bearerToken, 
        ), 
        'body'        => $body, 
        'data_format' => 'body', 
      ); 
 
      $response = wp_remote_post($url, $args); 
 
      if (is_wp_error($response)) { 
        $error_message = $response->get_error_message(); 
        error_log('execPost error: ' . $error_message); 
 
        return false; 
      } else { 
        return wp_remote_retrieve_body($response); 
      } 
    } 
 
    // this function wraps the api get status order 
    public function execGetOrderStatus($url, $bearerToken) 
    { 
      $args = array( 
        'method'      => 'GET', 
        'headers'     => array( 
          'Content-Type'  => 'application/json', 
          'Authorization' => 'Bearer ' . $bearerToken, 
        ), 
      ); 
 
      $response = wp_remote_get($url, $args); 
 
      error_log('execGetOrderStatus ' . $response); 
 
      if (is_wp_error($response)) { 
        $error_message = $response->get_error_message(); 
        error_log('execGetOrderStatus error: ' . $error_message); 
 
        return false; 
      } else { 
        return wp_remote_retrieve_body($response); 
      } 
    } 
 
 
    public function mapping_order_status($status, $description) 
    { 
      $result = new stdClass(); 
 
      $result->description = $description; 
 
      switch ($status) { 
        case 'PENDING': 
          $result->status = 'processing'; 
          break; 
        case 'COMPLETED': 
          $result->status = 'completed'; 
          break; 
        case 'CANCELLED': 
          $result->status = 'failed'; 
          break; 
        case 'FAILED': 
          $result->status = 'failed'; 
          break; 
        case 'DECLINED': 
          $result->status = 'failed'; 
          break; 
        default: 
          $result->status = ''; 
          $result->description = 'Unknown status.'; 
      } 
 
      return $result; 
    } 
 
    // this function will be called when the user clicks on the button 
    public function process_payment($order_id) 
    { 
 
      $order = wc_get_order($order_id); 
      $options = get_option('woocommerce_paybotic_settings'); 
 
      $paybotic_api_key = $options['paybotic_api_key']; 
      $paybotic_merchant_id = $options['paybotic_merchant_id']; 
      $paybotic_ecommerce_platform_id = $options['paybotic_ecommerce_platform_id']; 
      $paybotic_custom_fees = $options['paybotic_custom_fees']; 
 
      $is_paid = $order->get_status(); 
 
      error_log('The order #' . $is_paid); 
 
      $fees = $order->get_fees('Transaction Fee'); 
      $total_fees = 0; 
 
      foreach ($fees as $fee) { 
        $fee_name = $fee->get_name(); 
 
        if ($fee_name === 'Transaction Fee') { 
          $total_fees = $fee->get_total(); 
        } 
      } 
 
      $data = array( 
        'merchantId' => $paybotic_merchant_id, 
        'orderId' => strval($order->id), 
        'transactionAmount' => $order->total, 
        'originalAmount' => $order->total - $total_fees, 
        'street' => $order->get_billing_address_1(), 
        'unit' => $order->get_billing_address_2(), 
        'city' => $order->get_billing_city(), 
        'state' => $order->get_billing_state(), 
        'postalCode' => $order->get_billing_postcode(), 
        'externalMerchantId' => $paybotic_merchant_id, 
        'ecommercePlatformId' => $paybotic_ecommerce_platform_id, 
        'feeType' => $paybotic_custom_fees, 
      ); 
 
      $result = $this->execPost($this->init_payment_endpoint, $data, $paybotic_api_key); 
 
      $first_name = ''; 
      $last_name = ''; 
      $email = ''; 
      $phone = ''; 
      $street = ''; 
      $unit = ''; 
      $city = ''; 
      $state = ''; 
 
      if ($order->get_billing_first_name()) { 
        $first_name = $order->get_billing_first_name(); 
      } 
      if ($order->get_billing_last_name()) { 
        $last_name = $order->get_billing_last_name(); 
      } 
      if ($order->get_billing_email()) { 
        $email = $order->get_billing_email(); 
      } 
      if ($order->get_billing_phone()) { 
        $phone = $order->get_billing_phone(); 
      } 
      if ($order->get_billing_address_1()) { 
        $street = $order->get_billing_address_1(); 
      } 
      if ($order->get_billing_address_2()) { 
        $unit = $order->get_billing_address_2(); 
      } 
      if ($order->get_billing_city()) { 
        $city = $order->get_billing_city(); 
      } 
      if ($order->get_billing_state()) { 
        $state = $order->get_billing_state(); 
      } 
 
      if (!$street && $order->get_shipping_address_1()) { 
        $street = $order->get_shipping_address_1(); 
      } 
      if (!$unit && $order->get_shipping_address_2()) { 
        $unit = $order->get_shipping_address_2(); 
      } 
      if (!$city && $order->get_shipping_city()) { 
        $city = $order->get_shipping_city(); 
      } 
      if (!$state && $order->get_shipping_state()) { 
        $state = $order->get_shipping_state(); 
      } 
 
      if ($result !== false) { 
        $result_decode = json_decode($result); 
        $callback_url = urlencode($result_decode->paymentButtonUrl . '&firstName=' . $first_name . '&lastName=' . $last_name . '&email=' . $email . '&phone=' . $phone . '&street=' . $street . '&unit=' . $unit . '&city=' . $city . '&state=' . $state); 
        $orderId = urlencode($order->get_id()); 
        $orderKey = urlencode($order->get_order_key()); 
 
        return array( 
          'result'   => 'success', 
          'redirect' => home_url("checkout?callback_url={$callback_url}&orderId={$orderId}&orderKey={$orderKey}&firstName={$first_name}&lastName={$last_name}&email={$email}") 
        ); 
      } 
    } 
 
    // this function will be called api check payment status from Paybotic 
    public function sync_order_status($prop_order_id) 
    { 
      $instance = new WC_Paybotic_Gateway(); 
 
      $options = get_option('woocommerce_paybotic_settings'); 
 
      $paybotic_api_key = $options['paybotic_api_key']; 
      $paybotic_ecommerce_platform_id = $options['paybotic_ecommerce_platform_id']; 
 
      $order = wc_get_order($prop_order_id); 
      $order_id = $order->get_id(); 
 
      $url = str_replace( 
        array(':ecommercePlatformId', ':orderId'), 
        array($paybotic_ecommerce_platform_id, $order_id), 
        $instance->check_payment_status_endpoint 
      ); 
 
      if (!$order_id) return false; 
 
      $result = $instance->execGetOrderStatus($url, $paybotic_api_key); 
 
      if ($result !== false) { 
        $result_decode = json_decode($result); 
 
        $is_processing = $result_decode->status === 'PENDING'; 
        $is_completed = $result_decode->status === 'COMPLETED'; 
 
        if ($result_decode->id && $is_processing || $is_completed) { 
          $description = $is_processing ? 'The order has been successfully paid and is being processed.' : 'The order has been completed successfully.'; 
 
          $mapping_status = $instance->mapping_order_status($result_decode->status, $description); 
 
          error_log('The order sync status - ' . $order_id . ' - ' . $mapping_status->status . ' - ' . $mapping_status->description); 
 
          $order->update_status($mapping_status->status, $mapping_status->description); 
 
          return true; 
        } 
      } 
 
      return false; 
    } 
 
    public function calculate_order_fees($total_amount) 
    { 
      $options = get_option('woocommerce_paybotic_settings'); 
 
      $paybotic_api_key = $options['paybotic_api_key']; 
      $paybotic_ecommerce_platform_id = $options['paybotic_ecommerce_platform_id']; 
      $paybotic_custom_fees = $options['paybotic_custom_fees']; 
 
      $data = array( 
        'ecommercePlatformId' => $paybotic_ecommerce_platform_id, 
        'type' => $paybotic_custom_fees, 
        'totalAmount' => $total_amount, 
      ); 
 
      return $this->execPost($this->check_fees_order_endpoint, $data, $paybotic_api_key); 
    } 
  } 
} 
 
add_action('wp_ajax_check_order_status', 'ajax_check_order_status'); 
add_action('wp_ajax_nopriv_check_order_status', 'ajax_check_order_status'); 
 
function ajax_check_order_status() 
{ 
  $order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0; 
  $order_key = isset($_POST['order_key']) ? $_POST['order_key'] : ''; 
 
  $instance = new WC_Paybotic_Gateway(); 
  $result = $instance->sync_order_status($order_id); 
 
  if ($result) { 
    wp_send_json_success(array('redirect' => home_url("checkout/order-received/{$order_id}/?key={$order_key}"))); 
  } 
} 
 
add_action('wp_ajax_paybotic_webhook_sync_status', 'paybotic_webhook_sync_status'); 
add_action('wp_ajax_nopriv_paybotic_webhook_sync_status', 'paybotic_webhook_sync_status'); 
 
function paybotic_webhook_sync_status() 
{ 
  $data = isset($_POST['data']) ? $_POST['data'] : ''; 
  $instance = new WC_Paybotic_Gateway(); 
 
  if ($data && $data['object']) { 
    $order_id = $data['object']['order_key']; 
    $status = $data['object']['transaction_status']; 
    $description = $data['object']['transaction_message']; 
 
    $order = wc_get_order($order_id); 
    $mapping_status = $instance->mapping_order_status(strtoupper($status), $description); 
 
    if (!$mapping_status->status) return; 
 
    error_log('The order webhook status - ' . $order_id . ' - ' . $mapping_status->status . ' - ' . $mapping_status->description); 
 
    $order->update_status($mapping_status->status,  $mapping_status->description); 
  } 
} 
 
// Add dialog to notify popup block by browser setting 
function custom_popup_html() 
{ 
?> 
  <div class="pf-popup-backdrop"></div> 
  <div class="pf-popup-content"> 
    <div class="pf-close-button"></div> 
    <p>Click continue to proceed with payment via Paybotic Financial</p> 
    <button class="pf-popup-action">Continue</button> 
  </div> 
<?php 
} 
 
add_action('wp_footer', 'custom_popup_html'); 
 
function custom_popup_payment_html() 
{ 
?> 
  <div class="pf-popup-payment-backdrop"></div> 
  <div class="pf-popup-payment-content"> 
    <div class="pf-close-button"></div> 
    <iframe class="pf-popup-payment-iframe"></iframe> 
  </div> 
<?php 
} 
 
add_action('wp_footer', 'custom_popup_payment_html'); 
 
 
function my_plugin_enqueue_styles() 
{ 
  wp_enqueue_style('my-plugin-styles', plugins_url('assets/css/index.css', __FILE__)); 
} 
 
add_action('wp_enqueue_scripts', 'my_plugin_enqueue_styles'); 
 
// add custom fees 
add_filter('woocommerce_cart_calculate_fees', 'add_paybotic_custom_fees', 10, 1); 
 
function add_paybotic_custom_fees($cart) 
{ 
  $instance = new WC_Paybotic_Gateway(); 
 
  $type = $instance->get_option('paybotic_custom_fees'); 
 
  if ($type === 'charge_merchant') { 
    return; 
  } 
 
  $subtotal = $cart->subtotal; 
  $shipping_total = $cart->shipping_total; 
  $tax_total = $cart->tax_total; 
 
  $total_amount = 0; 
 
  if ($subtotal) { 
    $total_amount += $subtotal; 
  } 
 
  if ($shipping_total) { 
    $total_amount += $shipping_total; 
  } 
 
  if ($tax_total) { 
    $total_amount += $tax_total; 
  } 
 
  $result = $instance->calculate_order_fees($total_amount); 
 
  if ($result !== false) { 
    $result_decode = json_decode($result); 
    $total_fees = $result_decode->totalFees; 
    $total_fees_rounded = round($total_fees, 2); 
    $title = 'Transaction Fee'; 
 
    error_log($title . ' - ' . $total_fees_rounded); 
 
    $cart->add_fee($title, $total_fees_rounded); 
  } 
} 
 
?>

Did this file decode correctly?

Original Code

<?php

/**
 * Plugin Name:     Paybotic Payment Gateway
 * Plugin URI:      https://yourwebsite.com/
 * Description:     This plugin adds a custom option to WooCommerce payments.
 * Author:          Paybotic
 * Author URI:      https://yourwebsite.com/
 * Text Domain:     wc-button-adder
 * Domain Path:     /languages
 * Version:         1.0.0
 *
 * @package WooCommerceButtonAdder
 */

/*
 * This action hook registers our PHP class as a WooCommerce payment gateway
 */

add_filter('woocommerce_payment_gateways', 'paybotic_add_gateway_class');
function paybotic_add_gateway_class($gateways)
{
  $gateways[] = 'WC_Paybotic_Gateway'; // your class name is here
  return $gateways;
}

/*
 * The class itself, please note that it is inside plugins_loaded action hook
 */
add_action('plugins_loaded', 'paybotic_init_gateway_class');

function paybotic_init_gateway_class()
{

  class WC_Paybotic_Gateway extends WC_Payment_Gateway
  {

    public $testmode;
    public $get_testmode;
    public $paybotic_custom_fees;
    public $init_payment_endpoint;
    public $check_payment_status_endpoint;
    public $check_fees_order_endpoint;

    /**
     * Class constructor
     */
    public function __construct()
    {

      $this->id = 'paybotic'; // payment gateway plugin ID
      $this->icon = ''; // URL of the icon that will be displayed on checkout page near your gateway name
      $this->has_fields = true; // in case you need a custom credit card form
      $this->method_title = 'Paybotic Payment Gateway';
      $this->method_description = 'Description of Paybotic payment gateway'; // will be displayed on the options page

      // gateways can support subscriptions, refunds, saved payment methods,
      // but in this tutorial we begin with simple payments
      $this->supports = array(
        'products'
      );

      // Method with all the options fields
      $this->init_form_fields();

      // Load the settings.
      $this->init_settings();
      $this->title = $this->get_option('title');
      $this->description = $this->get_option('description');
      $this->enabled = $this->get_option('enabled');
      $this->testmode = 'yes' === $this->get_option('testmode');
      $this->paybotic_custom_fees = $this->get_option('paybotic_custom_fees');

      $options = get_option('woocommerce_paybotic_settings');

      $this->get_testmode = $options['testmode'];


      // path api
      $endpoint = 'yes' === $this->get_testmode
        ? 'https://paybotic-banking-be-stage-iwwpe3yduq-uc.a.run.app/'
        : 'https://paybotic-banking-backend-lwmbsw7i6q-uc.a.run.app/';

      $this->init_payment_endpoint = $endpoint . 'v1/marketplace/init-payment';
      $this->check_payment_status_endpoint = $endpoint . 'v1/buyer-transaction/:ecommercePlatformId/:orderId/detail';
      $this->check_fees_order_endpoint = $endpoint . 'v1/buyer-transaction/calculate/fees';

      // This action hook saves the settings
      add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));

      // We need custom JavaScript to obtain a token
      add_action('wp_enqueue_scripts', array($this, 'payment_scripts'));

      // You can also register a webhook here
      // add_action( 'woocommerce_api_{webhook name}', array( $this, 'webhook' ) );
    }

    /**
     * Plugin options, configurable via WooCommerce
     */
    public function init_form_fields()
    {

      $this->form_fields = array(
        'enabled' => array(
          'title'       => 'Enable/Disable',
          'label'       => 'Enable Paybotic Gateway',
          'type'        => 'checkbox',
          'description' => '',
          'default'     => 'no'
        ),
        'title' => array(
          'title'       => 'Title',
          'type'        => 'text',
          'description' => 'This controls the title which the user sees during checkout.',
          'default'     => ' Pay with Paybotic Financial',
          'desc_tip'    => true,
        ),
        'description' => array(
          'title'       => 'Description',
          'type'        => 'textarea',
          'description' => 'This controls the description which the user sees during checkout.',
          'default'     => 'Pay with your credit card via our super-cool payment gateway.',
        ),
        'testmode' => array(
          'title'       => 'Test mode',
          'label'       => 'Enable Test Mode',
          'type'        => 'checkbox',
          'description' => 'Place the payment gateway in test mode using test API keys.',
          'default'     => 'yes',
          'desc_tip'    => true,
        ),
        'paybotic_api_key' => array(
          'title'       => 'Api key',
          'type'        => 'text',
        ),
        'paybotic_merchant_id' => array(
          'title'       => 'Merchant ID',
          'type'        => 'text',
        ),
        'paybotic_ecommerce_platform_id' => array(
          'title'       => 'E-commerce Platform ID',
          'type'        => 'text',
        ),
        'paybotic_custom_fees' => array(
          'title'       => 'Transaction Fee',
          'type'        => 'select',
          'options'     => array(
            'charge_merchant' => 'Merchant pay',
            'charge_buyer'  => 'Buyer pay',
          ),
          'default'     => 'charge_merchant'
        ),
      );
    }

    public function payment_scripts()
    {

      // let's suppose it is our payment processor JavaScript that allows to obtain a token
      wp_enqueue_script('paybotic_js', 'some payment processor site/api/token.js');

      // and this is our custom JS in your plugin directory that works with token.js
      wp_register_script('woocommerce_paybotic', plugins_url('assets/js/popup.js', __FILE__), array('jquery', 'paybotic_js'));

      wp_enqueue_script('woocommerce_paybotic');
    }

    // this function wraps the api post init payment
    public function execPost($url, $data, $bearerToken)
    {
      $body = json_encode($data);
      $args = array(
        'method'      => 'POST',
        'headers'     => array(
          'Content-Type'  => 'application/json',
          'Authorization' => 'Bearer ' . $bearerToken,
        ),
        'body'        => $body,
        'data_format' => 'body',
      );

      $response = wp_remote_post($url, $args);

      if (is_wp_error($response)) {
        $error_message = $response->get_error_message();
        error_log('execPost error: ' . $error_message);

        return false;
      } else {
        return wp_remote_retrieve_body($response);
      }
    }

    // this function wraps the api get status order
    public function execGetOrderStatus($url, $bearerToken)
    {
      $args = array(
        'method'      => 'GET',
        'headers'     => array(
          'Content-Type'  => 'application/json',
          'Authorization' => 'Bearer ' . $bearerToken,
        ),
      );

      $response = wp_remote_get($url, $args);

      error_log('execGetOrderStatus ' . $response);

      if (is_wp_error($response)) {
        $error_message = $response->get_error_message();
        error_log('execGetOrderStatus error: ' . $error_message);

        return false;
      } else {
        return wp_remote_retrieve_body($response);
      }
    }


    public function mapping_order_status($status, $description)
    {
      $result = new stdClass();

      $result->description = $description;

      switch ($status) {
        case 'PENDING':
          $result->status = 'processing';
          break;
        case 'COMPLETED':
          $result->status = 'completed';
          break;
        case 'CANCELLED':
          $result->status = 'failed';
          break;
        case 'FAILED':
          $result->status = 'failed';
          break;
        case 'DECLINED':
          $result->status = 'failed';
          break;
        default:
          $result->status = '';
          $result->description = 'Unknown status.';
      }

      return $result;
    }

    // this function will be called when the user clicks on the button
    public function process_payment($order_id)
    {

      $order = wc_get_order($order_id);
      $options = get_option('woocommerce_paybotic_settings');

      $paybotic_api_key = $options['paybotic_api_key'];
      $paybotic_merchant_id = $options['paybotic_merchant_id'];
      $paybotic_ecommerce_platform_id = $options['paybotic_ecommerce_platform_id'];
      $paybotic_custom_fees = $options['paybotic_custom_fees'];

      $is_paid = $order->get_status();

      error_log('The order #' . $is_paid);

      $fees = $order->get_fees('Transaction Fee');
      $total_fees = 0;

      foreach ($fees as $fee) {
        $fee_name = $fee->get_name();

        if ($fee_name === 'Transaction Fee') {
          $total_fees = $fee->get_total();
        }
      }

      $data = array(
        'merchantId' => $paybotic_merchant_id,
        'orderId' => strval($order->id),
        'transactionAmount' => $order->total,
        'originalAmount' => $order->total - $total_fees,
        'street' => $order->get_billing_address_1(),
        'unit' => $order->get_billing_address_2(),
        'city' => $order->get_billing_city(),
        'state' => $order->get_billing_state(),
        'postalCode' => $order->get_billing_postcode(),
        'externalMerchantId' => $paybotic_merchant_id,
        'ecommercePlatformId' => $paybotic_ecommerce_platform_id,
        'feeType' => $paybotic_custom_fees,
      );

      $result = $this->execPost($this->init_payment_endpoint, $data, $paybotic_api_key);

      $first_name = '';
      $last_name = '';
      $email = '';
      $phone = '';
      $street = '';
      $unit = '';
      $city = '';
      $state = '';

      if ($order->get_billing_first_name()) {
        $first_name = $order->get_billing_first_name();
      }
      if ($order->get_billing_last_name()) {
        $last_name = $order->get_billing_last_name();
      }
      if ($order->get_billing_email()) {
        $email = $order->get_billing_email();
      }
      if ($order->get_billing_phone()) {
        $phone = $order->get_billing_phone();
      }
      if ($order->get_billing_address_1()) {
        $street = $order->get_billing_address_1();
      }
      if ($order->get_billing_address_2()) {
        $unit = $order->get_billing_address_2();
      }
      if ($order->get_billing_city()) {
        $city = $order->get_billing_city();
      }
      if ($order->get_billing_state()) {
        $state = $order->get_billing_state();
      }

      if (!$street && $order->get_shipping_address_1()) {
        $street = $order->get_shipping_address_1();
      }
      if (!$unit && $order->get_shipping_address_2()) {
        $unit = $order->get_shipping_address_2();
      }
      if (!$city && $order->get_shipping_city()) {
        $city = $order->get_shipping_city();
      }
      if (!$state && $order->get_shipping_state()) {
        $state = $order->get_shipping_state();
      }

      if ($result !== false) {
        $result_decode = json_decode($result);
        $callback_url = urlencode($result_decode->paymentButtonUrl . '&firstName=' . $first_name . '&lastName=' . $last_name . '&email=' . $email . '&phone=' . $phone . '&street=' . $street . '&unit=' . $unit . '&city=' . $city . '&state=' . $state);
        $orderId = urlencode($order->get_id());
        $orderKey = urlencode($order->get_order_key());

        return array(
          'result'   => 'success',
          'redirect' => home_url("checkout?callback_url={$callback_url}&orderId={$orderId}&orderKey={$orderKey}&firstName={$first_name}&lastName={$last_name}&email={$email}")
        );
      }
    }

    // this function will be called api check payment status from Paybotic
    public function sync_order_status($prop_order_id)
    {
      $instance = new WC_Paybotic_Gateway();

      $options = get_option('woocommerce_paybotic_settings');

      $paybotic_api_key = $options['paybotic_api_key'];
      $paybotic_ecommerce_platform_id = $options['paybotic_ecommerce_platform_id'];

      $order = wc_get_order($prop_order_id);
      $order_id = $order->get_id();

      $url = str_replace(
        array(':ecommercePlatformId', ':orderId'),
        array($paybotic_ecommerce_platform_id, $order_id),
        $instance->check_payment_status_endpoint
      );

      if (!$order_id) return false;

      $result = $instance->execGetOrderStatus($url, $paybotic_api_key);

      if ($result !== false) {
        $result_decode = json_decode($result);

        $is_processing = $result_decode->status === 'PENDING';
        $is_completed = $result_decode->status === 'COMPLETED';

        if ($result_decode->id && $is_processing || $is_completed) {
          $description = $is_processing ? 'The order has been successfully paid and is being processed.' : 'The order has been completed successfully.';

          $mapping_status = $instance->mapping_order_status($result_decode->status, $description);

          error_log('The order sync status - ' . $order_id . ' - ' . $mapping_status->status . ' - ' . $mapping_status->description);

          $order->update_status($mapping_status->status, $mapping_status->description);

          return true;
        }
      }

      return false;
    }

    public function calculate_order_fees($total_amount)
    {
      $options = get_option('woocommerce_paybotic_settings');

      $paybotic_api_key = $options['paybotic_api_key'];
      $paybotic_ecommerce_platform_id = $options['paybotic_ecommerce_platform_id'];
      $paybotic_custom_fees = $options['paybotic_custom_fees'];

      $data = array(
        'ecommercePlatformId' => $paybotic_ecommerce_platform_id,
        'type' => $paybotic_custom_fees,
        'totalAmount' => $total_amount,
      );

      return $this->execPost($this->check_fees_order_endpoint, $data, $paybotic_api_key);
    }
  }
}

add_action('wp_ajax_check_order_status', 'ajax_check_order_status');
add_action('wp_ajax_nopriv_check_order_status', 'ajax_check_order_status');

function ajax_check_order_status()
{
  $order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0;
  $order_key = isset($_POST['order_key']) ? $_POST['order_key'] : '';

  $instance = new WC_Paybotic_Gateway();
  $result = $instance->sync_order_status($order_id);

  if ($result) {
    wp_send_json_success(array('redirect' => home_url("checkout/order-received/{$order_id}/?key={$order_key}")));
  }
}

add_action('wp_ajax_paybotic_webhook_sync_status', 'paybotic_webhook_sync_status');
add_action('wp_ajax_nopriv_paybotic_webhook_sync_status', 'paybotic_webhook_sync_status');

function paybotic_webhook_sync_status()
{
  $data = isset($_POST['data']) ? $_POST['data'] : '';
  $instance = new WC_Paybotic_Gateway();

  if ($data && $data['object']) {
    $order_id = $data['object']['order_key'];
    $status = $data['object']['transaction_status'];
    $description = $data['object']['transaction_message'];

    $order = wc_get_order($order_id);
    $mapping_status = $instance->mapping_order_status(strtoupper($status), $description);

    if (!$mapping_status->status) return;

    error_log('The order webhook status - ' . $order_id . ' - ' . $mapping_status->status . ' - ' . $mapping_status->description);

    $order->update_status($mapping_status->status,  $mapping_status->description);
  }
}

// Add dialog to notify popup block by browser setting
function custom_popup_html()
{
?>
  <div class="pf-popup-backdrop"></div>
  <div class="pf-popup-content">
    <div class="pf-close-button"></div>
    <p>Click continue to proceed with payment via Paybotic Financial</p>
    <button class="pf-popup-action">Continue</button>
  </div>
<?php
}

add_action('wp_footer', 'custom_popup_html');

function custom_popup_payment_html()
{
?>
  <div class="pf-popup-payment-backdrop"></div>
  <div class="pf-popup-payment-content">
    <div class="pf-close-button"></div>
    <iframe class="pf-popup-payment-iframe"></iframe>
  </div>
<?php
}

add_action('wp_footer', 'custom_popup_payment_html');


function my_plugin_enqueue_styles()
{
  wp_enqueue_style('my-plugin-styles', plugins_url('assets/css/index.css', __FILE__));
}

add_action('wp_enqueue_scripts', 'my_plugin_enqueue_styles');

// add custom fees
add_filter('woocommerce_cart_calculate_fees', 'add_paybotic_custom_fees', 10, 1);

function add_paybotic_custom_fees($cart)
{
  $instance = new WC_Paybotic_Gateway();

  $type = $instance->get_option('paybotic_custom_fees');

  if ($type === 'charge_merchant') {
    return;
  }

  $subtotal = $cart->subtotal;
  $shipping_total = $cart->shipping_total;
  $tax_total = $cart->tax_total;

  $total_amount = 0;

  if ($subtotal) {
    $total_amount += $subtotal;
  }

  if ($shipping_total) {
    $total_amount += $shipping_total;
  }

  if ($tax_total) {
    $total_amount += $tax_total;
  }

  $result = $instance->calculate_order_fees($total_amount);

  if ($result !== false) {
    $result_decode = json_decode($result);
    $total_fees = $result_decode->totalFees;
    $total_fees_rounded = round($total_fees, 2);
    $title = 'Transaction Fee';

    error_log($title . ' - ' . $total_fees_rounded);

    $cart->add_fee($title, $total_fees_rounded);
  }
}

?>

Function Calls

add_filter 1

Variables

None

Stats

MD5 f49b71479fbb01986049fdd86599b53c
Eval Count 0
Decode Time 87 ms