diff --git a/Readme.md b/Readme.md index 5606172..26ac8c7 100644 --- a/Readme.md +++ b/Readme.md @@ -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', ``` @@ -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="" ``` @@ -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 @@ -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']); } @@ -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'); @@ -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.. + } ``` @@ -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 @@ -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 diff --git a/config/bkash.php b/config/bkash.php index 9891edc..5b075c5 100644 --- a/config/bkash.php +++ b/config/bkash.php @@ -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', ]; diff --git a/src/Controllers/BkashTokenizePaymentController.php b/src/Controllers/BkashTokenizePaymentController.php index 1d8be4a..31d4e87 100644 --- a/src/Controllers/BkashTokenizePaymentController.php +++ b/src/Controllers/BkashTokenizePaymentController.php @@ -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" @@ -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") { @@ -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) @@ -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) { @@ -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.. } } diff --git a/src/Facade/BkashPaymentTokenize.php b/src/Facade/BkashPaymentTokenize.php index e4945b3..c53a6b7 100644 --- a/src/Facade/BkashPaymentTokenize.php +++ b/src/Facade/BkashPaymentTokenize.php @@ -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) diff --git a/src/Payment/TBPayment.php b/src/Payment/TBPayment.php index a2b3915..7159770 100644 --- a/src/Payment/TBPayment.php +++ b/src/Payment/TBPayment.php @@ -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) { diff --git a/src/Payment/TBRefund.php b/src/Payment/TBRefund.php index bc3fa27..0605ccd 100644 --- a/src/Payment/TBRefund.php +++ b/src/Payment/TBRefund.php @@ -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, @@ -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); } } diff --git a/src/Traits/Helpers.php b/src/Traits/Helpers.php index e49985f..65a6660 100644 --- a/src/Traits/Helpers.php +++ b/src/Traits/Helpers.php @@ -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', @@ -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( @@ -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'), @@ -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'), @@ -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); } }