Plugin Directory

Changeset 1976669


Ignore:
Timestamp:
11/19/2018 09:39:10 AM (6 years ago)
Author:
sharroh
Message:

adding test and live mode

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cashfree/tags/1.0/woocommerce-cashfree.php

    r1952569 r1976669  
    2828  // If we made it this far, then include our Gateway Class
    2929  class WC_Gateway_cashfree extends WC_Payment_Gateway {
    30        
     30   
    3131    // Setup our Gateway's id, description and other values
    3232    function __construct() {
    33         global $woocommerce;
    34         global $wpdb;
    35         $this->id = "cashfree";
    36         $this->icon = 'https://s3-ap-southeast-1.amazonaws.com/cfreeimages/cf-mailer-logo.png';
     33      global $woocommerce;
     34      global $wpdb;
     35      $this->id = "cashfree";
     36      $this->icon = 'https://s3-ap-southeast-1.amazonaws.com/cfreeimages/cf-mailer-logo.png';
    3737      $this->method_title = __( "Cashfree", 'wc_gateway_cashfree' );
    3838      $this->method_description = "Cashfree payment gateway redirects customers to checkout page to fill in their payment details and complete the payment";
    3939      $this->title = __( "Cashfree", 'wc_gateway_cashfree' );
    40         $this->has_fields = false;
    41         $this->init_form_fields();
    42         $this->init_settings();
    43         $this->api_url              = $this->settings['api_url'];
    44         $this->app_id       = $this->settings['app_id'];
    45         $this->secret_key       = $this->settings['secret_key'];
    46         $this->description      = $this->settings['description'];
     40      $this->has_fields = false;
     41      $this->init_form_fields();
     42      $this->init_settings();     
     43      $this->environment         = $this->settings['environment'];
     44      $this->app_id     = $this->settings['app_id'];
     45      $this->secret_key     = $this->settings['secret_key'];
     46      $this->description    = $this->settings['description'];
    4747        $this->title = $this->settings['title'];
    4848
     
    5252      }
    5353    }
    54    
    55    
     54 
     55 
    5656  // Build the administration fields for this specific Gateway
    5757    public function init_form_fields() {
     
    7878                    'desc_tip'         => true
    7979                ),
    80                 'api_url' => array(
    81                     'title'         => __('API Endpoint', 'wc_gateway_cashfree'),
    82                     'type'             => 'text',
    83                     'description'     => __('Refer API documentation or contact Cashfree Team ', 'wc_gateway_cashfree'),
    84                     'desc_tip'         => true
     80                'environment' => array (
     81                    'type' => 'select',
     82                    'options' => array (
     83                        'sandbox' => __ ( 'Test Mode', 'wc_gateway_cashfree' ),
     84                        'production' => __ ( 'Live Mode', 'wc_gateway_cashfree' )
     85                    ),
     86                    'default' => 'sandbox',
     87                    'title' => __ ( 'Active Environment', 'wc_gateway_cashfree' ),
     88                    'class' => array (
     89                        'wc_gateway_cashfree-active-environment'
     90                    ),
     91                    'tool_tip' => true,
     92                    'description' => __ ( 'You can enable Test mode or Live mode with this setting. When testing the plugin, enable Test mode and you can run test transactions using your Cashfree account.
     93                      When you are ready to go live, enable Live mode.', 'wc_gateway_cashfree' )
    8594                ),
    8695                'app_id' => array(
     
    95104                    'description'     => __('Copy from your dashboard or contact Cashfree Team', 'wc_gateway_cashfree'),
    96105                    'desc_tip'         => true
    97                 ),
     106                ),               
    98107            );
    99       }
     108    }
    100109
    101110    function check_cashfree_response(){
     
    109118          }
    110119        $orderId = sanitize_text_field($_POST["orderId"]);
    111           $order = new WC_Order($orderId);
    112           if ($order && $order->get_status() == "pending") {
     120        $order = new WC_Order($orderId);
     121        if ($order && $order->get_status() == "pending") {
    113122          $cashfree_response = array();
    114123          $cashfree_response["orderId"] = $orderId;
     
    120129          $cashfree_response["paymentMode"] = sanitize_text_field($_POST["paymentMode"]);
    121130          $cashfree_response["signature"] = sanitize_text_field($_POST["signature"]);
    122    
     131 
    123132          $secret_key = $this->secret_key;
    124133          $data = "{$cashfree_response['orderId']}{$cashfree_response['orderAmount']}{$cashfree_response['referenceId']}{$cashfree_response['txStatus']}{$cashfree_response['paymentMode']}{$cashfree_response['txMsg']}{$cashfree_response['txTime']}";
     
    127136          if ($cashfree_response["signature"] != $computedSignature) {
    128137                 //error
    129                 die();
     138                die();
    130139          }
    131140
     
    137146            $woocommerce -> cart -> empty_cart();
    138147            $this->msg['message'] = "Thank you for shopping with us. Your payment has been confirmed. Cashfree reference id is: <b>".$cashfree_response["referenceId"]."</b>.";
    139             $this->msg['class'] = 'woocommerce-message';           
     148            $this->msg['class'] = 'woocommerce-message';     
    140149          } else if ($cashfree_response["txStatus"] == "CANCELLED") {
    141150            $order->update_status( 'failed', __( 'Payment has been cancelled.', 'woocommerce' ));
    142151            $this->msg['class'] = 'woocommerce-error';
    143                     $this->msg['message'] = "Your transaction has been cancelled. Please try again.";
     152            $this->msg['message'] = "Your transaction has been cancelled. Please try again.";
    144153          } else if ($cashfree_response["txStatus"] == "PENDING") {
    145154            $order->update_status( 'failed', __( 'Payment is under review.', 'woocommerce' ));
    146155            $this->msg['class'] = 'woocommerce-error';
    147                     $this->msg['message'] = "Your transaction is under review. Please wait for an status update.";
     156            $this->msg['message'] = "Your transaction is under review. Please wait for an status update.";
    148157          } else {
    149158            $order->update_status( 'failed', __( 'Payment Failed', 'woocommerce' ));
    150159            $this->msg['class'] = 'woocommerce-error';
    151                     $this->msg['message'] = "Your transaction has failed.";
     160            $this->msg['message'] = "Your transaction has failed.";
    152161          }
    153162          if ($showContent) {
    154163                add_action('the_content', array(&$this, 'showMessage'));
    155164          }
    156         }               
     165        }       
    157166      }
    158167    }
    159168
    160    
    161    
     169   public function getEnvironment()
     170  {
     171    $environment = $this->get_option( 'environment' ) === 'sandbox' ? 'sandbox' : 'production';
     172    return $environment;
     173  }
     174 
    162175    function showMessage ($content) {
    163176       return '<div class="woocommerce"><div class="'.$this->msg['class'].'">'.$this->msg['message'].'</div></div>'.$content;
    164177    }
    165    
    166       // Submit payment and handle response
     178 
     179    // Submit payment and handle response
    167180    public function process_payment( $order_id ) {
    168181        global $woocommerce;
    169182        global $wpdb;
    170183        global $current_user;
    171             //get user details   
     184        //get user details   
    172185        $current_user = wp_get_current_user();
    173186        $user_email = $current_user->user_email;
     
    183196          $first_name = sanitize_text_field($_POST['billing_first_name']);
    184197          $last_name  = sanitize_text_field($_POST['billing_last_name']);
    185           $phone_number = sanitize_text_field($_POST['billing_phone']);         
    186           $customerName = $first_name." ".$last_name;
     198          $phone_number = sanitize_text_field($_POST['billing_phone']);     
     199          $customerName = $first_name." ".$last_name;
    187200          $customerEmail = $user_email;
    188201          $customerPhone = $phone_number;
     
    192205        $this->return_url = add_query_arg(array('cashfree_callback' => 1), $this->get_return_url( $order));
    193206        $this->notify_url = add_query_arg(array('cashfree_callback' => 1, 'ipn' => '1'), $this->get_return_url( $order));
    194        
    195           $cf_request = array();
     207   
     208        $cf_request = array();
     209       
    196210        $cf_request["appId"] =  $this->app_id;
    197211        $cf_request["secretKey"] = $this->secret_key;
     
    205219        $cf_request["returnUrl"] = $this->return_url;
    206220        $cf_request["notifyUrl"] = $this->notify_url;
    207         $timeout = 30;
    208  
    209         $apiEndpoint = $this->api_url;
    210         $apiEndpoint = rtrim($apiEndpoint, "/");
     221        $timeout = 30;               
     222        if ( $this->getEnvironment() === 'sandbox' ) {
     223          $apiEndpoint = "https://test.cashfree.com";
     224        } elseif ($this->getEnvironment() === 'production') {
     225          $apiEndpoint = "https://api.cashfree.com";
     226        }
    211227        $apiEndpoint = $apiEndpoint."/api/v1/order/create";
    212228        $postBody = array("body" => $cf_request, "timeout" => $timeout);
     
    223239    }
    224240  }
    225    
    226     // Now that we have successfully included our class,
    227     // Lets add it to WooCommerce
     241 
    228242  add_filter( 'woocommerce_payment_gateways', 'add_cashfree_gateway' );
    229243 
    230244  function add_cashfree_gateway( $methods ) {
    231245    $methods[] = 'WC_Gateway_cashfree';
    232         return $methods;
    233     }
     246    return $methods;
     247  }
    234248}
Note: See TracChangeset for help on using the changeset viewer.