Charge.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | use SureCart\Models\Traits\HasCustomer; |
| 6 | use SureCart\Models\Traits\HasDates; |
| 7 | use SureCart\Models\Traits\HasOrder; |
| 8 | use SureCart\Models\Traits\HasPaymentMethod; |
| 9 | use SureCart\Models\Traits\HasSubscription; |
| 10 | use SureCart\Models\Traits\HasPaymentIntent; |
| 11 | use SureCart\Support\Currency; |
| 12 | |
| 13 | /** |
| 14 | * Subscription model |
| 15 | */ |
| 16 | class Charge extends Model { |
| 17 | use HasCustomer; |
| 18 | use HasOrder; |
| 19 | use HasSubscription; |
| 20 | use HasDates; |
| 21 | use HasPaymentMethod; |
| 22 | use HasPaymentIntent; |
| 23 | |
| 24 | /** |
| 25 | * Rest API endpoint |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $endpoint = 'charges'; |
| 30 | |
| 31 | /** |
| 32 | * Object name |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $object_name = 'charge'; |
| 37 | |
| 38 | /** |
| 39 | * Refund this specific charge |
| 40 | * |
| 41 | * @return \SureCart\Models\Refund |
| 42 | */ |
| 43 | protected function refund() { |
| 44 | return new Refund( [ 'charge' => $this->id ] ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get the display amount attribute. |
| 49 | * |
| 50 | * @return string |
| 51 | */ |
| 52 | protected function getDisplayAmountAttribute() { |
| 53 | return Currency::format( $this->amount, $this->currency ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the human discount attribute. |
| 58 | * |
| 59 | * @return string |
| 60 | */ |
| 61 | public function getExternalChargeLinkAttribute() { |
| 62 | if ( ! $this->payment_method || ! $this->payment_method->processor_type ) { |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | $payment_type = $this->payment_method->processor_type; |
| 67 | |
| 68 | if ( ! in_array( $payment_type, [ 'stripe', 'paypal' ], true ) ) { |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | if ( ! $this->external_charge_id ) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | $external_charge_id = $this->external_charge_id; |
| 77 | $is_live_mode = $this->live_mode; |
| 78 | |
| 79 | if ( 'stripe' === $payment_type ) { |
| 80 | return 'https://dashboard.stripe.com/' . ( ! $is_live_mode ? 'test/' : '' ) . 'charges/' . $external_charge_id; |
| 81 | } |
| 82 | if ( 'paypal' === $payment_type ) { |
| 83 | return 'https://www.' . ( ! $is_live_mode ? 'sandbox.' : '' ) . 'paypal.com/activity/payment/' . $external_charge_id; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get the display amount attribute. |
| 89 | * |
| 90 | * @return string |
| 91 | */ |
| 92 | public function getAmountDisplayAmountAttribute() { |
| 93 | return Currency::format( $this->amount, $this->currency ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the refunded display amount attribute. |
| 98 | * |
| 99 | * @return string |
| 100 | */ |
| 101 | public function getRefundedDisplayAmountAttribute() { |
| 102 | return Currency::format( $this->refunded_amount, $this->currency ); |
| 103 | } |
| 104 | } |
| 105 |