File size: 3,921 Bytes
89a1081
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php

namespace App\Http\Controllers\PaymentMethod;

use App\Http\Controllers\Controller;
use App\Models\PaymentLog;
use App\Models\PaymentMethod;
use Illuminate\Http\Request;
use Karim007\LaravelBkashTokenize\Facade\BkashPaymentTokenize;
use Config;

class BkashController extends Controller
{
    public $paymentMethod;
    public function __construct(){
        $this->paymentMethod = PaymentMethod::where('unique_code', 'BKASH109')->first();

    }
    public function config($trxCode)
    {
        
        if(!$this->paymentMethod) {

            return false;
        }
        $sandbox = true;
        if ($this->paymentMethod->payment_parameter->environment == 'live') {

            $sandbox = false;
        } 
        $config = [
            'sandbox'          => $sandbox ,
            'bkash_app_key'    => $this->paymentMethod->payment_parameter->api_key,
            'bkash_app_secret' => $this->paymentMethod->payment_parameter->api_secret,
            'bkash_username'   => $this->paymentMethod->payment_parameter->user_name,
            'bkash_password'   => $this->paymentMethod->payment_parameter->password,
            'callbackURL'      => route('user.bkash.callback', $trxCode),
            "timezone"         => "Asia/Dhaka"
        ];

        Config::set('bkash',  $config );

        return true;
    }

    public function confirmPayment() {
        
         try {
            $paymentTrackNumber = session()->get('payment_track');
            $paymentLog = PaymentLog::where('trx_number', $paymentTrackNumber)->first();
            if(!$paymentLog || !$this->config($paymentLog->trx_number)) {

                return redirect()->route('home')->with('error', translate('Invalaid Transaction'));
            }
            $inv = uniqid();
            $request['intent'] = 'sale';
            $request['mode'] = '0011'; 
            $request['payerReference'] = $inv;
            $request['currency'] = 'BDT';
            $request['amount'] = round($paymentLog->final_amount,2);
            $request['merchantInvoiceNumber'] = $inv;
            $request['callbackURL'] = config("bkash.callbackURL");
            
            $request_data_json = json_encode($request);
            $response = BkashPaymentTokenize::cPayment($request_data_json);
            
            if (isset($response['bkashURL'])) {
                
                return redirect()->away($response['bkashURL']);
            }
            $notify[] = ['error', $response['statusMessage']];
            return back()->withNotify($notify);

        } catch (\Exception $ex) {
            return back()->with('error',$ex->getMessage());
        }
    }

    public function callBack(Request $request, $trx_code)
    {
        $paymentLog = PaymentLog::where('trx_number', $trx_code)->first();
        
        if(!$paymentLog || !$this->config($paymentLog->trx_number)){
           abort(404);
        }
        
        if ($request->status == 'success'){
            $response = BkashPaymentTokenize::executePayment($request->paymentID); 
        
            if (isset($response['statusCode']) && $response['statusCode'] == "0000" && $response['transactionStatus'] == "Completed") {

                PaymentController::paymentUpdate($trx_code);
                $notify[] = ['success', 'Payment successful!'];
                return redirect()->route('user.dashboard')->withNotify($notify);
            } else {
                $notify[] = ['error', translate('Something went wrong the payment was not successfull')];
                return redirect()->route('user.dashboard')->withNotify($notify);
            }
        } else {
            PaymentLog::where('trx_number',$trx_code)->where([
                'status' => 0
            ])->delete();
            $notify[] = ['error', translate('Payment was cancelled')];
            return redirect()->route('user.plan.create')->withNotify($notify);
        }
    }
}