Plugin Directory

Changeset 3112122


Ignore:
Timestamp:
07/04/2024 05:47:06 AM (21 months ago)
Author:
devcashfree
Message:

Update to version 4.7.2

Location:
cashfree
Files:
30 added
3 edited

Legend:

Unmodified
Added
Removed
  • cashfree/trunk/cashfree.php

    r3108628 r3112122  
    22/**
    33 * Plugin Name: Cashfree
    4  * Version: 4.7.1
     4 * Version: 4.7.2
    55 * Plugin URI: https://github.com/cashfree/cashfree-woocommerce
    66 * Description: Payment gateway plugin by Cashfree Payments for Woocommerce sites.
  • cashfree/trunk/includes/http/class-wc-cashfree-adapter.php

    r3101786 r3112122  
    1111class WC_Cashfree_Adapter {
    1212
    13     /**
    14      * Cashfree gateway instance.
    15      *
    16      * @var WC_Cashfree_Gateway
    17      */
    18     protected $gateway;
    19 
    20     const API_VERSION_20220901 = '2022-09-01';
    21 
    22     /**
    23      * Constructor.
    24      *
    25      * @param WC_Cashfree_Gateway $gateway Cashfree gateway instance.
    26      */
    27     public function __construct( $gateway ) {
    28         $this->gateway = $gateway;
    29     }
    30 
    31     /**
    32      * Create a checkout using the given order ID.
    33      *
    34      * @param string $order_id Order ID.
    35      *
    36      * @return array An array containing the order token and environment.
    37      *
    38      * @throws Exception If an error occurs while creating the checkout.
    39      */
    40     public function checkout( $order_id ) {
    41         require_once WC_CASHFREE_DIR_PATH . 'includes/request/class-wc-cashfree-request-checkout.php';
    42         $cf_order_id = $order_id;
    43 
    44         // Get the Cashfree URL and set the order URL.
    45         $env_value = $this->getCurlValue();
    46         if($this->gateway->settings['order_id_prefix_text'] == "yes"){
    47             $encoded_string = md5(home_url());
    48             $order_id_prefix_text = substr($encoded_string, 0, 4);
    49             $cf_order_id = $order_id_prefix_text.'_'.$order_id;
    50         }
    51         $order_url = $env_value['curlUrl'] . '/' . $cf_order_id;
    52 
    53         // Set the request headers.
    54         $args = array(
    55             'timeout' => 30,
    56             'headers' => array(
    57                 'x-api-version'   => self::API_VERSION_20220901,
    58                 'x-client-id'     => $this->gateway->settings['app_id'],
    59                 'x-client-secret' => $this->gateway->settings['secret_key'],
    60             ),
    61         );
    62 
    63         // Make the request to get the order.
    64         $response = wp_remote_get( $order_url, $args );
    65         $http_code = wp_remote_retrieve_response_code( $response );
    66 
    67         // Check if the request was successful.
    68         if ( $http_code === 200 ) {
    69             $cf_order = json_decode( wp_remote_retrieve_body( $response ) );
    70 
    71             // Check if the order has already been paid for.
    72             if ( $cf_order->order_status === 'PAID' ) {
    73                 throw new Exception( 'Please reach out to the support team' );
    74             }
    75 
    76             // Check if the order details are correct.
    77             if (
    78                 strtotime( $cf_order->order_expiry_time ) > time()
    79                 && round( $cf_order->order_amount ) === round( wc_get_order( $order_id )->get_total() )
    80                 && $cf_order->order_currency === wc_get_order( $order_id )->get_currency()
    81             ) {
    82                 $cashfree_wc_response = array(
    83                     'payment_session_id' => $cf_order->payment_session_id,
    84                     'environment' => $env_value['environment'],
     13    /**
     14     * Cashfree gateway instance.
     15     *
     16     * @var WC_Cashfree_Gateway
     17     */
     18    protected $gateway;
     19
     20    const API_VERSION_20220901 = '2022-09-01';
     21
     22    /**
     23     * Constructor.
     24     *
     25     * @param WC_Cashfree_Gateway $gateway Cashfree gateway instance.
     26     */
     27    public function __construct( $gateway ) {
     28        $this->gateway = $gateway;
     29    }
     30
     31    /**
     32     * Create a checkout using the given order ID.
     33     *
     34     * @param string $order_id Order ID.
     35     *
     36     * @return array An array containing the order token and environment.
     37     *
     38     * @throws Exception If an error occurs while creating the checkout.
     39     */
     40    public function checkout( $order_id ) {
     41        require_once WC_CASHFREE_DIR_PATH . 'includes/request/class-wc-cashfree-request-checkout.php';
     42        $cf_order_id = $order_id;
     43
     44        // Get the Cashfree URL and set the order URL.
     45        $env_value = $this->getCurlValue();
     46        if($this->gateway->settings['order_id_prefix_text'] == "yes"){
     47            $encoded_string = md5(home_url());
     48            $order_id_prefix_text = substr($encoded_string, 0, 4);
     49            $cf_order_id = $order_id_prefix_text.'_'.$order_id;
     50        }
     51        $order_url = $env_value['curlUrl'] . '/' . $cf_order_id;
     52
     53        // Set the request headers.
     54        $args = array(
     55            'timeout' => 30,
     56            'headers' => array(
     57                'x-api-version'   => self::API_VERSION_20220901,
     58                'x-client-id'     => $this->gateway->settings['app_id'],
     59                'x-client-secret' => $this->gateway->settings['secret_key'],
     60                'x-request-id' => 'cf-woo-'.$cf_order_id.'-'.time().'-',
     61            ),
     62        );
     63
     64        // Make the request to get the order.
     65        $response = wp_remote_get( $order_url, $args );
     66        $http_code = wp_remote_retrieve_response_code( $response );
     67
     68        // Check if the request was successful.
     69        if ( $http_code === 200 ) {
     70            $cf_order = json_decode( wp_remote_retrieve_body( $response ) );
     71
     72            // Check if the order has already been paid for.
     73            if ( $cf_order->order_status === 'PAID' ) {
     74                throw new Exception( 'Please reach out to the support team' );
     75            }
     76
     77            // Check if the order details are correct.
     78            if (
     79                strtotime( $cf_order->order_expiry_time ) > time()
     80                && round( $cf_order->order_amount ) === round( wc_get_order( $order_id )->get_total() )
     81                && $cf_order->order_currency === wc_get_order( $order_id )->get_currency()
     82            ) {
     83                $cashfree_wc_response = array(
     84                    'payment_session_id' => $cf_order->payment_session_id,
     85                    'environment' => $env_value['environment'],
    8586                    'order_id' => $cf_order_id,
    86                 );
    87 
    88                 return $cashfree_wc_response;
    89             } else {
    90                 throw new Exception( 'Please reach out to the support team' );
    91             }
    92         }
    93 
    94         // Build the request params.
    95         $request_params = WC_Cashfree_Request_Checkout::build( $order_id, $this->gateway, $cf_order_id );
    96 
    97         // If the order is not found, create a new checkout.
    98         $curl_post_field = json_encode( $request_params );
    99 
    100         try {
    101             $result = $this->curlPostRequest( $env_value['curlUrl'], $curl_post_field, self::API_VERSION_20220901, $request_params['order_id'] );
    102             $cashfree_wc_response = array(
    103                 'payment_session_id' => $result->payment_session_id,
    104                 'environment' => $env_value['environment'],
     87                );
     88
     89                return $cashfree_wc_response;
     90            } else {
     91                throw new Exception( 'Please reach out to the support team' );
     92            }
     93        }
     94
     95        // Build the request params.
     96        $request_params = WC_Cashfree_Request_Checkout::build( $order_id, $this->gateway, $cf_order_id );
     97
     98        // If the order is not found, create a new checkout.
     99        $curl_post_field = json_encode( $request_params );
     100
     101        try {
     102            $result = $this->curlPostRequest( $env_value['curlUrl'], $curl_post_field, self::API_VERSION_20220901, $request_params['order_id'] );
     103            $cashfree_wc_response = array(
     104                'payment_session_id' => $result->payment_session_id,
     105                'environment' => $env_value['environment'],
    105106                'order_id' => $cf_order_id,
    106             );
    107 
    108             return $cashfree_wc_response;
    109         } catch ( Exception $e ) {
    110             throw new Exception( $e->getMessage() );
    111         }
    112     }
    113 
    114     /**
    115      * Capture an order.
    116      *
    117      * @param array $postData post data.
    118      *
    119      * @return array
    120      *
    121      * @throws Exception If payment method is not properly configured or response status code is invalid.
    122      */
    123     public function capture(array $postData)
    124     {
    125         $curlValue = $this->getCurlValue();
    126         $cfOrderId = $postData['order_id'];
    127        
    128         $orderUrl = $curlValue['curlUrl'] . '/' . $cfOrderId . '/payments';
    129         $result = $this->curlGetRequest($orderUrl);
    130        
    131         return $result;
    132     }
    133 
    134     /**
    135      * Notify an order.
    136      *
    137      * @param array $post_data post data.
    138      *
    139      * @return array
    140      *
    141      * @throws Exception If payment method is not properly configured.
    142      * @throws ApiException If response status code is invalid.
    143      */
    144     public function notify( $post_data ) {
    145         return $this->capture( $post_data );
    146     }
    147 
    148     /**
    149      * Refund a capture transaction.
    150      *
    151      * @param string $id          Transaction id.
    152      * @param string $refund_id   Refund id.
    153      * @param float  $amount      Amount to refund.
    154      * @param string $description Refund description.
    155      *
    156      * @return array
    157      *
    158      * @throws Exception If payment method is not properly configured.
    159      * @throws ApiException If response status code is invalid.
    160      */
    161     public function refund( $order_id, $refund_id, $amount, $description ) {
    162         $getEnvValue = $this->getCurlValue();
    163         $cartData = array(
    164             'refund_amount' => $amount,
    165             'refund_id'     => $refund_id,
    166             'refund_note'   => $description,
    167         );
    168         $curlPostfield = json_encode($cartData); 
    169        
    170         $cf_order_id = $order_id;
    171 
    172         if($this->gateway->settings['order_id_prefix_text'] == "yes"){
    173             $encoded_string = md5(home_url());
    174             $order_id_prefix_text = substr($encoded_string, 0, 4);
    175             $cf_order_id = $order_id_prefix_text.'_'.$order_id;
    176         }
    177    
    178         $refundUrl = $getEnvValue['curlUrl']."/".$cf_order_id."/refunds";
    179        
    180         try{
    181             $result = $this->curlPostRequest($refundUrl, $curlPostfield, self::API_VERSION_20220901);
    182             return $result;
    183         } catch(Exception $e) {
    184             throw new Exception($e->getMessage());
    185         }
    186 
    187     }
    188 
    189     // Get config values for gateway environment
    190     public function getCurlValue() {
    191         $isSandbox = $this->gateway->settings['sandbox'] === 'yes';
    192         $baseUrl = $isSandbox ? 'https://sandbox.cashfree.com' : 'https://api.cashfree.com';
    193    
    194         return [
    195             'curlUrl' => "{$baseUrl}/pg/orders",
    196             'environment' => $isSandbox ? 'sandbox' : 'production'
    197         ];
    198     }   
    199 
    200     // Post request for gateway
    201     private function curlPostRequest($curlUrl, $data, $apiVersion, $idemKey = "") {
    202         $headers = [
    203             'Accept' => 'application/json',
    204             'Content-Type' => 'application/json',
    205             'x-api-version' => $apiVersion,
    206             'x-client-id' => $this->gateway->settings['app_id'],
    207             'x-client-secret' => $this->gateway->settings['secret_key']
    208         ];
    209        
    210         if(!empty($idemKey)) {
    211             $headers['x-idempotency-key'] = $idemKey;
    212         }
    213        
    214         $args = [
    215             'body'        => $data,
    216             'timeout'     => 30,
    217             'headers'     => $headers,
    218         ];
    219 
    220         $response = wp_remote_post( $curlUrl, $args );
    221         $http_code = wp_remote_retrieve_response_code( $response );
    222         $body     = json_decode(wp_remote_retrieve_body( $response ));
    223 
    224         if($http_code === 200) {
    225             return $body;
    226         } else {
    227             throw new Exception($body->message);
    228         }
    229     }
    230 
    231     // Get request for gateway
    232     private function curlGetRequest($curlUrl) {
    233         $args = array(
    234             'timeout'     => '30',
    235             'headers'     => array(
    236                 'x-api-version'     =>  self::API_VERSION_20220901,
    237                 'x-client-id'       =>  $this->gateway->settings['app_id'],
    238                 'x-client-secret'   =>  $this->gateway->settings['secret_key'],
    239             ),
    240         );
    241 
    242         $response = wp_remote_get( $curlUrl, $args );
    243 
    244         $http_code = wp_remote_retrieve_response_code( $response );
    245         $body     = json_decode(wp_remote_retrieve_body( $response ));
    246         if($http_code == 200) {
    247             return $body[0];   
    248         } else {
    249             throw new Exception($body[0]->payment_message);
    250         }
    251        
    252     }
     107            );
     108
     109            return $cashfree_wc_response;
     110        } catch ( Exception $e ) {
     111            throw new Exception( $e->getMessage() );
     112        }
     113    }
     114
     115    /**
     116     * Capture an order.
     117     *
     118     * @param array $postData post data.
     119     *
     120     * @return array
     121     *
     122     * @throws Exception If payment method is not properly configured or response status code is invalid.
     123     */
     124    public function capture(array $postData)
     125    {
     126        $curlValue = $this->getCurlValue();
     127        $cfOrderId = $postData['order_id'];
     128
     129        $orderUrl = $curlValue['curlUrl'] . '/' . $cfOrderId . '/payments';
     130        $result = $this->curlGetRequest($orderUrl);
     131
     132        return $result;
     133    }
     134
     135    /**
     136     * Notify an order.
     137     *
     138     * @param array $post_data post data.
     139     *
     140     * @return array
     141     *
     142     * @throws Exception If payment method is not properly configured.
     143     * @throws ApiException If response status code is invalid.
     144     */
     145    public function notify( $post_data ) {
     146        return $this->capture( $post_data );
     147    }
     148
     149    /**
     150     * Refund a capture transaction.
     151     *
     152     * @param string $order_id    order id.
     153     * @param string $refund_id   Refund id.
     154     * @param float  $amount      Amount to refund.
     155     * @param string $description Refund description.
     156     *
     157     * @return array
     158     *
     159     * @throws Exception If payment method is not properly configured.
     160     * @throws ApiException If response status code is invalid.
     161     */
     162    public function refund( $order_id, $refund_id, $amount, $description ) {
     163        $getEnvValue = $this->getCurlValue();
     164        $cartData = array(
     165            'refund_amount' => $amount,
     166            'refund_id'     => $refund_id,
     167            'refund_note'   => $description,
     168        );
     169        $curlPostfield = json_encode($cartData);
     170
     171        $cf_order_id = $order_id;
     172
     173        if($this->gateway->settings['order_id_prefix_text'] == "yes"){
     174            $encoded_string = md5(home_url());
     175            $order_id_prefix_text = substr($encoded_string, 0, 4);
     176            $cf_order_id = $order_id_prefix_text.'_'.$order_id;
     177        }
     178
     179        $refundUrl = $getEnvValue['curlUrl']."/".$cf_order_id."/refunds";
     180
     181        try{
     182            $result = $this->curlPostRequest($refundUrl, $curlPostfield, self::API_VERSION_20220901, $cf_order_id);
     183            return $result;
     184        } catch(Exception $e) {
     185            throw new Exception($e->getMessage());
     186        }
     187
     188    }
     189
     190    // Get config values for gateway environment
     191    public function getCurlValue() {
     192        $isSandbox = $this->gateway->settings['sandbox'] === 'yes';
     193        $baseUrl = $isSandbox ? 'https://sandbox.cashfree.com' : 'https://api.cashfree.com';
     194
     195        return [
     196            'curlUrl' => "{$baseUrl}/pg/orders",
     197            'environment' => $isSandbox ? 'sandbox' : 'production'
     198        ];
     199    }
     200
     201    // Post request for gateway
     202    private function curlPostRequest($curlUrl, $data, $apiVersion, $cf_orderId) {
     203        $headers = [
     204            'Accept' => 'application/json',
     205            'Content-Type' => 'application/json',
     206            'x-api-version' => $apiVersion,
     207            'x-client-id' => $this->gateway->settings['app_id'],
     208            'x-client-secret' => $this->gateway->settings['secret_key'],
     209            'x-request-id' => 'cf-woo-'.$cf_orderId.'-'.time().'-',
     210        ];
     211
     212        $args = [
     213            'body'        => $data,
     214            'timeout'     => 30,
     215            'headers'     => $headers,
     216        ];
     217
     218        $response = wp_remote_post( $curlUrl, $args );
     219        $http_code = wp_remote_retrieve_response_code( $response );
     220        $body     = json_decode(wp_remote_retrieve_body( $response ));
     221
     222        if($http_code === 200) {
     223            return $body;
     224        } else {
     225            throw new Exception($body->message);
     226        }
     227    }
     228
     229    // Get request for gateway
     230    private function curlGetRequest($curlUrl) {
     231        $args = array(
     232            'timeout'     => '30',
     233            'headers'     => array(
     234                'x-api-version'     =>  self::API_VERSION_20220901,
     235                'x-client-id'       =>  $this->gateway->settings['app_id'],
     236                'x-client-secret'   =>  $this->gateway->settings['secret_key']
     237            ),
     238        );
     239
     240        $response = wp_remote_get( $curlUrl, $args );
     241
     242        $http_code = wp_remote_retrieve_response_code( $response );
     243        $body     = json_decode(wp_remote_retrieve_body( $response ));
     244        if($http_code == 200) {
     245            return $body[0];
     246        } else {
     247            throw new Exception($body[0]->payment_message);
     248        }
     249
     250    }
    253251}
  • cashfree/trunk/readme.txt

    r3108628 r3112122  
    44Tested up to: 6.5
    55Requires PHP: 5.6
    6 Stable tag: 4.7.1
    7 Version: 4.7.1
     6Stable tag: 4.7.2
     7Version: 4.7.2
    88License: GPLv3
    99License URI: https://www.gnu.org/licenses/gpl-3.0.html
     
    5050
    5151== Changelog ==
     52
     53= 4.7.2 =
     54* Added additional data to get trace of the request
    5255
    5356= 4.7.1 =
Note: See TracChangeset for help on using the changeset viewer.