1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Facades\Mail; |
6
|
|
|
use App\Facades\User as UserFacade; |
7
|
|
|
use App\Models\Country; |
8
|
|
|
use App\Models\PaymentCheckout; |
9
|
|
|
use App\Models\Purse; |
10
|
|
|
use App\Models\ShopHistory; |
11
|
|
|
use App\Models\ShopInventory; |
12
|
|
|
use App\Models\ShopItem; |
13
|
|
|
use App\Models\Voucher; |
14
|
|
|
use Illuminate\Http\JsonResponse; |
15
|
|
|
use Illuminate\Http\RedirectResponse; |
16
|
|
|
use Illuminate\Http\Request; |
17
|
|
|
use Illuminate\Http\Response; |
18
|
|
|
use Laravel\Lumen\Http\Redirector; |
19
|
|
|
use Laravel\Lumen\Http\ResponseFactory; |
20
|
|
|
use Laravel\Lumen\Routing\Controller as BaseController; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ShopController. |
24
|
|
|
*/ |
25
|
|
|
class ShopController extends BaseController |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* List all Shop Countries. |
29
|
|
|
* |
30
|
|
|
* @return JsonResponse |
31
|
|
|
*/ |
32
|
|
|
public function listCountries(): JsonResponse |
33
|
|
|
{ |
34
|
|
|
return response()->json(Country::all()); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the Shop Inventory of a Country. |
39
|
|
|
* |
40
|
|
|
* @param string $countryCode |
41
|
|
|
* |
42
|
|
|
* @return JsonResponse |
43
|
|
|
*/ |
44
|
|
|
public function getInventory(string $countryCode): JsonResponse |
45
|
|
|
{ |
46
|
|
|
return response()->json(new ShopInventory(Country::where('countryCode', $countryCode)->first()), |
|
|
|
|
47
|
|
|
200, [], JSON_UNESCAPED_SLASHES); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get User Purse. |
52
|
|
|
* |
53
|
|
|
* @param Request $request |
54
|
|
|
* |
55
|
|
|
* @return JsonResponse |
56
|
|
|
*/ |
57
|
|
|
public function getPurse(Request $request): JsonResponse |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
return response()->json(new Purse(UserFacade::getUser()->uniqueId)); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Proceed Payment Checkout. |
64
|
|
|
* |
65
|
|
|
* @param string $paymentCategory |
66
|
|
|
* @param int $countryCode |
67
|
|
|
* @param int $shopItem |
68
|
|
|
* @param int $paymentMethod |
69
|
|
|
* |
70
|
|
|
* @return RedirectResponse|Response|Redirector|ResponseFactory |
71
|
|
|
*/ |
72
|
|
|
public function proceed(string $paymentCategory, int $countryCode, int $shopItem, int $paymentMethod) |
73
|
|
|
{ |
74
|
|
|
$paymentCheckout = PaymentCheckout::where('category', $paymentCategory)->where('country', $countryCode) |
75
|
|
|
->where('item', $shopItem)->where('method', $paymentMethod)->first(); |
76
|
|
|
|
77
|
|
|
if ($paymentCheckout == null) { |
78
|
|
|
return response(view('habbo-web-payments.failed-payment'), 400); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ((strtotime($paymentCheckout->generated_at) + 172800) < time()) { |
82
|
|
|
return response(view('habbo-web-payments.canceled-payment'), 400); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return response(view('habbo-web-payments.proceed', ['payment' => $paymentCheckout])); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Success Payment Checkout. |
90
|
|
|
* |
91
|
|
|
* @TODO: Code Business Logic |
92
|
|
|
* |
93
|
|
|
* @param Request $request |
94
|
|
|
* @param string $paymentCategory |
95
|
|
|
* @param int $countryCode |
96
|
|
|
* @param int $shopItem |
97
|
|
|
* @param int $paymentMethod |
98
|
|
|
* |
99
|
|
|
* @return RedirectResponse|Response|Redirector|ResponseFactory |
100
|
|
|
*/ |
101
|
|
|
public function success(Request $request, string $paymentCategory, int $countryCode, int $shopItem, int $paymentMethod) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$paymentCheckout = PaymentCheckout::where('category', $paymentCategory)->where('country', $countryCode) |
104
|
|
|
->where('item', $shopItem)->where('method', $paymentMethod)->first(); |
105
|
|
|
|
106
|
|
|
if ($paymentCheckout == null) { |
107
|
|
|
return response(view('habbo-web-payments.canceled-payment'), 500); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$purchaseItem = (new ShopHistory())->store($paymentMethod, UserFacade::getUser()->uniqueId, $shopItem); |
111
|
|
|
|
112
|
|
|
Mail::send(['email' => UserFacade::getUser()->email, 'purchaseId' => $purchaseItem->transactionId, |
113
|
|
|
'product' => ShopItem::find($shopItem), 'subject' => 'Purchase completed', |
114
|
|
|
], 'habbo-web-mail.purchase-confirmation'); |
115
|
|
|
|
116
|
|
|
$paymentCheckout->delete(); |
117
|
|
|
|
118
|
|
|
return response(view('habbo-web-payments.success-payment', ['checkoutId' => $purchaseItem->transactionId]), 200); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get User Purchase History. |
123
|
|
|
* |
124
|
|
|
* @TODO: User Purchase History will be coded on the Future |
125
|
|
|
* @TODO: All Purchases of the CMS are Manually, so will be difficult track. |
126
|
|
|
* @TODO: Probably Administrators will Manually Insert History Through HK |
127
|
|
|
* |
128
|
|
|
* @return JsonResponse |
129
|
|
|
*/ |
130
|
|
|
public function getHistory(): JsonResponse |
131
|
|
|
{ |
132
|
|
|
return response()->json(ShopHistory::where('user_id', UserFacade::getUser()->uniqueId)->get()); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Redeem Voucher. |
137
|
|
|
* |
138
|
|
|
* @TODO: Need to Test if really works |
139
|
|
|
* |
140
|
|
|
* @param Request $request |
141
|
|
|
* |
142
|
|
|
* @return JsonResponse |
143
|
|
|
*/ |
144
|
|
|
public function redeem(Request $request): JsonResponse |
145
|
|
|
{ |
146
|
|
|
if (($voucher = Voucher::where('code', $request->json()->get('voucherCode'))->first()) == null) { |
147
|
|
|
return response()->json(null, 404); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
UserFacade::getUser()->increment('credits', $voucher->credits); |
151
|
|
|
UserFacade::getUser()->increment('pixels', $voucher->points); |
152
|
|
|
|
153
|
|
|
$voucher->delete(); |
154
|
|
|
|
155
|
|
|
return response()->json(null, 204); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.