-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAddMoneyController.php
188 lines (132 loc) · 4.15 KB
/
AddMoneyController.php
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use Validator;
use URL;
use Session;
use Redirect;
use Illuminate\Support\Facades\Input;
use Srmklive\PayPal\Services\PayPal as PayPalClient;
use App\Cart;
use App\Http\helpers;
use App\ShippingRate;
use App\CartCoupon;
use App\TaxRate;
class AddMoneyController extends HomeController
{
private $_api_context;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Show the application paywith paypalpage.
*
* @return \Illuminate\Http\Response
*/
public function payWithPaypal()
{
return view('paywithpaypal');
}
/**
* Store a details of payment with paypal.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postPaymentWithpaypal(Request $request)
{
$cartsess=Session::get('carts');
$cartdata = Cart::select('*')->where('cart_sessionid' ,'=' , $cartsess)->get();
$pi=1;
$total=0;
$shiptotal=0;
foreach($cartdata as $key => $cartdetail){
$oprodType = Cart::cartdescnfo($cartdetail->id,'oprodType');
$pricee=$cartdetail->price;
$shipping=$cartdetail->shipping;
if($shipping!='' || $shipping!=0){
$shiptotal=$shiptotal+$shipping;
}else{
$shiptotal=$shiptotal;
}
$total=$total+$pricee;
$pi++;
}
$subtotal=$total+$shiptotal;
$coucount = CartCoupon::select('*')->where('cart_sessionid' ,'=' , $cartsess)->count();
if($coucount>0){
$coucode = CartCoupon::select('*')->where('cart_sessionid' ,'=' , $cartsess)->first();
$couamt=$coucode->coupon_amt;
$couptotal=$subtotal-$couamt;
}else{
$couptotal=$subtotal;
}
$taxdata = TaxRate::select('*')->where('id', '=', 1)->first();
if($taxdata->tax_status==1){
$taxrate=$taxdata->tax_rates;
$tax=($total*$taxrate)/100;
$grtotal=$couptotal+$tax;
}else{
$tax=0;
$grtotal=$couptotal;
}
//print_r($product);
//exit();
/* $shipping_address = new ShippingAddress();
$shipping_address->setCity('City');
$shipping_address->setCountryCode('AR');
$shipping_address->setPostalCode('200');
$shipping_address->setLine1('Adress Line1');
$shipping_address->setState('State');
$shipping_address->setRecipientName('Recipient Name');
$shipping_address->setShippingAddress($shipping_address);*/
/*$amount_details = new Details();
$amount_details->setSubtotal($grtotal);*/
$provider = new PayPalClient([]);
$provider->getAccessToken();
$e = $provider->createOrder([
"intent"=> "CAPTURE",
"purchase_units"=> [
0 => [
"amount"=> [
"currency_code"=> "USD",
"value"=> strval(round($grtotal,2))
]
]
],
"application_context" => [
"cancel_url" => route('cartitems'),
"return_url" => route('payment.status')
]
]);
// dd(number_format(floor($grtotal*100)/100,2, '.', ''));
session(['orderID'.auth()->user()->id => $e['id']]);
foreach($e['links'] as $l){
if($l['rel'] == 'approve'){
return redirect($l['href']);
}
}
\Session::put('error','Some error occur, sorry for inconvenient');
return Redirect::route('cartitems');
}
public function getPaymentStatus()
{
$orderID = Session::get('orderID'.auth()->user()->id);
$provider = new PayPalClient([]);
$provider->getAccessToken();
$id = request()->input('PayerID');
$response = $provider->capturePaymentOrder($orderID);
//dd($response);
if($response['status'] == 'COMPLETED')
return Redirect::route('order');
\Session::put('error','Payment failed');
return Redirect::route('cartitems');
}
}