Changeset 1976669
- Timestamp:
- 11/19/2018 09:39:10 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cashfree/tags/1.0/woocommerce-cashfree.php
r1952569 r1976669 28 28 // If we made it this far, then include our Gateway Class 29 29 class WC_Gateway_cashfree extends WC_Payment_Gateway { 30 30 31 31 // Setup our Gateway's id, description and other values 32 32 function __construct() { 33 34 35 36 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'; 37 37 $this->method_title = __( "Cashfree", 'wc_gateway_cashfree' ); 38 38 $this->method_description = "Cashfree payment gateway redirects customers to checkout page to fill in their payment details and complete the payment"; 39 39 $this->title = __( "Cashfree", 'wc_gateway_cashfree' ); 40 41 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']; 47 47 $this->title = $this->settings['title']; 48 48 … … 52 52 } 53 53 } 54 55 54 55 56 56 // Build the administration fields for this specific Gateway 57 57 public function init_form_fields() { … … 78 78 'desc_tip' => true 79 79 ), 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' ) 85 94 ), 86 95 'app_id' => array( … … 95 104 'description' => __('Copy from your dashboard or contact Cashfree Team', 'wc_gateway_cashfree'), 96 105 'desc_tip' => true 97 ), 106 ), 98 107 ); 99 108 } 100 109 101 110 function check_cashfree_response(){ … … 109 118 } 110 119 $orderId = sanitize_text_field($_POST["orderId"]); 111 112 120 $order = new WC_Order($orderId); 121 if ($order && $order->get_status() == "pending") { 113 122 $cashfree_response = array(); 114 123 $cashfree_response["orderId"] = $orderId; … … 120 129 $cashfree_response["paymentMode"] = sanitize_text_field($_POST["paymentMode"]); 121 130 $cashfree_response["signature"] = sanitize_text_field($_POST["signature"]); 122 131 123 132 $secret_key = $this->secret_key; 124 133 $data = "{$cashfree_response['orderId']}{$cashfree_response['orderAmount']}{$cashfree_response['referenceId']}{$cashfree_response['txStatus']}{$cashfree_response['paymentMode']}{$cashfree_response['txMsg']}{$cashfree_response['txTime']}"; … … 127 136 if ($cashfree_response["signature"] != $computedSignature) { 128 137 //error 129 138 die(); 130 139 } 131 140 … … 137 146 $woocommerce -> cart -> empty_cart(); 138 147 $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'; 140 149 } else if ($cashfree_response["txStatus"] == "CANCELLED") { 141 150 $order->update_status( 'failed', __( 'Payment has been cancelled.', 'woocommerce' )); 142 151 $this->msg['class'] = 'woocommerce-error'; 143 152 $this->msg['message'] = "Your transaction has been cancelled. Please try again."; 144 153 } else if ($cashfree_response["txStatus"] == "PENDING") { 145 154 $order->update_status( 'failed', __( 'Payment is under review.', 'woocommerce' )); 146 155 $this->msg['class'] = 'woocommerce-error'; 147 156 $this->msg['message'] = "Your transaction is under review. Please wait for an status update."; 148 157 } else { 149 158 $order->update_status( 'failed', __( 'Payment Failed', 'woocommerce' )); 150 159 $this->msg['class'] = 'woocommerce-error'; 151 160 $this->msg['message'] = "Your transaction has failed."; 152 161 } 153 162 if ($showContent) { 154 163 add_action('the_content', array(&$this, 'showMessage')); 155 164 } 156 } 165 } 157 166 } 158 167 } 159 168 160 161 169 public function getEnvironment() 170 { 171 $environment = $this->get_option( 'environment' ) === 'sandbox' ? 'sandbox' : 'production'; 172 return $environment; 173 } 174 162 175 function showMessage ($content) { 163 176 return '<div class="woocommerce"><div class="'.$this->msg['class'].'">'.$this->msg['message'].'</div></div>'.$content; 164 177 } 165 166 178 179 // Submit payment and handle response 167 180 public function process_payment( $order_id ) { 168 181 global $woocommerce; 169 182 global $wpdb; 170 183 global $current_user; 171 184 //get user details 172 185 $current_user = wp_get_current_user(); 173 186 $user_email = $current_user->user_email; … … 183 196 $first_name = sanitize_text_field($_POST['billing_first_name']); 184 197 $last_name = sanitize_text_field($_POST['billing_last_name']); 185 $phone_number = sanitize_text_field($_POST['billing_phone']); 186 $customerName 198 $phone_number = sanitize_text_field($_POST['billing_phone']); 199 $customerName = $first_name." ".$last_name; 187 200 $customerEmail = $user_email; 188 201 $customerPhone = $phone_number; … … 192 205 $this->return_url = add_query_arg(array('cashfree_callback' => 1), $this->get_return_url( $order)); 193 206 $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 196 210 $cf_request["appId"] = $this->app_id; 197 211 $cf_request["secretKey"] = $this->secret_key; … … 205 219 $cf_request["returnUrl"] = $this->return_url; 206 220 $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 } 211 227 $apiEndpoint = $apiEndpoint."/api/v1/order/create"; 212 228 $postBody = array("body" => $cf_request, "timeout" => $timeout); … … 223 239 } 224 240 } 225 226 // Now that we have successfully included our class, 227 // Lets add it to WooCommerce 241 228 242 add_filter( 'woocommerce_payment_gateways', 'add_cashfree_gateway' ); 229 243 230 244 function add_cashfree_gateway( $methods ) { 231 245 $methods[] = 'WC_Gateway_cashfree'; 232 233 246 return $methods; 247 } 234 248 }
Note: See TracChangeset
for help on using the changeset viewer.