| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Commerce\Payments\HitPay\HitPayApi;
- use App\Commerce\Payments\PaymentException;
- use App\Models\HitPayAccount;
- use App\Models\Store;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- use Illuminate\Validation\ValidationException;
- use Inertia\Inertia;
-
- /**
- * Connecting a store to the merchant's own HitPay account, for DuitNow QR, Touch 'n Go and ShopeePay.
- * The merchant pastes their API key from HitPay. We check it with HitPay before saving it.
- */
- class HitPayController extends Controller
- {
- public function connect(Request $request, string $slug, HitPayApi $api): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- $validated = $request->validate([
- 'api_key' => ['required', 'string', 'min:10', 'max:200', 'regex:/^\S+$/'],
- ], [
- 'api_key.required' => __('Paste your HitPay API key.'),
- 'api_key.*' => __('That does not look like a HitPay API key. Copy it again from HitPay.'),
- ]);
-
- $key = trim($validated['api_key']);
-
- try {
- $accepted = $api->acceptsKey($key);
- } catch (PaymentException $e) {
- report($e);
-
- throw ValidationException::withMessages(['api_key' => __('HitPay could not be reached. Please try again in a moment.')]);
- }
-
- if (! $accepted) {
- throw ValidationException::withMessages(['api_key' => config('services.hitpay.sandbox')
- ? __('HitPay did not accept this key. This app uses HitPay sandbox (test) accounts, so use a key from dashboard.sandbox.hit-pay.com.')
- : __('HitPay did not accept this key. Copy it again from HitPay, under Settings > API Keys.')]);
- }
-
- $store->hitpayAccount()->updateOrCreate([], [
- 'api_key' => $key,
- 'methods' => $store->hitpayAccount->methods ?? HitPayAccount::METHODS,
- // A new key may be a different HitPay account: try every method again.
- 'refused_methods' => null,
- 'refused_at' => null,
- ]);
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('HitPay is connected. Shoppers can now pay by DuitNow QR and e-wallets.')]);
-
- return to_route('stores.settings', $store->slug);
- }
-
- /**
- * Which HitPay methods shoppers see. Only methods switched on in the merchant's HitPay account should be ticked.
- */
- public function update(Request $request, string $slug): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
- $account = $store->hitpayAccount ?? abort(404);
-
- $validated = $request->validate([
- 'methods' => ['required', 'array', 'min:1'],
- 'methods.*' => ['string', Rule::in(HitPayAccount::METHODS)],
- ], [
- 'methods.required' => __('Tick at least one way to pay, or disconnect HitPay.'),
- 'methods.min' => __('Tick at least one way to pay, or disconnect HitPay.'),
- ]);
-
- // Kept in our own order, without repeats.
- $account->methods = array_values(array_intersect(HitPayAccount::METHODS, $validated['methods']));
- // The merchant may have switched refused methods on in HitPay meanwhile: offer them again.
- $account->clearRefused();
- $account->save();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Saved.')]);
-
- return to_route('stores.settings', $store->slug);
- }
-
- public function disconnect(Request $request, string $slug): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
- $store->hitpayAccount()->delete();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('HitPay is disconnected. Shoppers can no longer pay by DuitNow QR or e-wallets.')]);
-
- return to_route('stores.settings', $store->slug);
- }
-
- private function ownedStore(Request $request, string $slug): Store
- {
- return $request->user()->stores()->where('slug', $slug)->firstOrFail();
- }
- }
|