PaymentIntent.php
| 1 | <?php |
| 2 | namespace SureCart\Models; |
| 3 | |
| 4 | use SureCart\Models\Traits\HasCustomer; |
| 5 | use SureCart\Models\Traits\HasPlatformFee; |
| 6 | use SureCart\Support\Currency; |
| 7 | |
| 8 | /** |
| 9 | * Payment intent model. |
| 10 | */ |
| 11 | class PaymentIntent extends Model { |
| 12 | use HasCustomer; |
| 13 | use HasPlatformFee; |
| 14 | |
| 15 | /** |
| 16 | * Rest API endpoint |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $endpoint = 'payment_intents'; |
| 21 | |
| 22 | /** |
| 23 | * Object name |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $object_name = 'payment_intent'; |
| 28 | |
| 29 | /** |
| 30 | * Capture the payment intent |
| 31 | * |
| 32 | * @return $this|\WP_Error |
| 33 | */ |
| 34 | protected function capture() { |
| 35 | if ( $this->fireModelEvent( 'capturing' ) === false ) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | if ( empty( $this->attributes['id'] ) ) { |
| 40 | return new \WP_Error( 'not_saved', 'Please create a payment intent.' ); |
| 41 | } |
| 42 | |
| 43 | $captured = \SureCart::request( |
| 44 | $this->endpoint . '/' . $this->attributes['id'] . '/capture/', |
| 45 | [ |
| 46 | 'method' => 'PATCH', |
| 47 | 'query' => $this->query, |
| 48 | 'body' => [ |
| 49 | $this->object_name => $this->getAttributes(), |
| 50 | ], |
| 51 | ] |
| 52 | ); |
| 53 | |
| 54 | if ( is_wp_error( $captured ) ) { |
| 55 | return $captured; |
| 56 | } |
| 57 | |
| 58 | $this->resetAttributes(); |
| 59 | |
| 60 | $this->fill( $captured ); |
| 61 | |
| 62 | $this->fireModelEvent( 'captured' ); |
| 63 | |
| 64 | return $this; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the display amount for total fees |
| 69 | * |
| 70 | * @return array |
| 71 | */ |
| 72 | public function getTotalFeesDisplayAmountAttribute() { |
| 73 | $platform_fee_amount = $this->platform_fee->amount ?? 0; |
| 74 | $service_fee_amount = $this->service_fee->amount ?? 0; |
| 75 | $total_fees = $platform_fee_amount + $service_fee_amount; |
| 76 | |
| 77 | return Currency::format( $total_fees, $this->currency ); |
| 78 | } |
| 79 | } |
| 80 |