AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

HitPayAccount.php 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Models;
  3. use Carbon\CarbonInterface;
  4. use Illuminate\Database\Eloquent\Attributes\Fillable;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. /**
  8. * A store's own HitPay account, for DuitNow QR, Touch 'n Go and ShopeePay. Shoppers' payments go straight into it.
  9. *
  10. * @property int $id
  11. * @property int $store_id
  12. * @property string $api_key The merchant's HitPay API key (encrypted)
  13. * @property list<string> $methods HitPay's names for the methods the merchant ticked
  14. * @property list<string>|null $refused_methods Ticked methods HitPay refused (switched off in the merchant's HitPay account)
  15. * @property CarbonInterface|null $refused_at When HitPay last refused them
  16. * @property CarbonInterface|null $created_at
  17. * @property CarbonInterface|null $updated_at
  18. * @property-read Store $store
  19. */
  20. #[Fillable(['api_key', 'methods', 'refused_methods', 'refused_at'])]
  21. class HitPayAccount extends Model
  22. {
  23. protected $table = 'hitpay_accounts';
  24. protected $hidden = ['api_key'];
  25. /**
  26. * The HitPay methods we offer, by HitPay's own name. Cards, FPX and GrabPay stay with Stripe.
  27. */
  28. public const array METHODS = ['duitnow', 'touch_n_go', 'shopee_pay'];
  29. /**
  30. * How long a refused method is left out of checkout before we try it again. The merchant may have
  31. * switched it on in HitPay meanwhile; saving the methods in Settings tries again at once.
  32. */
  33. public const int RETRY_REFUSED_AFTER_MINUTES = 60;
  34. /**
  35. * The ticked methods to show shoppers: refused ones are left out for a while, so shoppers are not
  36. * offered something that fails.
  37. *
  38. * @return list<string>
  39. */
  40. public function offeredMethods(): array
  41. {
  42. $ticked = array_values(array_intersect(self::METHODS, $this->methods ?? []));
  43. if ($this->refused_at === null || $this->refused_at->lt(now()->subMinutes(self::RETRY_REFUSED_AFTER_MINUTES))) {
  44. return $ticked;
  45. }
  46. return array_values(array_diff($ticked, $this->refused_methods ?? []));
  47. }
  48. /**
  49. * Remember that HitPay refused these methods.
  50. *
  51. * @param list<string> $methods
  52. */
  53. public function refused(array $methods): void
  54. {
  55. $this->refused_methods = array_values(array_intersect(self::METHODS, $methods));
  56. $this->refused_at = now();
  57. $this->save();
  58. }
  59. /** The merchant changed their methods or key: try every ticked method again. */
  60. public function clearRefused(): void
  61. {
  62. $this->refused_methods = null;
  63. $this->refused_at = null;
  64. }
  65. /**
  66. * @return BelongsTo<Store, $this>
  67. */
  68. public function store(): BelongsTo
  69. {
  70. return $this->belongsTo(Store::class);
  71. }
  72. /**
  73. * @return array<string, string>
  74. */
  75. protected function casts(): array
  76. {
  77. return [
  78. 'api_key' => 'encrypted',
  79. 'methods' => 'array',
  80. 'refused_methods' => 'array',
  81. 'refused_at' => 'datetime',
  82. ];
  83. }
  84. }