Payment.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. /**
  7. * @property int $id
  8. * @property int $schedule_id
  9. * @property int $client_id
  10. * @property int|null $provider_id
  11. * @property int|null $client_payment_method_id
  12. * @property string $gateway_provider
  13. * @property string|null $gateway_entity_reference
  14. * @property string|null $gateway_entity_label
  15. * @property string|null $gateway_operation_reference
  16. * @property string|null $gateway_operation_label
  17. * @property string $payment_method
  18. * @property \App\Enums\PaymentStatusEnum $status
  19. * @property numeric $gross_amount Valor bruto cobrado do cliente.
  20. * @property numeric $gateway_fee_amount Taxa cobrada pelo gateway nesta cobranca.
  21. * @property numeric $platform_fee_amount Taxa da plataforma (regra interna), sem impacto na formula de net_amount.
  22. * @property numeric $net_amount Valor liquido apos taxa do gateway: net_amount = gross_amount - gateway_fee_amount.
  23. * @property string $currency
  24. * @property int $installments
  25. * @property \Illuminate\Support\Carbon|null $authorized_at
  26. * @property \Illuminate\Support\Carbon|null $paid_at
  27. * @property \Illuminate\Support\Carbon|null $failed_at
  28. * @property \Illuminate\Support\Carbon|null $cancelled_at
  29. * @property \Illuminate\Support\Carbon|null $expires_at
  30. * @property string|null $failure_code
  31. * @property string|null $failure_message
  32. * @property array<array-key, mixed>|null $gateway_payload
  33. * @property array<array-key, mixed>|null $metadata
  34. * @property \Illuminate\Support\Carbon|null $created_at
  35. * @property \Illuminate\Support\Carbon|null $updated_at
  36. * @property \Illuminate\Support\Carbon|null $deleted_at
  37. * @property string|null $gateway_code
  38. * @property-read \App\Models\Client $client
  39. * @property-read \App\Models\ClientPaymentMethod|null $clientPaymentMethod
  40. * @property-read \App\Models\Provider|null $provider
  41. * @property-read \App\Models\Schedule $schedule
  42. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\PaymentSplit> $splits
  43. * @property-read int|null $splits_count
  44. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Webhook> $webhooks
  45. * @property-read int|null $webhooks_count
  46. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment newModelQuery()
  47. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment newQuery()
  48. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment onlyTrashed()
  49. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment query()
  50. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereAuthorizedAt($value)
  51. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereCancelledAt($value)
  52. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereClientId($value)
  53. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereClientPaymentMethodId($value)
  54. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereCreatedAt($value)
  55. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereCurrency($value)
  56. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereDeletedAt($value)
  57. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereExpiresAt($value)
  58. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereFailedAt($value)
  59. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereFailureCode($value)
  60. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereFailureMessage($value)
  61. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereGatewayCode($value)
  62. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereGatewayEntityLabel($value)
  63. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereGatewayEntityReference($value)
  64. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereGatewayFeeAmount($value)
  65. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereGatewayOperationLabel($value)
  66. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereGatewayOperationReference($value)
  67. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereGatewayPayload($value)
  68. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereGatewayProvider($value)
  69. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereGrossAmount($value)
  70. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereId($value)
  71. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereInstallments($value)
  72. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereMetadata($value)
  73. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereNetAmount($value)
  74. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment wherePaidAt($value)
  75. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment wherePaymentMethod($value)
  76. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment wherePlatformFeeAmount($value)
  77. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereProviderId($value)
  78. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereScheduleId($value)
  79. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereStatus($value)
  80. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment whereUpdatedAt($value)
  81. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment withTrashed(bool $withTrashed = true)
  82. * @method static \Illuminate\Database\Eloquent\Builder<static>|Payment withoutTrashed()
  83. * @mixin \Eloquent
  84. */
  85. class Payment extends Model
  86. {
  87. use HasFactory, SoftDeletes;
  88. protected $table = 'payments';
  89. protected $fillable = [
  90. 'schedule_id',
  91. 'client_id',
  92. 'provider_id',
  93. 'client_payment_method_id',
  94. 'gateway_provider',
  95. 'gateway_code',
  96. 'gateway_entity_reference',
  97. 'gateway_entity_label',
  98. 'gateway_operation_reference',
  99. 'gateway_operation_label',
  100. 'payment_method',
  101. 'status',
  102. 'gross_amount',
  103. 'gateway_fee_amount',
  104. 'platform_fee_amount',
  105. 'net_amount',
  106. 'currency',
  107. 'installments',
  108. 'authorized_at',
  109. 'paid_at',
  110. 'failed_at',
  111. 'cancelled_at',
  112. 'expires_at',
  113. 'failure_code',
  114. 'failure_message',
  115. 'gateway_payload',
  116. 'metadata',
  117. ];
  118. protected $casts = [
  119. 'gross_amount' => 'decimal:2',
  120. 'gateway_fee_amount' => 'decimal:2',
  121. 'platform_fee_amount' => 'decimal:2',
  122. 'net_amount' => 'decimal:2',
  123. 'installments' => 'integer',
  124. 'status' => \App\Enums\PaymentStatusEnum::class,
  125. 'authorized_at' => 'datetime',
  126. 'paid_at' => 'datetime',
  127. 'failed_at' => 'datetime',
  128. 'cancelled_at' => 'datetime',
  129. 'expires_at' => 'datetime',
  130. 'gateway_payload' => 'array',
  131. 'metadata' => 'array',
  132. 'created_at' => 'datetime',
  133. 'updated_at' => 'datetime',
  134. 'deleted_at' => 'datetime',
  135. ];
  136. public function schedule()
  137. {
  138. return $this->belongsTo(Schedule::class);
  139. }
  140. public function client()
  141. {
  142. return $this->belongsTo(Client::class);
  143. }
  144. public function provider()
  145. {
  146. return $this->belongsTo(Provider::class);
  147. }
  148. public function clientPaymentMethod()
  149. {
  150. return $this->belongsTo(ClientPaymentMethod::class);
  151. }
  152. public function splits()
  153. {
  154. return $this->hasMany(PaymentSplit::class);
  155. }
  156. public function webhooks()
  157. {
  158. return $this->hasMany(Webhook::class);
  159. }
  160. //
  161. public function ensureGatewayCode(): string
  162. {
  163. if (! empty($this->gateway_code)) {
  164. return $this->gateway_code;
  165. }
  166. $code = 'payment-'.(string) \Illuminate\Support\Str::uuid();
  167. $this->forceFill(['gateway_code' => $code])->save();
  168. return $code;
  169. }
  170. }