PluginProbe
Pay with Vipps and MobilePay for WooCommerce / 5.4.3
Pay with Vipps and MobilePay for WooCommerce v5.4.3
6.2.3 6.2.2 6.2.1 6.2.0 6.1.10 6.1.9 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1.0 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 5.4.3 5.4.2 5.4.1 5.4.0 All 185 releases
woo-vipps / recurring / includes / models / WC_Vipps_Charge.php

WC_Vipps_Charge.php in Pay with Vipps and MobilePay for WooCommerce 5.4.3, at recurring/includes/models/WC_Vipps_Charge.php

204 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined( 'ABSPATH' ) || exit;
4
5 class WC_Vipps_Charge extends WC_Vipps_Model {
6 public const STATUS_PENDING = "PENDING";
7 public const STATUS_DUE = "DUE";
8 public const STATUS_PROCESSING = "PROCESSING";
9 public const STATUS_UNKNOWN = "UNKNOWN";
10 public const STATUS_CHARGED = "CHARGED";
11 public const STATUS_FAILED = "FAILED";
12 public const STATUS_REFUNDED = "REFUNDED";
13 public const STATUS_PARTIALLY_REFUNDED = "PARTIALLY_REFUNDED";
14 public const STATUS_RESERVED = "RESERVED";
15 public const STATUS_PARTIALLY_CAPTURED = "PARTIALLY_CAPTURED";
16 public const STATUS_CANCELLED = "CANCELLED";
17
18 protected array $valid_statuses = [
19 self::STATUS_PENDING,
20 self::STATUS_DUE,
21 self::STATUS_PROCESSING,
22 self::STATUS_UNKNOWN,
23 self::STATUS_CHARGED,
24 self::STATUS_FAILED,
25 self::STATUS_REFUNDED,
26 self::STATUS_PARTIALLY_REFUNDED,
27 self::STATUS_RESERVED,
28 self::STATUS_PARTIALLY_CAPTURED,
29 self::STATUS_CANCELLED,
30 ];
31
32 public const TYPE_RECURRING = "RECURRING";
33 public const TYPE_INITIAL = "INITIAL";
34
35 protected array $valid_types = [
36 self::TYPE_RECURRING,
37 self::TYPE_INITIAL,
38 ];
39
40 public const TRANSACTION_TYPE_DIRECT_CAPTURE = "DIRECT_CAPTURE";
41 public const TRANSACTION_TYPE_RESERVE_CAPTURE = "RESERVE_CAPTURE";
42
43 protected array $valid_transaction_types = [
44 self::TRANSACTION_TYPE_DIRECT_CAPTURE,
45 self::TRANSACTION_TYPE_RESERVE_CAPTURE,
46 ];
47
48 protected array $required_fields = [
49 "amount",
50 "order_id",
51 "description",
52 "due",
53 "retry_days"
54 ];
55
56 public ?string $id = null;
57 public ?string $order_id = null;
58 public ?string $status = null;
59 public ?string $type = null;
60 public ?int $amount = null;
61 public ?string $transaction_id = null;
62 public ?string $transaction_type = null;
63 public ?string $description = null;
64 public ?string $currency = null;
65 public ?string $external_id = null;
66
67 /**
68 * @var DateTime|string $due
69 */
70 public $due;
71 public ?int $retry_days = null;
72 public ?string $failure_reason = null;
73 public ?string $failure_description = null;
74 public ?array $summary = null;
75 public ?array $history = null;
76
77 public function set_id( string $id ): self {
78 $this->id = $id;
79
80 return $this;
81 }
82
83 public function set_order_id( string $order_id ): self {
84 $this->order_id = $order_id;
85
86 return $this;
87 }
88
89 public function set_amount( int $amount ): self {
90 $this->amount = $amount;
91
92 return $this;
93 }
94
95 /**
96 * @throws WC_Vipps_Recurring_Invalid_Value_Exception
97 */
98 public function set_type( string $type ): self {
99 if ( ! in_array( $type, $this->valid_types, true ) ) {
100 $class = get_class( $this );
101 throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$type is not a valid value for `type` in $class." );
102 }
103
104 $this->type = $type;
105
106 return $this;
107 }
108
109 /**
110 * @throws WC_Vipps_Recurring_Invalid_Value_Exception
111 */
112 public function set_status( string $status ): self {
113 if ( ! in_array( $status, $this->valid_statuses, true ) ) {
114 $class = get_class( $this );
115 throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$status is not a valid value for `status` in $class." );
116 }
117
118 $this->status = $status;
119
120 return $this;
121 }
122
123 /**
124 * @throws WC_Vipps_Recurring_Invalid_Value_Exception
125 */
126 public function set_transaction_type( string $transaction_type ): self {
127 if ( ! in_array( $transaction_type, $this->valid_transaction_types, true ) ) {
128 $class = get_class( $this );
129 throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$transaction_type is not a valid value for `transaction_type` in $class." );
130 }
131
132 $this->transaction_type = $transaction_type;
133
134 return $this;
135 }
136
137 public function set_transaction_id( string $transaction_id ): self {
138 $this->transaction_id = $transaction_id;
139
140 return $this;
141 }
142
143 public function set_description( ?string $description ): self {
144 if ( strlen( $description ) > 45 ) {
145 $description = mb_substr( $description, 0, 42 ) . '...';
146 }
147
148 $this->description = $description;
149
150 return $this;
151 }
152
153 /**
154 * @param DateTime|string $due
155 *
156 * @throws Exception
157 */
158 public function set_due( $due ): self {
159 if ( is_string( $due ) ) {
160 $this->due = new DateTime( $due );
161 } else {
162 $this->due = $due;
163 }
164
165
166 return $this;
167 }
168
169 public function set_retry_days( int $retry_days ): self {
170 $this->retry_days = $retry_days;
171
172 return $this;
173 }
174
175 public function set_external_id( string $external_id ): self {
176 $this->external_id = $external_id;
177
178 return $this;
179 }
180
181 /**
182 * @throws WC_Vipps_Recurring_Missing_Value_Exception
183 */
184 public function to_array( bool $check_required = false ): array {
185 if ( $check_required ) {
186 $this->check_required();
187 }
188
189 return array_merge(
190 [
191 "orderId" => $this->order_id,
192 "amount" => $this->amount,
193 "description" => $this->description,
194 "due" => $this->serialize_value( $this->due, 'Y-m-d' ),
195 "retryDays" => $this->retry_days
196 ],
197 $this->conditional( "type", $this->type ),
198 $this->conditional( "transactionType", $this->transaction_type ),
199 $this->conditional( "transactionId", $this->transaction_id ),
200 $this->conditional( "externalId", $this->external_id )
201 );
202 }
203 }
204