1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Plugins\Stripe\Controllers; |
4
|
|
|
|
5
|
|
|
use App\ApiKey; |
6
|
|
|
use App\Http\Controllers\Controller; |
7
|
|
|
use App\Model\Common\Setting; |
8
|
|
|
use App\Plugins\Stripe\Model\StripePayment; |
9
|
|
|
use Cartalyst\Stripe\Laravel\Facades\Stripe; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Schema; |
12
|
|
|
use Validator; |
13
|
|
|
|
14
|
|
|
class SettingsController extends Controller |
15
|
|
|
{ |
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->middleware('auth'); |
19
|
|
|
$this->middleware('admin', ['except'=>['postPaymentWithStripe']]); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function Settings() |
23
|
|
|
{ |
24
|
|
|
try { |
25
|
|
|
if (! Schema::hasTable('stripe')) { |
26
|
|
|
Schema::create('stripe', function ($table) { |
27
|
|
|
$table->increments('id'); |
28
|
|
|
$table->string('image_url'); |
29
|
|
|
$table->string('processing_fee'); |
30
|
|
|
$table->string('base_currency'); |
31
|
|
|
$table->string('currencies'); |
32
|
|
|
$table->timestamps(); |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$stripe1 = new StripePayment(); |
37
|
|
|
// //dd($ccavanue); |
38
|
|
|
$stripe = $stripe1->where('id', '1')->first(); |
39
|
|
|
|
40
|
|
|
if (! $stripe) { |
41
|
|
|
\Artisan::call('db:seed', ['--class' => 'database\\seeds\\StripeSupportedCurrencySeeder', '--force' => true]); |
42
|
|
|
} |
43
|
|
|
$allCurrencies = StripePayment::pluck('currencies', 'id')->toArray(); |
44
|
|
|
$apikey = new ApiKey(); |
45
|
|
|
$stripeKeys = $apikey->select('stripe_key', 'stripe_secret')->first(); |
46
|
|
|
$baseCurrency = StripePayment::pluck('base_currency')->toArray(); |
47
|
|
|
$path = app_path().'/Plugins/Stripe/views'; |
48
|
|
|
\View::addNamespace('plugins', $path); |
49
|
|
|
|
50
|
|
|
return view('plugins::settings', compact('stripe', 'baseCurrency', 'allCurrencies', 'stripeKeys')); |
51
|
|
|
} catch (\Exception $ex) { |
52
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function postSettings(Request $request) |
57
|
|
|
{ |
58
|
|
|
$this->validate($request, [ |
59
|
|
|
'business' => 'required', |
60
|
|
|
'cmd' => 'required', |
61
|
|
|
'paypal_url' => 'required|url', |
62
|
|
|
'success_url' => 'url', |
63
|
|
|
'cancel_url' => 'url', |
64
|
|
|
'notify_url' => 'url', |
65
|
|
|
'currencies' => 'required', |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$ccavanue1 = new Paypal(); |
70
|
|
|
$ccavanue = $ccavanue1->where('id', '1')->first(); |
71
|
|
|
$ccavanue->fill($request->input())->save(); |
72
|
|
|
|
73
|
|
|
return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
74
|
|
|
} catch (\Exception $ex) { |
75
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function changeBaseCurrency(Request $request) |
80
|
|
|
{ |
81
|
|
|
$baseCurrency = Stripe::where('id', $request->input('b_currency'))->pluck('currencies')->first(); |
82
|
|
|
$allCurrencies = Stripe::select('base_currency', 'id')->get(); |
83
|
|
|
foreach ($allCurrencies as $currencies) { |
84
|
|
|
Stripe::where('id', $currencies->id)->update(['base_currency'=>$baseCurrency]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return ['message' => 'success', 'update'=>'Base Currency Updated']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function updateApiKey(Request $request) |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
|
|
$stripe = Stripe::make($request->input('stripe_secret')); |
94
|
|
|
$response = $stripe->customers()->create(['description' => 'Test Customer to Validate Secret Key']); |
95
|
|
|
$stripe_secret = $request->input('stripe_secret'); |
96
|
|
|
ApiKey::find(1)->update(['stripe_secret'=>$stripe_secret]); |
97
|
|
|
|
98
|
|
|
return successResponse(['success'=>'true', 'message'=>'Secret key updated successfully']); |
99
|
|
|
} catch (\Cartalyst\Stripe\Exception\UnauthorizedException $e) { |
100
|
|
|
return errorResponse($e->getMessage()); |
101
|
|
|
} catch (\Exception $e) { |
102
|
|
|
return errorResponse($e->getMessage()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* success response method. |
108
|
|
|
* |
109
|
|
|
* @return \Illuminate\Http\Response |
110
|
|
|
*/ |
111
|
|
|
public function postPaymentWithStripe(Request $request) |
112
|
|
|
{ |
113
|
|
|
$validator = Validator::make($request->all(), [ |
114
|
|
|
]); |
115
|
|
|
$input = $request->all(); |
116
|
|
|
$validation = [ |
117
|
|
|
'card_no' => 'required', |
118
|
|
|
'exp_month' => 'required', |
119
|
|
|
'exp_year' => 'required', |
120
|
|
|
'cvv' => 'required', |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
$this->validate($request, $validation); |
124
|
|
|
$stripeSecretKey = ApiKey::pluck('stripe_secret')->first(); |
125
|
|
|
$stripe = Stripe::make($stripeSecretKey); |
126
|
|
|
try { |
127
|
|
|
$invoice = \Session::get('invoice'); |
128
|
|
|
// $invoiceTotal = \Session::get('totalToBePaid'); |
129
|
|
|
$amount = rounding(\Cart::getTotal()); |
130
|
|
|
if (! $amount) {//During renewal |
131
|
|
|
$amount = rounding(\Session::get('totalToBePaid')); |
132
|
|
|
} |
133
|
|
|
$stripeSecretKey = ApiKey::pluck('stripe_secret')->first(); |
134
|
|
|
$stripe = Stripe::make($stripeSecretKey); |
135
|
|
|
$token = $stripe->tokens()->create([ |
136
|
|
|
'card' => [ |
137
|
|
|
'number' => $request->get('card_no'), |
138
|
|
|
'exp_month' => $request->get('exp_month'), |
139
|
|
|
'exp_year' => $request->get('exp_year'), |
140
|
|
|
'cvc' => $request->get('cvv'), |
141
|
|
|
], |
142
|
|
|
]); |
143
|
|
|
if (! isset($token['id'])) { |
144
|
|
|
\Session::put('error', 'The Stripe Token was not generated correctly'); |
145
|
|
|
|
146
|
|
|
return redirect()->route('stripform'); |
147
|
|
|
} |
148
|
|
|
$customer = $stripe->customers()->create([ |
149
|
|
|
'name' => \Auth::user()->first_name.' '.\Auth::user()->last_name, |
150
|
|
|
'email' => \Auth::user()->email, |
151
|
|
|
'address' => [ |
152
|
|
|
'line1' => \Auth::user()->address, |
153
|
|
|
'postal_code' => \Auth::user()->zip, |
154
|
|
|
'city' => \Auth::user()->town, |
155
|
|
|
'state' => \Auth::user()->state, |
156
|
|
|
'country' => \Auth::user()->country, |
157
|
|
|
], |
158
|
|
|
]); |
159
|
|
|
$stripeCustomerId = $customer['id']; |
160
|
|
|
$currency = strtolower(\Auth::user()->currency); |
161
|
|
|
$card = $stripe->cards()->create($stripeCustomerId, $token['id']); |
162
|
|
|
$charge = $stripe->charges()->create([ |
163
|
|
|
'customer' => $customer['id'], |
164
|
|
|
'currency' => $currency, |
165
|
|
|
'amount' =>$amount, |
166
|
|
|
'description' => 'Add in wallet', |
167
|
|
|
]); |
168
|
|
|
if ($charge['status'] == 'succeeded') { |
169
|
|
|
//Change order Status as Success if payment is Successful |
170
|
|
|
$stateCode = \Auth::user()->state; |
171
|
|
|
$cont = new \App\Http\Controllers\RazorpayController(); |
172
|
|
|
$state = $cont->getState($stateCode); |
173
|
|
|
$currency = $cont->getCurrency(); |
174
|
|
|
|
175
|
|
|
$control = new \App\Http\Controllers\Order\RenewController(); |
176
|
|
|
//After Regular Payment |
177
|
|
|
if ($control->checkRenew() === false) { |
178
|
|
|
$checkout_controller = new \App\Http\Controllers\Front\CheckoutController(); |
179
|
|
|
$checkout_controller->checkoutAction($invoice); |
180
|
|
|
|
181
|
|
|
$view = $cont->getViewMessageAfterPayment($invoice, $state, $currency); |
182
|
|
|
$status = $view['status']; |
183
|
|
|
$message = $view['message']; |
184
|
|
|
} else { |
185
|
|
|
//Afer Renew |
186
|
|
|
$control->successRenew($invoice); |
187
|
|
|
$payment = new \App\Http\Controllers\Order\InvoiceController(); |
188
|
|
|
$payment->postRazorpayPayment($invoice); |
189
|
|
|
if ($invoice->grand_total && emailSendingStatus()) { |
190
|
|
|
$this->sendPaymentSuccessMailtoAdmin($invoice->currency, $invoice->grand_total, \Auth::user(), $invoice->invoiceItem()->first()->product_name); |
191
|
|
|
} |
192
|
|
|
$view = $cont->getViewMessageAfterRenew($invoice, $state, $currency); |
193
|
|
|
$status = $view['status']; |
194
|
|
|
$message = $view['message']; |
195
|
|
|
} |
196
|
|
|
\Session::forget('items'); |
197
|
|
|
\Session::forget('code'); |
198
|
|
|
\Session::forget('codevalue'); |
199
|
|
|
\Session::forget('totalToBePaid'); |
200
|
|
|
\Session::forget('invoice'); |
201
|
|
|
\Cart::removeCartCondition('Processing fee'); |
202
|
|
|
|
203
|
|
|
return redirect('checkout')->with($status, $message); |
204
|
|
|
} else { |
205
|
|
|
return redirect('checkout')->with('fails', 'Your Payment was declined. Please try making payment with other gateway'); |
206
|
|
|
} |
207
|
|
|
} catch (\Cartalyst\Stripe\Exception\ApiLimitExceededException | \Cartalyst\Stripe\Exception\BadRequestException | \Cartalyst\Stripe\Exception\MissingParameterException | \Cartalyst\Stripe\Exception\NotFoundException | \Cartalyst\Stripe\Exception\ServerErrorException | \Cartalyst\Stripe\Exception\StripeException | \Cartalyst\Stripe\Exception\UnauthorizedException $e) { |
208
|
|
|
if (emailSendingStatus()) { |
209
|
|
|
$this->sendFailedPaymenttoAdmin($amount, $e->getMessage()); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return redirect('checkout')->with('fails', 'Your Payment was declined. '.$e->getMessage().'. Please try again or try the other gateway'); |
213
|
|
|
} catch (\Cartalyst\Stripe\Exception\CardErrorException $e) { |
214
|
|
|
if (emailSendingStatus()) { |
215
|
|
|
$this->sendFailedPaymenttoAdmin($request['amount'], $e->getMessage()); |
216
|
|
|
} |
217
|
|
|
\Session::put('amount', $amount); |
218
|
|
|
\Session::put('error', $e->getMessage()); |
219
|
|
|
|
220
|
|
|
return redirect()->route('checkout'); |
221
|
|
|
} catch (\Exception $e) { |
222
|
|
|
return redirect('checkout')->with('fails', 'Your payment was declined. '.$e->getMessage().'. Please try again or try the other gateway.'); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public static function sendFailedPaymenttoAdmin($amount, $exceptionMessage) |
227
|
|
|
{ |
228
|
|
|
$setting = Setting::find(1); |
229
|
|
|
$paymentFailData = 'Payment for'.' '.'of'.' '.\Auth::user()->currency.' '.$amount.' '.'failed by'.' '.\Auth::user()->first_name.' '.\Auth::user()->last_name.' '.'. User Email:'.' '.\Auth::user()->email.'<br>'.'Reason:'.$exceptionMessage; |
230
|
|
|
|
231
|
|
|
$mail = new \App\Http\Controllers\Common\PhpMailController(); |
232
|
|
|
$mail->sendEmail($setting->email, $setting->company_email, $paymentFailData, 'Payment failed '); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public static function sendPaymentSuccessMailtoAdmin($currency, $total, $user, $productName) |
236
|
|
|
{ |
237
|
|
|
$setting = Setting::find(1); |
238
|
|
|
$templateController = new \App\Http\Controllers\Common\TemplateController(); |
239
|
|
|
$paymentSuccessdata = 'Payment for'.' '.$productName.' '.'of'.' '.$currency.' '.$total.' '.'successful by'.' '.$user->first_name.' '.$user->last_name.' '.'Email:'.' '.$user->email; |
240
|
|
|
|
241
|
|
|
$mail = new \App\Http\Controllers\Common\PhpMailController(); |
242
|
|
|
$mail->sendEmail($setting->email, $setting->company_email, $paymentSuccessdata, 'Payment Successful '); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|