AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HitPayController.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Commerce\Payments\HitPay\HitPayApi;
  4. use App\Commerce\Payments\PaymentException;
  5. use App\Models\HitPayAccount;
  6. use App\Models\Store;
  7. use Illuminate\Http\RedirectResponse;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Validation\Rule;
  10. use Illuminate\Validation\ValidationException;
  11. use Inertia\Inertia;
  12. /**
  13. * Connecting a store to the merchant's own HitPay account, for DuitNow QR, Touch 'n Go and ShopeePay.
  14. * The merchant pastes their API key from HitPay. We check it with HitPay before saving it.
  15. */
  16. class HitPayController extends Controller
  17. {
  18. public function connect(Request $request, string $slug, HitPayApi $api): RedirectResponse
  19. {
  20. $store = $this->ownedStore($request, $slug);
  21. $validated = $request->validate([
  22. 'api_key' => ['required', 'string', 'min:10', 'max:200', 'regex:/^\S+$/'],
  23. ], [
  24. 'api_key.required' => __('Paste your HitPay API key.'),
  25. 'api_key.*' => __('That does not look like a HitPay API key. Copy it again from HitPay.'),
  26. ]);
  27. $key = trim($validated['api_key']);
  28. try {
  29. $accepted = $api->acceptsKey($key);
  30. } catch (PaymentException $e) {
  31. report($e);
  32. throw ValidationException::withMessages(['api_key' => __('HitPay could not be reached. Please try again in a moment.')]);
  33. }
  34. if (! $accepted) {
  35. throw ValidationException::withMessages(['api_key' => config('services.hitpay.sandbox')
  36. ? __('HitPay did not accept this key. This app uses HitPay sandbox (test) accounts, so use a key from dashboard.sandbox.hit-pay.com.')
  37. : __('HitPay did not accept this key. Copy it again from HitPay, under Settings > API Keys.')]);
  38. }
  39. $store->hitpayAccount()->updateOrCreate([], [
  40. 'api_key' => $key,
  41. 'methods' => $store->hitpayAccount->methods ?? HitPayAccount::METHODS,
  42. // A new key may be a different HitPay account: try every method again.
  43. 'refused_methods' => null,
  44. 'refused_at' => null,
  45. ]);
  46. Inertia::flash('toast', ['type' => 'success', 'message' => __('HitPay is connected. Shoppers can now pay by DuitNow QR and e-wallets.')]);
  47. return to_route('stores.settings', $store->slug);
  48. }
  49. /**
  50. * Which HitPay methods shoppers see. Only methods switched on in the merchant's HitPay account should be ticked.
  51. */
  52. public function update(Request $request, string $slug): RedirectResponse
  53. {
  54. $store = $this->ownedStore($request, $slug);
  55. $account = $store->hitpayAccount ?? abort(404);
  56. $validated = $request->validate([
  57. 'methods' => ['required', 'array', 'min:1'],
  58. 'methods.*' => ['string', Rule::in(HitPayAccount::METHODS)],
  59. ], [
  60. 'methods.required' => __('Tick at least one way to pay, or disconnect HitPay.'),
  61. 'methods.min' => __('Tick at least one way to pay, or disconnect HitPay.'),
  62. ]);
  63. // Kept in our own order, without repeats.
  64. $account->methods = array_values(array_intersect(HitPayAccount::METHODS, $validated['methods']));
  65. // The merchant may have switched refused methods on in HitPay meanwhile: offer them again.
  66. $account->clearRefused();
  67. $account->save();
  68. Inertia::flash('toast', ['type' => 'success', 'message' => __('Saved.')]);
  69. return to_route('stores.settings', $store->slug);
  70. }
  71. public function disconnect(Request $request, string $slug): RedirectResponse
  72. {
  73. $store = $this->ownedStore($request, $slug);
  74. $store->hitpayAccount()->delete();
  75. Inertia::flash('toast', ['type' => 'success', 'message' => __('HitPay is disconnected. Shoppers can no longer pay by DuitNow QR or e-wallets.')]);
  76. return to_route('stores.settings', $store->slug);
  77. }
  78. private function ownedStore(Request $request, string $slug): Store
  79. {
  80. return $request->user()->stores()->where('slug', $slug)->firstOrFail();
  81. }
  82. }