123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace App\Http\Controllers\Payment;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use App\Models\CustomerPackage;
- use App\Models\SellerPackage;
- use App\Models\CombinedOrder;
- use App\Http\Controllers\CustomerPackageController;
- use App\Http\Controllers\SellerPackageController;
- use App\Http\Controllers\WalletController;
- use App\Http\Controllers\CheckoutController;
- use Session;
- class BkashController extends Controller
- {
- private $base_url;
- public function __construct()
- {
- if(get_setting('bkash_sandbox', 1)){
- $this->base_url = "https://checkout.sandbox.bka.sh/v1.2.0-beta/";
- }
- else {
- $this->base_url = "https://checkout.pay.bka.sh/v1.2.0-beta/";
- }
- }
- public function pay(){
- $amount = 0;
- if(Session::has('payment_type')){
- if(Session::get('payment_type') == 'cart_payment'){
- $combined_order = CombinedOrder::findOrFail(Session::get('combined_order_id'));
- $amount = round($combined_order->grand_total);
- }
- elseif (Session::get('payment_type') == 'wallet_payment') {
- $amount = round(Session::get('payment_data')['amount']);
- }
- elseif (Session::get('payment_type') == 'customer_package_payment') {
- $customer_package = CustomerPackage::findOrFail(Session::get('payment_data')['customer_package_id']);
- $amount = round($customer_package->amount);
- }
- elseif (Session::get('payment_type') == 'seller_package_payment') {
- $seller_package = SellerPackage::findOrFail(Session::get('payment_data')['seller_package_id']);
- $amount = round($seller_package->amount);
- }
- }
- $request_data = array('app_key'=> env('BKASH_CHECKOUT_APP_KEY'), 'app_secret'=>env('BKASH_CHECKOUT_APP_SECRET'));
- $url = curl_init($this->base_url.'checkout/token/grant');
- $request_data_json=json_encode($request_data);
- $header = array(
- 'Content-Type:application/json',
- 'username:'.env('BKASH_CHECKOUT_USER_NAME'),
- 'password:'.env('BKASH_CHECKOUT_PASSWORD')
- );
- curl_setopt($url,CURLOPT_HTTPHEADER, $header);
- curl_setopt($url,CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($url,CURLOPT_RETURNTRANSFER, true);
- curl_setopt($url,CURLOPT_POSTFIELDS, $request_data_json);
- curl_setopt($url,CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($url, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
- $resultdata = curl_exec($url);
- curl_close($url);
- $token = json_decode($resultdata)->id_token;
- Session::put('bkash_token', $token);
- Session::put('payment_amount', $amount);
- return view('frontend.bkash.index');
- }
- public function checkout(Request $request){
- $auth = Session::get('bkash_token');
- $requestbody = array(
- 'amount' => Session::get('payment_amount'),
- 'currency' => 'BDT',
- 'intent' => 'sale',
- 'merchantInvoiceNumber' => "Inv".Session::get('combined_order_id')
- );
- $url = curl_init($this->base_url.'checkout/payment/create');
- $requestbodyJson = json_encode($requestbody);
- $header = array(
- 'Content-Type:application/json',
- 'Authorization:' . $auth,
- 'X-APP-Key:'.env('BKASH_CHECKOUT_APP_KEY')
- );
- curl_setopt($url, CURLOPT_HTTPHEADER, $header);
- curl_setopt($url, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($url, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($url, CURLOPT_POSTFIELDS, $requestbodyJson);
- curl_setopt($url, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($url, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
- $resultdata = curl_exec($url);
- curl_close($url);
- return $resultdata;
- }
- public function excecute(Request $request){
- $paymentID = $request->paymentID;
- $auth = Session::get('bkash_token');
- $url = curl_init($this->base_url.'checkout/payment/execute/'.$paymentID);
- $header = array(
- 'Content-Type:application/json',
- 'Authorization:' . $auth,
- 'X-APP-Key:'.env('BKASH_CHECKOUT_APP_KEY')
- );
- curl_setopt($url,CURLOPT_HTTPHEADER, $header);
- curl_setopt($url,CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($url,CURLOPT_RETURNTRANSFER, true);
- curl_setopt($url,CURLOPT_FOLLOWLOCATION, 1);
- $resultdata = curl_exec($url);
- curl_close($url);
- return $resultdata;
- }
- public function success(Request $request){
- $payment_type = Session::get('payment_type');
- if ($payment_type == 'cart_payment') {
- return (new CheckoutController)->checkout_done(Session::get('combined_order_id'), $request->payment_details);
- }
- if ($payment_type == 'wallet_payment') {
- return (new WalletController)->wallet_payment_done(Session::get('payment_data'), $request->payment_details);
- }
- if ($payment_type == 'customer_package_payment') {
- return (new CustomerPackageController)->purchase_payment_done(Session::get('payment_data'), $request->payment_details);
- }
- if($payment_type == 'seller_package_payment') {
- return (new SellerPackageController)->purchase_payment_done(Session::get('payment_data'), $request->payment_details);
- }
- }
- }
|