Skip to content

Commit 6ca762a

Browse files
committed
uPay Payment Gateway
0 parents  commit 6ca762a

File tree

8 files changed

+299
-0
lines changed

8 files changed

+299
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "codeboxr/upay",
3+
"description": "uPay Payment Gateway Package",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Codeboxr\\Upay\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Codeboxr"
14+
}
15+
],
16+
"minimum-stability": "dev",
17+
"require": {
18+
"php": "^7.2|^7.3|^8.0|^8.1",
19+
"illuminate/support": "~6|~7|~8"
20+
},
21+
"extra": {
22+
"laravel": {
23+
"providers": [
24+
"Codeboxr\\Upay\\UpayServiceProvider"
25+
]
26+
}
27+
}
28+
}

config/upay.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
"sandbox" => env("UPAY_SANDBOX", false),
5+
"merchant_id" => env("UPAY_MERCHANT_ID", ""),
6+
"merchant_key" => env("UPAY_MERCHANT_KEY", ""),
7+
"merchant_code" => env("UPAY_MERCHANT_CODE", ""),
8+
"merchant_name" => env("UPAY_MERCHANT_NAME", ""),
9+
"merchant_mobile" => env("UPAY_MERCHANT_MOBILE", ""),
10+
"merchant_city" => env("UPAY_MERCHANT_CITY", ""),
11+
"merchant_category_code" => env("UPAY_CATEGORY_CODE", "9399"),
12+
"redirect_url" => env("UPAY_CALLBACK_URL", "")
13+
];

src/Exception/UpayException.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Codeboxr\Upay\Exception;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
class UpayException extends Exception
9+
{
10+
public function __construct($message = "", $code = 0, Throwable $previous = null)
11+
{
12+
parent::__construct($message, $code, $previous);
13+
}
14+
15+
public function render()
16+
{
17+
return [
18+
'error' => true,
19+
'code' => $this->code,
20+
'message' => $this->getMessage(),
21+
];
22+
}
23+
24+
}

src/Facade/Payment.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Codeboxr\Upay\Facade;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
/**
8+
* @method static createPayment($amount, $invoiceId, $txnId, $date)
9+
* @method static executePayment($amount, $invoiceId, $txnId, $date)
10+
* @method static queryPayment(string $txnId)
11+
* @method static getMultiStatus(array $txnIds)
12+
*/
13+
class Payment extends Facade
14+
{
15+
/**
16+
* Get the registered name of the component.
17+
*
18+
* @return string
19+
*/
20+
protected static function getFacadeAccessor()
21+
{
22+
return 'payment';
23+
}
24+
}

src/Managers/BaseApi.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Codeboxr\Upay\Managers;
4+
5+
use Codeboxr\Upay\Exception\UpayException;
6+
use Illuminate\Support\Facades\Http;
7+
8+
class BaseApi
9+
{
10+
protected $token = "";
11+
12+
protected $baseUrl = "";
13+
14+
public function __construct()
15+
{
16+
$this->baseUrl = config('upay.sandbox') == true ? "https://uat-pg.upay.systems/" : "";
17+
}
18+
19+
protected function headers()
20+
{
21+
return [
22+
"Authorization" => "UPAY {$this->getToken()}",
23+
"Accept" => "application/json"
24+
];
25+
}
26+
27+
protected function getToken()
28+
{
29+
if (empty($this->token)) {
30+
$response = Http::acceptJson()
31+
->post($this->baseUrl . "payment/merchant-auth/", [
32+
"merchant_id" => config("upay.merchant_id"),
33+
"merchant_key" => config("upay.merchant_key"),
34+
]);
35+
36+
$result = json_decode($response->body());
37+
if ($response->failed()) {
38+
throw new UpayException($result->message, $response->status());
39+
}
40+
$this->token = optional($result->data)->token;
41+
}
42+
43+
return $this->token;
44+
}
45+
}

src/Managers/Payment.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace Codeboxr\Upay\Managers;
4+
5+
use Illuminate\Support\Facades\Http;
6+
use Codeboxr\Upay\Exception\UpayException;
7+
8+
class Payment extends BaseApi
9+
{
10+
/**
11+
* Create payment
12+
*
13+
* @param string $amount
14+
* @param string $invoiceId
15+
* @param string $txnId
16+
* @param string $date
17+
*
18+
* @return \Illuminate\Http\JsonResponse
19+
* @throws UpayException
20+
*/
21+
public function createPayment($amount, $invoiceId, $txnId, $date)
22+
{
23+
$upayResponse = Http::withHeaders($this->headers())
24+
->post($this->baseUrl . "payment/merchant-payment-init/", [
25+
"date" => $date,
26+
"txn_id" => $txnId,
27+
"invoice_id" => $invoiceId,
28+
"amount" => $amount,
29+
"merchant_id" => config('upay.merchant_id'),
30+
"merchant_name" => config('upay.merchant_name'),
31+
"merchant_code" => config('upay.merchant_code'),
32+
"merchant_country_code" => "BD",
33+
"merchant_city" => config("upay.merchant_city"),
34+
"merchant_category_code" => config("upay.merchant_category_code"),
35+
"merchant_mobile" => config("upay.merchant_mobile"),
36+
"transaction_currency_code" => "BD",
37+
"redirect_url" => config("upay.redirect_url"),
38+
]);
39+
40+
$result = json_decode($upayResponse->body());
41+
if ($upayResponse->failed()) {
42+
throw new UpayException($result->message, $upayResponse->status());
43+
}
44+
45+
return response()->json([
46+
"code" => $result->code,
47+
"session_id" => optional($result->data)->session_id,
48+
"txn_id" => optional($result->data)->txn_id,
49+
"trx_id" => optional($result->data)->trx_id,
50+
"invoice_id" => optional($result->data)->invoice_id,
51+
"gateway_url" => optional($result->data)->gateway_url,
52+
]);
53+
54+
}
55+
56+
/**
57+
* Redirect payment page
58+
*
59+
* @param string $amount
60+
* @param string $invoiceId
61+
* @param string $txnId
62+
* @param string $date
63+
*
64+
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|void
65+
* @throws UpayException
66+
*/
67+
public function executePayment($amount, $invoiceId, $txnId, $date)
68+
{
69+
$data = $this->createPayment($amount, $invoiceId, $txnId, $date)->getData();
70+
return redirect($data->gateway_url);
71+
}
72+
73+
/**
74+
* Single Payment Details
75+
*
76+
* @param string $txnId
77+
*
78+
* @return \Illuminate\Http\JsonResponse
79+
* @throws UpayException
80+
*/
81+
public function queryPayment(string $txnId)
82+
{
83+
$upayResponse = Http::withHeaders($this->headers())
84+
->get($this->baseUrl . "payment/single-payment-status/{$txnId}/");
85+
86+
$result = json_decode($upayResponse->body());
87+
if ($upayResponse->failed()) {
88+
throw new UpayException($result->message, $upayResponse->status());
89+
}
90+
91+
return response()->json([
92+
"code" => $result->code,
93+
"session_id" => optional($result->data)->session_id,
94+
"txn_id" => optional($result->data)->txn_id,
95+
"trx_id" => optional($result->data)->trx_id,
96+
"invoice_id" => optional($result->data)->invoice_id,
97+
"status" => optional($result->data)->status,
98+
"amount" => optional($result->data)->amount,
99+
"merchant_name" => optional($result->data)->merchant_name,
100+
"date" => optional($result->data)->date,
101+
]);
102+
}
103+
104+
/**
105+
* Multiple Payment Status
106+
*
107+
* @param array $txnIds
108+
*
109+
* @return \Illuminate\Http\JsonResponse
110+
* @throws UpayException
111+
*/
112+
public function getMultiStatus(array $txnIds)
113+
{
114+
$upayResponse = Http::withHeaders($this->headers())
115+
->post($this->baseUrl . "payment/bulk-payment-status/", [
116+
"txn_id_list" => $txnIds
117+
]);
118+
119+
$result = json_decode($upayResponse->body());
120+
if ($upayResponse->failed()) {
121+
throw new UpayException($result->message, $upayResponse->status());
122+
}
123+
124+
return response()->json($result->data);
125+
}
126+
}

src/UpayServiceProvider.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Codeboxr\Upay;
4+
5+
use Codeboxr\Upay\Managers\Payment;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class UpayServiceProvider extends ServiceProvider
9+
{
10+
11+
/**
12+
* Bootstrap services.
13+
*
14+
* @return void
15+
*/
16+
public function boot()
17+
{
18+
$this->publishes([
19+
__DIR__ . "/../config/upay.php" => config_path("upay.php")
20+
]);
21+
}
22+
23+
/**
24+
* Register application services
25+
*
26+
* @return void
27+
*/
28+
public function register()
29+
{
30+
$this->mergeConfigFrom(__DIR__ . "/../config/upay.php", "upay");
31+
32+
$this->app->bind("payment", function () {
33+
return new Payment();
34+
});
35+
}
36+
37+
}

0 commit comments

Comments
 (0)