Skip to content

Commit

Permalink
Initial Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipesh79 committed Oct 23, 2023
0 parents commit b91172a
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
117 changes: 117 additions & 0 deletions src/LaravelPhonePe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Dipesh79\LaravelPhonePe;

use Illuminate\Http\Request;

class LaravelPhonePe
{
private $merchantId;
private $merchantUserId;
private $baseUrl;
private $saltKey;
private $saltIndex;

private $redirectUrl, $callBackUrl;

public function __construct()
{
$this->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;
}
}


}
32 changes: 32 additions & 0 deletions src/LaravelPhonePeServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Dipesh79\LaravelPhonePe;

use Illuminate\Support\ServiceProvider;

class LaravelPhonePeServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$this->publishes(
[
__DIR__ . '/config/phonepe.php' => config_path('phonepe.php'),
], 'config'
);
}
}
11 changes: 11 additions & 0 deletions src/config/phonepe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return array(
'merchantId' => 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'),
);

0 comments on commit b91172a

Please sign in to comment.