| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
class WC_Vipps_Checkout_Session extends WC_Vipps_Model { |
| 6 |
public const TYPE_SUBSCRIPTION = "SUBSCRIPTION"; |
| 7 |
public const TYPE_PAYMENT = "PAYMENT"; |
| 8 |
|
| 9 |
protected array $valid_types = [ |
| 10 |
self::TYPE_SUBSCRIPTION, |
| 11 |
self::TYPE_PAYMENT, |
| 12 |
]; |
| 13 |
|
| 14 |
protected array $required_fields = [ |
| 15 |
"SUBSCRIPTION" => [ "type", "subscription" ], |
| 16 |
"PAYMENT" => [ "type", "transaction" ], |
| 17 |
]; |
| 18 |
|
| 19 |
public ?string $type = null; |
| 20 |
public ?WC_Vipps_Checkout_Session_Customer $prefill_customer = null; |
| 21 |
public ?WC_Vipps_Checkout_Session_Merchant_Info $merchant_info = null; |
| 22 |
public ?WC_Vipps_Checkout_Session_Configuration $configuration = null; |
| 23 |
public ?WC_Vipps_Checkout_Session_Subscription $subscription = null; |
| 24 |
public ?WC_Vipps_Checkout_Session_Transaction $transaction = null; |
| 25 |
|
| 26 |
/** |
| 27 |
* @throws WC_Vipps_Recurring_Invalid_Value_Exception |
| 28 |
*/ |
| 29 |
public function set_type( string $type ): self { |
| 30 |
if ( ! in_array( $type, $this->valid_types, true ) ) { |
| 31 |
$class = get_class( $this ); |
| 32 |
throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$type is not a valid value for `type` in $class." ); |
| 33 |
} |
| 34 |
|
| 35 |
$this->type = $type; |
| 36 |
|
| 37 |
return $this; |
| 38 |
} |
| 39 |
|
| 40 |
public function set_prefill_customer( WC_Vipps_Checkout_Session_Customer $prefill_customer ): self { |
| 41 |
$this->prefill_customer = $prefill_customer; |
| 42 |
|
| 43 |
return $this; |
| 44 |
} |
| 45 |
|
| 46 |
public function set_merchant_info( WC_Vipps_Checkout_Session_Merchant_Info $merchant_info ): self { |
| 47 |
$this->merchant_info = $merchant_info; |
| 48 |
|
| 49 |
return $this; |
| 50 |
} |
| 51 |
|
| 52 |
public function set_configuration( WC_Vipps_Checkout_Session_Configuration $configuration ): self { |
| 53 |
$this->configuration = $configuration; |
| 54 |
|
| 55 |
return $this; |
| 56 |
} |
| 57 |
|
| 58 |
public function set_subscription( WC_Vipps_Checkout_Session_Subscription $subscription ): self { |
| 59 |
$this->subscription = $subscription; |
| 60 |
|
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
public function set_transaction( $transaction ): self { |
| 65 |
return $this->_set_value( "transaction", $transaction, WC_Vipps_Checkout_Session_Transaction::class ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @throws WC_Vipps_Recurring_Missing_Value_Exception |
| 70 |
*/ |
| 71 |
public function to_array( bool $check_required = false ): array { |
| 72 |
if ( $check_required ) { |
| 73 |
$this->check_required( $this->type ); |
| 74 |
} |
| 75 |
|
| 76 |
return array_merge( |
| 77 |
[ |
| 78 |
"type" => $this->type, |
| 79 |
], |
| 80 |
$this->conditional( "prefillCustomer", $this->prefill_customer, true ), |
| 81 |
$this->conditional( "merchantInfo", $this->merchant_info ), |
| 82 |
$this->conditional( "configuration", $this->configuration ), |
| 83 |
$this->conditional( "subscription", $this->subscription ), |
| 84 |
$this->conditional( "transaction", $this->transaction ), |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
|