| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
-
- namespace App\Models;
-
- use Carbon\CarbonInterface;
- use Illuminate\Database\Eloquent\Attributes\Fillable;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
-
- /**
- * A store's own HitPay account, for DuitNow QR, Touch 'n Go and ShopeePay. Shoppers' payments go straight into it.
- *
- * @property int $id
- * @property int $store_id
- * @property string $api_key The merchant's HitPay API key (encrypted)
- * @property list<string> $methods HitPay's names for the methods the merchant ticked
- * @property list<string>|null $refused_methods Ticked methods HitPay refused (switched off in the merchant's HitPay account)
- * @property CarbonInterface|null $refused_at When HitPay last refused them
- * @property CarbonInterface|null $created_at
- * @property CarbonInterface|null $updated_at
- * @property-read Store $store
- */
- #[Fillable(['api_key', 'methods', 'refused_methods', 'refused_at'])]
- class HitPayAccount extends Model
- {
- protected $table = 'hitpay_accounts';
-
- protected $hidden = ['api_key'];
-
- /**
- * The HitPay methods we offer, by HitPay's own name. Cards, FPX and GrabPay stay with Stripe.
- */
- public const array METHODS = ['duitnow', 'touch_n_go', 'shopee_pay'];
-
- /**
- * How long a refused method is left out of checkout before we try it again. The merchant may have
- * switched it on in HitPay meanwhile; saving the methods in Settings tries again at once.
- */
- public const int RETRY_REFUSED_AFTER_MINUTES = 60;
-
- /**
- * The ticked methods to show shoppers: refused ones are left out for a while, so shoppers are not
- * offered something that fails.
- *
- * @return list<string>
- */
- public function offeredMethods(): array
- {
- $ticked = array_values(array_intersect(self::METHODS, $this->methods ?? []));
-
- if ($this->refused_at === null || $this->refused_at->lt(now()->subMinutes(self::RETRY_REFUSED_AFTER_MINUTES))) {
- return $ticked;
- }
-
- return array_values(array_diff($ticked, $this->refused_methods ?? []));
- }
-
- /**
- * Remember that HitPay refused these methods.
- *
- * @param list<string> $methods
- */
- public function refused(array $methods): void
- {
- $this->refused_methods = array_values(array_intersect(self::METHODS, $methods));
- $this->refused_at = now();
- $this->save();
- }
-
- /** The merchant changed their methods or key: try every ticked method again. */
- public function clearRefused(): void
- {
- $this->refused_methods = null;
- $this->refused_at = null;
- }
-
- /**
- * @return BelongsTo<Store, $this>
- */
- public function store(): BelongsTo
- {
- return $this->belongsTo(Store::class);
- }
-
- /**
- * @return array<string, string>
- */
- protected function casts(): array
- {
- return [
- 'api_key' => 'encrypted',
- 'methods' => 'array',
- 'refused_methods' => 'array',
- 'refused_at' => 'datetime',
- ];
- }
- }
|