@@ -8,112 +8,118 @@ class Paytm
88{
99 /**
1010 * Generate a transaction token for the PayTM PG.
11- *
11+ *
1212 * @param float $amount
1313 * @param array $customer
14+ *
1415 * @return array $result
1516 */
1617 public function getTransactionToken ($ amount , $ customer )
1718 {
1819 // Generate a unique order id using the order_id_prefix.
19- $ newOrderId = config ('paytm.order_id_prefix ' ) . time ();
20-
20+ $ newOrderId = config ('paytm.order_id_prefix ' ). time ();
21+
2122 // Prepare the JSON string for request.
22- $ paytmParams ["body " ] = array (
23- "requestType " => "Payment " , // Request Type
24- "mid " => config ('paytm.merchant.id ' ), // Paytm Merchant ID
25- "websiteName " => config ('paytm.website ' ), // Paytm Website
26- "orderId " => $ newOrderId ,
27- "callbackUrl " => config ('paytm.callback_url ' ), // Callback url for processing responses from paytm
28- "txnAmount " => array (
29- "value " => $ amount ,
30- "currency " => "INR " , // Only INR Supported
31- ),
32- "userInfo " => array (
33- "custId " => $ customer ['custId ' ], // Mandatory
34- 'mobile ' => $ customer ['mobile ' ] ?? null , // Optional
35- 'email ' => $ customer ['email ' ] ?? null , // Optional
36- 'firstName ' => $ customer ['firstName ' ] ?? null , // Optional
37- 'lastName ' => $ customer ['lastName ' ] ?? null , // Optional
38- ),
39- );
40-
41- // Generate a unique signature using PaytmChecksumhash.
42- $ generateSignature = PaytmChecksum::generateSignature (json_encode ($ paytmParams ['body ' ], JSON_UNESCAPED_SLASHES ), config ('paytm.merchant.key ' ));
43-
44- // Add the generated signature to the parameters for posting.
45- $ paytmParams ["head " ] = array ("signature " => $ generateSignature );
46-
47- // Set the api endpoint for the local, testing or staging environment
48- if (config ('paytm.environment ' ) == 'testing ' )
49- $ url = config ('paytm.url.testing ' )."/theia/api/v1/initiateTransaction?mid= " . config ('paytm.merchant.id ' ) ."&orderId= " . $ newOrderId ;
50-
51- // Set the api endpoint for the production environment
52- elseif (config ('paytm.environment ' == 'production ' ))
53- $ url = config ('paytm.url.production ' )."/theia/api/v1/initiateTransaction?mid= " . config ('paytm.merchant.id ' ) ."&orderId= " . $ newOrderId ;
54-
55- // Send an api request to PayTM.
56- $ response = $ this ->getcURLRequest ($ url , $ paytmParams );
57-
58- // Evaluate the response and return appropriate result.
59- if (!empty ($ response ['body ' ]['resultInfo ' ]['resultStatus ' ]) && $ response ['body ' ]['resultInfo ' ]['resultStatus ' ] == 'S ' ) {
60- $ result = array ('success ' => true , 'orderId ' => $ newOrderId , 'txnToken ' => $ response ['body ' ]['txnToken ' ], 'amount ' => $ amount , 'message ' => $ response ['body ' ]['resultInfo ' ]['resultMsg ' ]);
61- }
62- else {
63- $ result = array ('success ' => false , 'orderId ' => $ newOrderId , 'txnToken ' => '' , 'amount ' => $ amount , 'message ' => $ response ['body ' ]['resultInfo ' ]['resultMsg ' ]);
64- }
23+ $ paytmParams ['body ' ] = [
24+ 'requestType ' => 'Payment ' , // Request Type
25+ 'mid ' => config ('paytm.merchant.id ' ), // Paytm Merchant ID
26+ 'websiteName ' => config ('paytm.website ' ), // Paytm Website
27+ 'orderId ' => $ newOrderId ,
28+ 'callbackUrl ' => config ('paytm.callback_url ' ), // Callback url for processing responses from paytm
29+ 'txnAmount ' => [
30+ 'value ' => $ amount ,
31+ 'currency ' => 'INR ' , // Only INR Supported
32+ ],
33+ 'userInfo ' => [
34+ 'custId ' => $ customer ['custId ' ], // Mandatory
35+ 'mobile ' => $ customer ['mobile ' ] ?? null , // Optional
36+ 'email ' => $ customer ['email ' ] ?? null , // Optional
37+ 'firstName ' => $ customer ['firstName ' ] ?? null , // Optional
38+ 'lastName ' => $ customer ['lastName ' ] ?? null , // Optional
39+ ],
40+ ];
41+
42+ // Generate a unique signature using PaytmChecksumhash.
43+ $ generateSignature = PaytmChecksum::generateSignature (json_encode ($ paytmParams ['body ' ], JSON_UNESCAPED_SLASHES ), config ('paytm.merchant.key ' ));
44+
45+ // Add the generated signature to the parameters for posting.
46+ $ paytmParams ['head ' ] = ['signature ' => $ generateSignature ];
47+
48+ // Set the api endpoint for the local, testing or staging environment
49+ if (config ('paytm.environment ' ) == 'testing ' ) {
50+ $ url = config ('paytm.url.testing ' ).'/theia/api/v1/initiateTransaction?mid= ' .config ('paytm.merchant.id ' ).'&orderId= ' .$ newOrderId ;
51+ }
52+
53+ // Set the api endpoint for the production environment
54+ elseif (config ('paytm.environment ' == 'production ' )) {
55+ $ url = config ('paytm.url.production ' ).'/theia/api/v1/initiateTransaction?mid= ' .config ('paytm.merchant.id ' ).'&orderId= ' .$ newOrderId ;
56+ }
57+
58+ // Send an api request to PayTM.
59+ $ response = $ this ->getcURLRequest ($ url , $ paytmParams );
60+
61+ // Evaluate the response and return appropriate result.
62+ if (!empty ($ response ['body ' ]['resultInfo ' ]['resultStatus ' ]) && $ response ['body ' ]['resultInfo ' ]['resultStatus ' ] == 'S ' ) {
63+ $ result = ['success ' => true , 'orderId ' => $ newOrderId , 'txnToken ' => $ response ['body ' ]['txnToken ' ], 'amount ' => $ amount , 'message ' => $ response ['body ' ]['resultInfo ' ]['resultMsg ' ]];
64+ } else {
65+ $ result = ['success ' => false , 'orderId ' => $ newOrderId , 'txnToken ' => '' , 'amount ' => $ amount , 'message ' => $ response ['body ' ]['resultInfo ' ]['resultMsg ' ]];
66+ }
6567
6668 // Return result array.
67- return $ result ;
68- }
69+ return $ result ;
70+ }
6971
7072 /**
7173 * Verify the status of a transaction/order.
72- *
74+ *
7375 * @param string $orderId
76+ *
7477 * @return $response
7578 */
7679 public function getTransactionStatus ($ orderId )
77- {
78- // initialize an array
79- $ paytmParams = array () ;
80+ {
81+ // initialize an array
82+ $ paytmParams = [] ;
8083
81- // set parameters
82- $ paytmParams [" body " ] = array (
83- " mid " => config ('paytm.merchant.id ' ), // Paytm Merchant ID
84- " orderId " => $ orderId ,
85- ) ;
84+ // set parameters
85+ $ paytmParams [' body ' ] = [
86+ ' mid ' => config ('paytm.merchant.id ' ), // Paytm Merchant ID
87+ ' orderId ' => $ orderId ,
88+ ] ;
8689
8790 // Generate a unique signature using PaytmChecksumhash.
88- $ generateSignature = PaytmChecksum::generateSignature (json_encode ($ paytmParams [" body " ], JSON_UNESCAPED_SLASHES ), config ('paytm.merchant.key ' ));
91+ $ generateSignature = PaytmChecksum::generateSignature (json_encode ($ paytmParams [' body ' ], JSON_UNESCAPED_SLASHES ), config ('paytm.merchant.key ' ));
8992
90- // Add the generated signature to the parameters for posting.
91- $ paytmParams [" head " ] = array ( " signature " => $ generateSignature) ;
93+ // Add the generated signature to the parameters for posting.
94+ $ paytmParams [' head ' ] = [ ' signature ' => $ generateSignature] ;
9295
9396 // Set the api endpoint for the testing environment
94- if (config ('paytm.environment ' ) == 'testing ' )
95- $ url = config ('paytm.url.testing ' ) . "/v3/order/status " ;
97+ if (config ('paytm.environment ' ) == 'testing ' ) {
98+ $ url = config ('paytm.url.testing ' ).'/v3/order/status ' ;
99+ }
96100
97101 // Set the api endpoint for the production environment
98- elseif (config ('paytm.environment ' == 'production ' ))
99- $ url = config ('paytm.url.production ' ) . "/v3/order/status " ;
100-
102+ elseif (config ('paytm.environment ' == 'production ' )) {
103+ $ url = config ('paytm.url.production ' ).'/v3/order/status ' ;
104+ }
105+
101106 // Send an api request to PayTM.
102- $ response = $ this ->getcURLRequest ($ url , $ paytmParams );
107+ $ response = $ this ->getcURLRequest ($ url , $ paytmParams );
103108
104109 // Return response.
105- return $ response ;
106- }
110+ return $ response ;
111+ }
107112
108113 /**
109114 * Send a POST request to the PayTM API endpoint.
110- *
115+ *
111116 * @param $url
112117 * @param $postDate
113118 * @param $headers
119+ *
114120 * @return $response
115121 */
116- public function getcURLRequest ($ url , $ postData = array () , $ headers = array ( " Content-Type: application/json " ) )
122+ public function getcURLRequest ($ url , $ postData = [] , $ headers = [ ' Content-Type: application/json ' ] )
117123 {
118124 // Encode the JSON string for the request
119125 $ post_data_string = json_encode ($ postData , JSON_UNESCAPED_SLASHES );
@@ -123,7 +129,7 @@ public function getcURLRequest($url, $postData = array(), $headers = array("Cont
123129 curl_setopt ($ ch , CURLOPT_POST , 1 );
124130 curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ post_data_string );
125131 curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
126- curl_setopt ($ ch , CURLOPT_HTTPHEADER , array ( " Content-Type: application/json " ) );
132+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , [ ' Content-Type: application/json ' ] );
127133 $ response = curl_exec ($ ch );
128134
129135 // Return response.
0 commit comments