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(); } }