diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..fbf6d37 Binary files /dev/null and b/.DS_Store differ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9dc9d2f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Dipesh Khanal + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..53161f9 --- /dev/null +++ b/composer.json @@ -0,0 +1,29 @@ +{ + "name": "dipesh79/laravel-phonepe", + "description": "Laravel PhonePe - Laravel PhonePe Payment Integration Library", + "type": "library", + "license": "MIT", + "autoload": { + "psr-4": { + "Dipesh79\\LaravelPhonePe\\": "src" + } + }, + "authors": [ + { + "name": "Dipesh79", + "email": "dipeshkhanal79@gmail.com", + "homepage": "https://www.khanaldipesh.com.np", + "role": "Developer" + } + ], + + "minimum-stability": "dev", + "require": {}, + "extra": { + "laravel": { + "providers": [ + "Dipesh79\\LaravelPhonePe\\LaravelPhonePeServiceProvider" + ] + } + } +} diff --git a/src/LaravelPhonePe.php b/src/LaravelPhonePe.php new file mode 100644 index 0000000..3a0ef00 --- /dev/null +++ b/src/LaravelPhonePe.php @@ -0,0 +1,117 @@ +merchantId = config('phonepe.merchantId'); + $this->merchantUserId = config('phonepe.merchantUserId'); + $this->baseUrl = config('phonepe.env') == 'production' ? 'https://api.phonepe.com/apis/hermes' : 'https://api-preprod.phonepe.com/apis/pg-sandbox'; + $this->saltKey = config('phonepe.saltKey'); + $this->saltIndex = config('phonepe.saltIndex'); + $this->redirectUrl = config('phonepe.redirectUrl'); + $this->callBackUrl = config('phonepe.callBackUrl'); + } + + public function makePayment($amount, $merchantTransactionId, $phone) + { + $data = array( + 'merchantId' => $this->merchantId, + 'merchantTransactionId' => $merchantTransactionId, + 'merchantUserId' => $this->merchantUserId, + 'amount' => $amount * 100, + 'redirectUrl' => $this->redirectUrl, + 'redirectMode' => 'POST', + 'callbackUrl' => $this->callBackUrl, + 'mobileNumber' => $phone, + 'paymentInstrument' => + array( + 'type' => 'PAY_PAGE', + ), + ); + + $encode = base64_encode(json_encode($data)); + + $string = $encode . '/pg/v1/pay' . $this->saltKey; + $sha256 = hash('sha256', $string); + $finalXHeader = $sha256 . '###' . $this->saltIndex; + + $curl = curl_init(); + + curl_setopt_array($curl, array( + CURLOPT_URL => $this->baseUrl . '/pg/v1/pay', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => '', + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => false, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => json_encode(['request' => $encode]), + CURLOPT_HTTPHEADER => array( + 'Content-Type: application/json', + 'X-VERIFY: ' . $finalXHeader + ), + )); + + $response = curl_exec($curl); + + curl_close($curl); + + $rData = json_decode($response); + return $rData->data->instrumentResponse->redirectInfo->url; + + + } + + public function getTransactionStatus(array $request) + { + + $finalXHeader = hash('sha256','/pg/v1/status/'.$request['merchantId'].'/'.$request['transactionId'].$this->saltKey).'###'.$this->saltIndex; + + $curl = curl_init(); + + curl_setopt_array($curl, array( + CURLOPT_URL => $this->baseUrl.'/pg/v1/status/'.$request['merchantId'].'/'.$request['transactionId'], + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => '', + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => false, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => 'GET', + CURLOPT_HTTPHEADER => array( + 'Content-Type: application/json', + 'accept: application/json', + 'X-VERIFY: '.$finalXHeader, + 'X-MERCHANT-ID: '.$request['transactionId'] + ), + )); + + $response = curl_exec($curl); + + curl_close($curl); + + if (json_decode($response)->success) { + return true; + } + else + { + return false; + } + } + + +} diff --git a/src/LaravelPhonePeServiceProvider.php b/src/LaravelPhonePeServiceProvider.php new file mode 100644 index 0000000..46fe78d --- /dev/null +++ b/src/LaravelPhonePeServiceProvider.php @@ -0,0 +1,32 @@ +publishes( + [ + __DIR__ . '/config/phonepe.php' => config_path('phonepe.php'), + ], 'config' + ); + } +} diff --git a/src/config/phonepe.php b/src/config/phonepe.php new file mode 100644 index 0000000..bb2c5d4 --- /dev/null +++ b/src/config/phonepe.php @@ -0,0 +1,11 @@ + env('PHONEPE_MERCHANT_ID'), + 'merchantUserId' => env('PHONEPE_MERCHANT_USER_ID'), + 'env' => env('PHONEPE_ENV'), + 'saltKey' => env('PHONEPE_SALT_KEY'), + 'saltIndex' => env('PHONEPE_SALT_INDEX'), + 'redirectUrl' => env('PHONEPE_REDIRECT_URL'), + 'callBackUrl' => env('PHONEPE_CALLBACK_URL'), +);