| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
class WC_Vipps_Agreement_Initial_Charge extends WC_Vipps_Model { |
| 6 |
public const TRANSACTION_TYPE_RESERVE_CAPTURE = "RESERVE_CAPTURE"; |
| 7 |
public const TRANSACTION_TYPE_DIRECT_CAPTURE = "DIRECT_CAPTURE"; |
| 8 |
|
| 9 |
protected array $valid_transaction_types = [ |
| 10 |
self::TRANSACTION_TYPE_RESERVE_CAPTURE, |
| 11 |
self::TRANSACTION_TYPE_DIRECT_CAPTURE |
| 12 |
]; |
| 13 |
|
| 14 |
protected array $required_fields = [ "amount", "description", "transaction_type" ]; |
| 15 |
|
| 16 |
public ?int $amount = null; |
| 17 |
public ?string $description = null; |
| 18 |
public ?string $transaction_type = null; |
| 19 |
public ?string $order_id = null; |
| 20 |
public ?string $external_id = null; |
| 21 |
|
| 22 |
public function set_amount( int $amount ): self { |
| 23 |
$this->amount = $amount; |
| 24 |
|
| 25 |
return $this; |
| 26 |
} |
| 27 |
|
| 28 |
public function set_description( string $description ): self { |
| 29 |
if ( strlen( $description ) > 100 ) { |
| 30 |
$description = mb_substr( $description, 0, 97 ) . '...'; |
| 31 |
} |
| 32 |
|
| 33 |
$this->description = $description; |
| 34 |
|
| 35 |
return $this; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @throws WC_Vipps_Recurring_Invalid_Value_Exception |
| 40 |
*/ |
| 41 |
public function set_transaction_type( string $transaction_type ): self { |
| 42 |
if ( ! in_array( $transaction_type, $this->valid_transaction_types, true ) ) { |
| 43 |
$class = get_class( $this ); |
| 44 |
throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$transaction_type is not a valid value for `transaction_type` in $class." ); |
| 45 |
} |
| 46 |
|
| 47 |
$this->transaction_type = $transaction_type; |
| 48 |
|
| 49 |
return $this; |
| 50 |
} |
| 51 |
|
| 52 |
public function set_order_id( string $order_id ): self { |
| 53 |
$this->order_id = $order_id; |
| 54 |
|
| 55 |
return $this; |
| 56 |
} |
| 57 |
|
| 58 |
public function set_external_id( string $external_id ): self { |
| 59 |
$this->external_id = $external_id; |
| 60 |
|
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @throws WC_Vipps_Recurring_Missing_Value_Exception |
| 66 |
*/ |
| 67 |
public function to_array( bool $check_required = false ): array { |
| 68 |
if ( $check_required ) { |
| 69 |
$this->check_required(); |
| 70 |
} |
| 71 |
|
| 72 |
return array_merge( |
| 73 |
[ |
| 74 |
"amount" => $this->amount, |
| 75 |
"description" => $this->description, |
| 76 |
"transactionType" => $this->transaction_type, |
| 77 |
], |
| 78 |
$this->conditional( "orderId", $this->order_id ), |
| 79 |
$this->conditional( "externalId", $this->external_id ) |
| 80 |
); |
| 81 |
} |
| 82 |
} |
| 83 |
|