| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit; |
| 4 |
|
| 5 |
class WC_Vipps_Agreement extends WC_Vipps_Model { |
| 6 |
public const STATUS_ACTIVE = "ACTIVE"; |
| 7 |
public const STATUS_PENDING = "PENDING"; |
| 8 |
public const STATUS_STOPPED = "STOPPED"; |
| 9 |
public const STATUS_EXPIRED = "EXPIRED"; |
| 10 |
|
| 11 |
protected array $valid_statuses = [ |
| 12 |
self::STATUS_ACTIVE, |
| 13 |
self::STATUS_PENDING, |
| 14 |
self::STATUS_STOPPED, |
| 15 |
self::STATUS_EXPIRED, |
| 16 |
]; |
| 17 |
|
| 18 |
protected array $required_fields = [ |
| 19 |
"pricing", |
| 20 |
"interval", |
| 21 |
"merchant_agreement_url", |
| 22 |
"merchant_redirect_url", |
| 23 |
"product_name", |
| 24 |
"external_id", |
| 25 |
]; |
| 26 |
|
| 27 |
public ?string $id = null; |
| 28 |
|
| 29 |
public ?string $external_id = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var DateTime|string $start |
| 33 |
*/ |
| 34 |
public $start; |
| 35 |
/** |
| 36 |
* @var DateTime|string $start |
| 37 |
*/ |
| 38 |
public $stop; |
| 39 |
public ?string $status = null; |
| 40 |
public ?WC_Vipps_Agreement_Campaign $campaign = null; |
| 41 |
public ?WC_Vipps_Agreement_Pricing $pricing = null; |
| 42 |
public ?string $phone_number = null; |
| 43 |
public ?WC_Vipps_Agreement_Initial_Charge $initial_charge = null; |
| 44 |
public ?WC_Vipps_Agreement_Interval $interval = null; |
| 45 |
public ?bool $is_app = null; |
| 46 |
public ?string $merchant_agreement_url = null; |
| 47 |
public ?string $merchant_redirect_url = null; |
| 48 |
public ?string $product_name = null; |
| 49 |
public ?string $product_description = null; |
| 50 |
public ?string $scope = null; |
| 51 |
public ?bool $skip_landing_page = null; |
| 52 |
public ?string $sub = null; |
| 53 |
public ?string $userinfo_url = null; |
| 54 |
public ?string $vipps_confirmation_url = null; |
| 55 |
|
| 56 |
/** |
| 57 |
* @throws WC_Vipps_Recurring_Invalid_Value_Exception |
| 58 |
*/ |
| 59 |
public function set_status( string $status ): self { |
| 60 |
if ( ! in_array( $status, $this->valid_statuses, true ) ) { |
| 61 |
$class = get_class( $this ); |
| 62 |
throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$status is not a valid value for `status` in $class." ); |
| 63 |
} |
| 64 |
|
| 65 |
$this->status = $status; |
| 66 |
|
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @param DateTime|string $start |
| 72 |
* |
| 73 |
* @throws Exception |
| 74 |
*/ |
| 75 |
public function set_start( $start ): self { |
| 76 |
if ( is_string( $start ) ) { |
| 77 |
$this->start = new DateTime( $start ); |
| 78 |
} else { |
| 79 |
$this->start = $start; |
| 80 |
} |
| 81 |
|
| 82 |
return $this; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @param DateTime|string $stop |
| 87 |
* |
| 88 |
* @throws Exception |
| 89 |
*/ |
| 90 |
public function set_stop( $stop ): self { |
| 91 |
if ( is_string( $stop ) ) { |
| 92 |
$this->stop = new DateTime( $stop ); |
| 93 |
} else { |
| 94 |
$this->stop = $stop; |
| 95 |
} |
| 96 |
|
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
public function set_id( string $id ): self { |
| 101 |
$this->id = $id; |
| 102 |
|
| 103 |
return $this; |
| 104 |
} |
| 105 |
|
| 106 |
public function set_external_id( string $external_id ): self { |
| 107 |
$this->external_id = $external_id; |
| 108 |
|
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @param WC_Vipps_Agreement_Campaign|array $campaign |
| 114 |
*/ |
| 115 |
public function set_campaign( $campaign ): self { |
| 116 |
return $this->_set_value( 'campaign', $campaign, WC_Vipps_Agreement_Campaign::class ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @param WC_Vipps_Agreement_Pricing|array $pricing |
| 121 |
*/ |
| 122 |
public function set_pricing( $pricing ): self { |
| 123 |
return $this->_set_value( 'pricing', $pricing, WC_Vipps_Agreement_Pricing::class ); |
| 124 |
} |
| 125 |
|
| 126 |
public function set_phone_number( string $phone_number ): self { |
| 127 |
$this->phone_number = $phone_number; |
| 128 |
|
| 129 |
return $this; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @param WC_Vipps_Agreement_Initial_Charge|array $initial_charge |
| 134 |
*/ |
| 135 |
public function set_initial_charge( $initial_charge ): self { |
| 136 |
return $this->_set_value( 'initial_charge', $initial_charge, WC_Vipps_Agreement_Initial_Charge::class ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* @param WC_Vipps_Agreement_Interval|array $interval |
| 141 |
*/ |
| 142 |
public function set_interval( $interval ): self { |
| 143 |
return $this->_set_value( 'interval', $interval, WC_Vipps_Agreement_Interval::class ); |
| 144 |
} |
| 145 |
|
| 146 |
public function set_is_app( bool $is_app ): self { |
| 147 |
$this->is_app = $is_app; |
| 148 |
|
| 149 |
return $this; |
| 150 |
} |
| 151 |
|
| 152 |
public function set_merchant_agreement_url( string $merchant_agreement_url ): self { |
| 153 |
$this->merchant_agreement_url = $merchant_agreement_url; |
| 154 |
|
| 155 |
return $this; |
| 156 |
} |
| 157 |
|
| 158 |
public function set_merchant_redirect_url( string $merchant_redirect_url ): self { |
| 159 |
$this->merchant_redirect_url = $merchant_redirect_url; |
| 160 |
|
| 161 |
return $this; |
| 162 |
} |
| 163 |
|
| 164 |
public function set_product_name( string $product_name ): self { |
| 165 |
if ( strlen( $product_name ) > 45 ) { |
| 166 |
$product_name = mb_substr( $product_name, 0, 42 ) . '...'; |
| 167 |
} |
| 168 |
|
| 169 |
$this->product_name = $product_name; |
| 170 |
|
| 171 |
return $this; |
| 172 |
} |
| 173 |
|
| 174 |
public function set_product_description( string $product_description ): self { |
| 175 |
if ( strlen( $product_description ) > 100 ) { |
| 176 |
$product_description = mb_substr( $product_description, 0, 97 ) . '...'; |
| 177 |
} |
| 178 |
|
| 179 |
$this->product_description = $product_description; |
| 180 |
|
| 181 |
return $this; |
| 182 |
} |
| 183 |
|
| 184 |
public function set_scope( string $scope ): self { |
| 185 |
$this->scope = $scope; |
| 186 |
|
| 187 |
return $this; |
| 188 |
} |
| 189 |
|
| 190 |
public function set_skip_landing_page( bool $skip_landing_page ): self { |
| 191 |
$this->skip_landing_page = $skip_landing_page; |
| 192 |
|
| 193 |
return $this; |
| 194 |
} |
| 195 |
|
| 196 |
public function set_sub( string $sub ): self { |
| 197 |
$this->sub = $sub; |
| 198 |
|
| 199 |
return $this; |
| 200 |
} |
| 201 |
|
| 202 |
public function set_userinfo_url( string $userinfo_url ): self { |
| 203 |
$this->userinfo_url = $userinfo_url; |
| 204 |
|
| 205 |
return $this; |
| 206 |
} |
| 207 |
|
| 208 |
public function set_vipps_confirmation_url( string $vipps_confirmation_url ): self { |
| 209 |
$this->vipps_confirmation_url = $vipps_confirmation_url; |
| 210 |
|
| 211 |
return $this; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* @throws WC_Vipps_Recurring_Missing_Value_Exception |
| 216 |
*/ |
| 217 |
public function to_array( bool $check_required = false ): array { |
| 218 |
if ( $check_required ) { |
| 219 |
$this->check_required(); |
| 220 |
} |
| 221 |
|
| 222 |
return array_merge( |
| 223 |
[ |
| 224 |
"productName" => $this->product_name, |
| 225 |
"pricing" => $this->pricing ? $this->pricing->to_array( $check_required ) : null, |
| 226 |
"interval" => $this->interval ? $this->interval->to_array( $check_required ) : null, |
| 227 |
], |
| 228 |
$this->conditional( "start", $this->start ), |
| 229 |
$this->conditional( "stop", $this->stop ), |
| 230 |
$this->conditional( "id", $this->id ), |
| 231 |
$this->conditional( "externalId", $this->external_id ), |
| 232 |
$this->conditional( "status", $this->status ), |
| 233 |
$this->conditional( "campaign", $this->campaign ), |
| 234 |
$this->conditional( "phoneNumber", $this->phone_number ), |
| 235 |
$this->conditional( "initialCharge", $this->initial_charge ), |
| 236 |
$this->conditional( "isApp", $this->is_app ), |
| 237 |
$this->conditional( "merchantAgreementUrl", $this->merchant_agreement_url ), |
| 238 |
$this->conditional( "merchantRedirectUrl", $this->merchant_redirect_url ), |
| 239 |
$this->conditional( "productDescription", $this->product_description ), |
| 240 |
$this->conditional( "scope", $this->scope ), |
| 241 |
$this->conditional( "skipLandingPage", $this->skip_landing_page ), |
| 242 |
$this->conditional( "sub", $this->sub ), |
| 243 |
$this->conditional( "userinfoUrl", $this->userinfo_url ), |
| 244 |
$this->conditional( "vippsConfirmationUrl", $this->vipps_confirmation_url ), |
| 245 |
); |
| 246 |
} |
| 247 |
} |
| 248 |
|