PaymentDirectRecharge.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\SoftDeletes;
  4. class PaymentDirectRecharge extends BaseModel
  5. {
  6. use SoftDeletes;
  7. protected $table = 'payment_direct_recharges';
  8. protected $guarded = [];
  9. protected $hidden = ['deleted_at'];
  10. protected $casts = [
  11. 'amount' => 'decimal:2',
  12. 'recharge_channel_group_ids' => 'array',
  13. 'status' => 'integer',
  14. ];
  15. protected $appends = ['fee_rate', 'fee_fixed', 'actual_amount'];
  16. public function rechargeChannel()
  17. {
  18. return $this->belongsTo(RechargeChannel::class, 'recharge_channel_id');
  19. }
  20. public function getFeeRateAttribute($value = null): string
  21. {
  22. return $this->rechargeChannel
  23. ? bcmul((string)$this->rechargeChannel->rate, '100', 4)
  24. : '0.0000';
  25. }
  26. public function getFeeFixedAttribute($value = null): string
  27. {
  28. return bcadd((string)($this->rechargeChannel->fee_fixed ?? 0), '0', 4);
  29. }
  30. /** 扣除三方手续费(金额 × 费率 + 每笔固定费,平台承担)后平台实际收到的金额。 */
  31. public function getActualAmountAttribute(): string
  32. {
  33. $rate = $this->rechargeChannel ? (string)$this->rechargeChannel->rate : '0';
  34. $fee = bcadd(bcmul((string)$this->amount, $rate, 6), $this->fee_fixed, 6);
  35. return bcsub((string)$this->amount, $fee, 2);
  36. }
  37. }