Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
karim-007 committed May 19, 2023
1 parent b135c2c commit 23207ef
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 44 deletions.
37 changes: 34 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ After publish config file setup your credential. you can see this in your config

```
"sandbox" => env("BKASH_SANDBOX", true),
"bkash_app_key" => env("BKASH_APP_KEY", ""),
"bkash_app_secret" => env("BKASH_APP_SECRET", ""),
"bkash_username" => env("BKASH_USERNAME", ""),
"bkash_password" => env("BKASH_PASSWORD", ""),
"callbackURL" => env("BKASH_CALLBACK_URL", "http://127.0.0.1:8000/bkash/callback"),
'timezone' => 'Asia/Dhaka',
```
Expand All @@ -50,10 +52,12 @@ After publish config file setup your credential. you can see this in your config

```
BKASH_SANDBOX=true #for production use false
BKASH_APP_KEY=""
BKASH_APP_SECRET=""
BKASH_USERNAME=""
BKASH_PASSWORD=""
BKASH_CALLBACK_URL=""
```

Expand All @@ -64,7 +68,7 @@ php artisan vendor:publish --provider="Karim007\LaravelBkashTokenize\BkashTokeni
```

### 2. you can override the routes (routes must be in authenticate bkash prefer it)
### 2. you can override the routes
```php
Route::group(['middleware' => ['web']], function () {
// Payment Routes for bKash
Expand Down Expand Up @@ -109,6 +113,12 @@ public function createPayment(Request $request)
$request_data_json = json_encode($request->all());

$response = BkashPaymentTokenize::cPayment($request_data_json);
//$response = BkashPaymentTokenize::cPayment($request_data_json,1); //last parameter is your account number for multi account its like, 1,2,3,4,cont.. default param is 1

//store paymentID and your account number for matching in callback request
// dd($response) //if you are using sandbox and not submit info to bkash use it for 1 response


if (isset($response['bkashURL'])) return redirect()->away($response['bkashURL']);
else return redirect()->back()->with('error-alert2', $response['statusMessage']);
}
Expand Down Expand Up @@ -139,13 +149,26 @@ array[
```php
public function callBack(Request $request)
{
//paymentID=TR00117B1674409647770&status=success&apiVersion=1.2.0-beta
//callback request params
// paymentID=TR00117B1674409647770&status=success&apiVersion=1.2.0-beta
//using paymentID find the account number for sending params

if ($request->status == 'success'){
$response = BkashPaymentTokenize::executePayment($request->paymentID);
//$response = BkashPaymentTokenize::executePayment($request->paymentID, 1); //last parameter is your account number for multi account its like, 1,2,3,4,cont..

if (!$response){
$response = BkashPaymentTokenize::queryPayment($request->paymentID);
//$response = BkashPaymentTokenize::queryPayment($request->paymentID,1); //last parameter is your account number for multi account its like, 1,2,3,4,cont..

}
if (isset($response['statusCode']) && $response['statusCode'] == "0000" && $response['transactionStatus'] == "Completed") {
/*
* for refund need to store
* paymentID and trxID
* */
return BkashPaymentTokenize::success('Thank you for your payment', $response['trxID']);
}
if (isset($response['statusCode']) && $response['statusCode'] == "0000") return $this->success('Thank you for your payment',$response['trxID']);
return BkashPaymentTokenize::failure($response['statusMessage']);
}else if ($request->status == 'cancel'){
return BkashPaymentTokenize::cancel('Your payment is canceled');
Expand Down Expand Up @@ -212,6 +235,8 @@ public function searchTnx($trxID)
"statusMessage":"Successful"
}*/
return BkashPaymentTokenize::searchTransaction($trxID);
//return BkashPaymentTokenize::searchTransaction($trxID,1); //last parameter is your account number for multi account its like, 1,2,3,4,cont..

}
```

Expand All @@ -238,6 +263,8 @@ public function refund(Request $request)
"completedTime":"2023-01-23T15:53:29:120 GMT+0600"
}*/
return BkashRefundTokenize::refund($paymentID,$trxID,$amount,$reason,$sku);
//return BkashRefundTokenize::refund($paymentID,$trxID,$amount,$reason,$sku, 1); //last parameter is your account number for multi account its like, 1,2,3,4,cont..

}
```
### 9. refund status check
Expand All @@ -259,8 +286,12 @@ public function refundStatus(Request $request)
"completedTime":"2023-01-23T15:53:29:120 GMT+0600"
}*/
return BkashRefundTokenize::refundStatus($paymentID,$trxID);
//return BkashRefundTokenize::refundStatus($paymentID,$trxID, 1); //last parameter is your account number for multi account its like, 1,2,3,4,cont..

}
```
N.B. If you are using single account dont need to think about account parameter ignore it just simply use as usal

#### Required APIs
0. **Developer Portal** (detail Product, workflow, API information): https://developer.bka.sh/docs/checkout-process-overview
1. **Grant Token :** https://developer.bka.sh/v1.2.0-beta/reference#gettokenusingpost
Expand Down
17 changes: 17 additions & 0 deletions config/bkash.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@

return [
"sandbox" => env("BKASH_SANDBOX", true),

"bkash_app_key" => env("BKASH_APP_KEY", ""),
"bkash_app_secret" => env("BKASH_APP_SECRET", ""),
"bkash_username" => env("BKASH_USERNAME", ""),
"bkash_password" => env("BKASH_PASSWORD", ""),

"bkash_app_key_2" => env("BKASH_APP_KEY_2", ""),
"bkash_app_secret_2" => env("BKASH_APP_SECRET_2", ""),
"bkash_username_2" => env("BKASH_USERNAME_2", ""),
"bkash_password_2" => env("BKASH_PASSWORD_2", ""),

"bkash_app_key_3" => env("BKASH_APP_KEY_3", ""),
"bkash_app_secret_3" => env("BKASH_APP_SECRET_3", ""),
"bkash_username_3" => env("BKASH_USERNAME_3", ""),
"bkash_password_3" => env("BKASH_PASSWORD_3", ""),

"bkash_app_key_4" => env("BKASH_APP_KEY_4", ""),
"bkash_app_secret_4" => env("BKASH_APP_SECRET_4", ""),
"bkash_username_4" => env("BKASH_USERNAME_4", ""),
"bkash_password_4" => env("BKASH_PASSWORD_4", ""),

"callbackURL" => env("BKASH_CALLBACK_URL", "http://127.0.0.1:8000/bkash/callback"),
'timezone' => 'Asia/Dhaka',
];
14 changes: 13 additions & 1 deletion src/Controllers/BkashTokenizePaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function createPayment(Request $request)
$request_data_json = json_encode($request->all());

$response = BkashPaymentTokenize::cPayment($request_data_json);
//$response = BkashPaymentTokenize::cPayment($request_data_json,1); //last parameter is your account number for multi account its like, 1,2,3,4,cont..
/*array[
"statusCode" => "0000"
"statusMessage" => "Successful"
Expand All @@ -41,18 +42,26 @@ public function createPayment(Request $request)
"transactionStatus" => "Initiated"
"merchantInvoiceNumber" => "63cd99abe6bae"
]*/

//store paymentID and your account number for matching in callback request
// dd($response) //if you are using sandbox and not submit info to bkash use it for 1 response

if (isset($response['bkashURL'])) return redirect()->away($response['bkashURL']);
else return redirect()->back()->with('error-alert2', $response['statusMessage']);
}

public function callBack(Request $request)
{
//paymentID=TR00117B1674409647770&status=success&apiVersion=1.2.0-beta
//callback request params
// paymentID=TR00117B1674409647770&status=success&apiVersion=1.2.0-beta
//using paymentID find the account number for sending params

if ($request->status == 'success'){
$response = BkashPaymentTokenize::executePayment($request->paymentID);
//$response = BkashPaymentTokenize::executePayment($request->paymentID, 1); //last parameter is your account number for multi account its like, 1,2,3,4,cont..
if (!$response){ //if executePayment payment not found call queryPayment
$response = BkashPaymentTokenize::queryPayment($request->paymentID);
//$response = BkashPaymentTokenize::queryPayment($request->paymentID,1); //last parameter is your account number for multi account its like, 1,2,3,4,cont..
}

if (isset($response['statusCode']) && $response['statusCode'] == "0000" && $response['transactionStatus'] == "Completed") {
Expand Down Expand Up @@ -87,6 +96,7 @@ public function searchTnx($trxID)
"statusMessage":"Successful"
}*/
return BkashPaymentTokenize::searchTransaction($trxID);
//return BkashPaymentTokenize::searchTransaction($trxID,1); //last parameter is your account number for multi account its like, 1,2,3,4,cont..
}

public function refund(Request $request)
Expand All @@ -109,6 +119,7 @@ public function refund(Request $request)
"completedTime":"2023-01-23T15:53:29:120 GMT+0600"
}*/
return BkashRefundTokenize::refund($paymentID,$trxID,$amount,$reason,$sku);
//return BkashRefundTokenize::refund($paymentID,$trxID,$amount,$reason,$sku, 1); //last parameter is your account number for multi account its like, 1,2,3,4,cont..
}
public function refundStatus(Request $request)
{
Expand All @@ -126,5 +137,6 @@ public function refundStatus(Request $request)
"completedTime":"2023-01-23T15:53:29:120 GMT+0600"
}*/
return BkashRefundTokenize::refundStatus($paymentID,$trxID);
//return BkashRefundTokenize::refundStatus($paymentID,$trxID, 1); //last parameter is your account number for multi account its like, 1,2,3,4,cont..
}
}
11 changes: 5 additions & 6 deletions src/Facade/BkashPaymentTokenize.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
use Illuminate\Support\Facades\Facade;

/**
* @method static cPayment($request_data_json)
* @method static executePayment($paymentID)
* @method static queryPayment($paymentID)
* @method static bkashSuccess($pay_success)
* @method static refreshToken($refresh_token)
* @method static searchTransaction($trxID)
* @method static cPayment($request_data_json, $account=1)
* @method static executePayment($paymentID, $account=1)
* @method static queryPayment($paymentID, $account=1)
* @method static refreshToken($refresh_token, $account=1)
* @method static searchTransaction($trxID, $account=1)
* @method static success($message,$transId)
* @method static cancel($message,$transId=null)
* @method static failure($message,$transId=null)
Expand Down
43 changes: 28 additions & 15 deletions src/Payment/TBPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,52 @@ class TBPayment extends TBBaseApi
{
use Helpers;

public function cPayment($request_data_json)
public function cPayment($request_data_json, $account=1)
{
$response = $this->getToken();
if ($account == 1) $account=null;
else $account="_$account";
$response = $this->getToken($account);
if ($response['id_token']){
return $this->getUrl('/checkout/create','POST',$request_data_json);
return $this->getUrl('/checkout/create','POST',$request_data_json, $account);
}
return redirect()->back()->with('error-alert2', 'Invalid request try again');

}
public function executePayment($paymentID)
public function executePayment($paymentID, $account=1)
{
if ($account == 1) $account=null;
else $account="_$account";

$token = session()->get('bkash_token');
if (!$token) $this->getToken();
return $this->getUrl2($paymentID,'/checkout/execute');
if (!$token) $this->getToken($account);
return $this->getUrl2($paymentID,'/checkout/execute', $account);
}
public function queryPayment($paymentID)
public function queryPayment($paymentID, $account=1)
{
if ($account == 1) $account=null;
else $account="_$account";

$token = session()->get('bkash_token');
if (!$token) $this->getToken();
return $this->getUrl2($paymentID,'/checkout/payment/status');
if (!$token) $this->getToken($account);
return $this->getUrl2($paymentID,'/checkout/payment/status', $account);
}
public function refreshToken($refresh_token)
public function refreshToken($refresh_token, $account=1)
{
return $this->getUrlToken("/checkout/token/refresh",$refresh_token);
if ($account == 1) $account=null;
else $account="_$account";

return $this->getUrlToken("/checkout/token/refresh",$refresh_token, $account);
}
public function searchTransaction($trxID)
public function searchTransaction($trxID, $account=1)
{
if ($account == 1) $account=null;
else $account="_$account";

$post_token = array(
'trxID' => $trxID
);
$posttoken = json_encode($post_token);
$this->getToken();
return $this->getUrl3("/checkout/general/searchTransaction",$posttoken);
$this->getToken($account);
return $this->getUrl3("/checkout/general/searchTransaction",$posttoken, $account);
}
public function success($message,$transId)
{
Expand Down
18 changes: 12 additions & 6 deletions src/Payment/TBRefund.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ class TBRefund extends TBBaseApi
{
use Helpers;

public function refund($paymentID,$trxID,$amount,$reason='refined amount',$sku='abc')
public function refund($paymentID,$trxID,$amount,$reason='refined amount',$sku='abc', $account=1)
{
if ($account == 1) $account=null;
else $account="_$account";

$post_token = array(
'paymentID' => $paymentID,
'amount' => $amount,
Expand All @@ -17,18 +20,21 @@ public function refund($paymentID,$trxID,$amount,$reason='refined amount',$sku='
'sku' => $sku,
);
$posttoken = json_encode($post_token);
$this->getToken();
return $this->getUrl3("/checkout/payment/refund",$posttoken);
$this->getToken($account);
return $this->getUrl3("/checkout/payment/refund",$posttoken, $account);
}

public function refundStatus($paymentID,$trxID)
public function refundStatus($paymentID,$trxID, $account=1)
{
if ($account == 1) $account=null;
else $account="_$account";

$post_token = array(
'paymentID' => $paymentID,
'trxID' => $trxID,
);
$posttoken = json_encode($post_token);
$this->getToken();
return $this->getUrl3("/checkout/payment/refund",$posttoken);
$this->getToken($account);
return $this->getUrl3("/checkout/payment/refund",$posttoken, $account);
}
}
26 changes: 13 additions & 13 deletions src/Traits/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ public function getIp()
return request()->ip();
}

protected function getUrlToken($url,$refresh_token=null)
protected function getUrlToken($url,$refresh_token=null, $account=null)
{
session()->forget('bkash_token');
session()->forget('bkash_token_type');
session()->forget('bkash_refresh_token');
$post_token = array(
'app_key' => config("bkash.bkash_app_key"),
'app_secret' => config("bkash.bkash_app_secret"),
'app_key' => config("bkash.bkash_app_key$account"),
'app_secret' => config("bkash.bkash_app_secret$account"),
'refresh_token' => $refresh_token,
);
$url = curl_init($this->baseUrl.$url);
$post_token = json_encode($post_token);

$username = config("bkash.bkash_username");
$password = config("bkash.bkash_password");
$username = config("bkash.bkash_username$account");
$password = config("bkash.bkash_password$account");

$header = array(
'Content-Type:application/json',
Expand Down Expand Up @@ -55,10 +55,10 @@ protected function getUrlToken($url,$refresh_token=null)
return $response;
}

protected function getUrl($url, $method, $data=null)
protected function getUrl($url, $method, $data=null, $account=null)
{
$token = session()->get('bkash_token');
$app_key = config("bkash.bkash_app_key");
$app_key = config("bkash.bkash_app_key$account");

$url = curl_init($this->baseUrl.$url);
$header = array(
Expand All @@ -78,13 +78,13 @@ protected function getUrl($url, $method, $data=null)
return json_decode($resultdata, true);
}

protected function getUrl2($paymentID,$url){
protected function getUrl2($paymentID, $url, $account=null){
$post_token = array(
'paymentID' => $paymentID
);
$url = curl_init($this->baseUrl.$url);
$posttoken = json_encode($post_token);
$app_key = config("bkash.bkash_app_key");
$app_key = config("bkash.bkash_app_key$account");
$header = array(
'Content-Type:application/json',
'Authorization:' . session()->get('bkash_token'),
Expand All @@ -102,9 +102,9 @@ protected function getUrl2($paymentID,$url){
return json_decode($resultdata, true);
}

protected function getUrl3($url,$data){
protected function getUrl3($url,$data, $account=null){
$url = curl_init($this->baseUrl.$url);
$app_key = config("bkash.bkash_app_key");
$app_key = config("bkash.bkash_app_key$account");
$header = array(
'Content-Type:application/json',
'Authorization:' . session()->get('bkash_token'),
Expand All @@ -122,8 +122,8 @@ protected function getUrl3($url,$data){
return json_decode($resultdata, true);
}

protected function getToken()
protected function getToken($account=null)
{
return $this->getUrlToken('/checkout/token/grant');
return $this->getUrlToken('/checkout/token/grant',null, $account);
}
}

0 comments on commit 23207ef

Please sign in to comment.