| 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 |
public const PROCESSING_MODE_MULTIPLE_ATTEMPTS = "MULTIPLE_ATTEMPTS"; |
| 49 |
public const PROCESSING_MODE_SINGLE_ATTEMPT = "SINGLE_ATTEMPT"; |
| 50 |
|
| 51 |
protected array $valid_processing_modes = [ |
| 52 |
self::PROCESSING_MODE_MULTIPLE_ATTEMPTS, |
| 53 |
self::PROCESSING_MODE_SINGLE_ATTEMPT, |
| 54 |
]; |
| 55 |
|
| 56 |
protected array $required_fields = [ |
| 57 |
"amount", |
| 58 |
"order_id", |
| 59 |
"description", |
| 60 |
"due", |
| 61 |
"retry_days", |
| 62 |
"processing_mode", |
| 63 |
]; |
| 64 |
|
| 65 |
public ?string $id = null; |
| 66 |
public ?string $order_id = null; |
| 67 |
public ?string $status = null; |
| 68 |
public ?string $type = null; |
| 69 |
public ?int $amount = null; |
| 70 |
public ?string $transaction_id = null; |
| 71 |
public ?string $transaction_type = null; |
| 72 |
public ?string $description = null; |
| 73 |
public ?string $currency = null; |
| 74 |
public ?string $external_id = null; |
| 75 |
public ?string $processing_mode = self::PROCESSING_MODE_MULTIPLE_ATTEMPTS; |
| 76 |
|
| 77 |
/** |
| 78 |
* @var DateTime|string $due |
| 79 |
*/ |
| 80 |
public $due; |
| 81 |
public ?int $retry_days = null; |
| 82 |
public ?string $failure_reason = null; |
| 83 |
public ?string $failure_description = null; |
| 84 |
public ?array $summary = null; |
| 85 |
public ?array $history = null; |
| 86 |
|
| 87 |
public function set_id( string $id ): self { |
| 88 |
$this->id = $id; |
| 89 |
|
| 90 |
return $this; |
| 91 |
} |
| 92 |
|
| 93 |
public function set_order_id( string $order_id ): self { |
| 94 |
$this->order_id = $order_id; |
| 95 |
|
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
public function set_amount( int $amount ): self { |
| 100 |
$this->amount = $amount; |
| 101 |
|
| 102 |
return $this; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @throws WC_Vipps_Recurring_Invalid_Value_Exception |
| 107 |
*/ |
| 108 |
public function set_type( string $type ): self { |
| 109 |
if ( ! in_array( $type, $this->valid_types, true ) ) { |
| 110 |
$class = get_class( $this ); |
| 111 |
throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$type is not a valid value for `type` in $class." ); |
| 112 |
} |
| 113 |
|
| 114 |
$this->type = $type; |
| 115 |
|
| 116 |
return $this; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @throws WC_Vipps_Recurring_Invalid_Value_Exception |
| 121 |
*/ |
| 122 |
public function set_status( string $status ): self { |
| 123 |
if ( ! in_array( $status, $this->valid_statuses, true ) ) { |
| 124 |
$class = get_class( $this ); |
| 125 |
throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$status is not a valid value for `status` in $class." ); |
| 126 |
} |
| 127 |
|
| 128 |
$this->status = $status; |
| 129 |
|
| 130 |
return $this; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* @throws WC_Vipps_Recurring_Invalid_Value_Exception |
| 135 |
*/ |
| 136 |
public function set_transaction_type( string $transaction_type ): self { |
| 137 |
if ( ! in_array( $transaction_type, $this->valid_transaction_types, true ) ) { |
| 138 |
$class = get_class( $this ); |
| 139 |
throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$transaction_type is not a valid value for `transaction_type` in $class." ); |
| 140 |
} |
| 141 |
|
| 142 |
$this->transaction_type = $transaction_type; |
| 143 |
|
| 144 |
return $this; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @throws WC_Vipps_Recurring_Invalid_Value_Exception |
| 149 |
*/ |
| 150 |
public function set_processing_mode( string $processing_mode ): self { |
| 151 |
if ( ! in_array( $processing_mode, $this->valid_processing_modes, true ) ) { |
| 152 |
$class = get_class( $this ); |
| 153 |
throw new WC_Vipps_Recurring_Invalid_Value_Exception( "$processing_mode is not a valid value for `processing_mode` in $class." ); |
| 154 |
} |
| 155 |
|
| 156 |
$this->processing_mode = $processing_mode; |
| 157 |
|
| 158 |
return $this; |
| 159 |
} |
| 160 |
|
| 161 |
public function set_transaction_id( string $transaction_id ): self { |
| 162 |
$this->transaction_id = $transaction_id; |
| 163 |
|
| 164 |
return $this; |
| 165 |
} |
| 166 |
|
| 167 |
public function set_description( ?string $description ): self { |
| 168 |
if ( strlen( $description ) > 45 ) { |
| 169 |
$description = mb_substr( $description, 0, 42 ) . '...'; |
| 170 |
} |
| 171 |
|
| 172 |
$this->description = $description; |
| 173 |
|
| 174 |
return $this; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* @param DateTime|string $due |
| 179 |
* |
| 180 |
* @throws Exception |
| 181 |
*/ |
| 182 |
public function set_due( $due ): self { |
| 183 |
if ( is_string( $due ) ) { |
| 184 |
$this->due = new DateTime( $due ); |
| 185 |
} else { |
| 186 |
$this->due = $due; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
return $this; |
| 191 |
} |
| 192 |
|
| 193 |
public function set_retry_days( int $retry_days ): self { |
| 194 |
$this->retry_days = $retry_days; |
| 195 |
|
| 196 |
return $this; |
| 197 |
} |
| 198 |
|
| 199 |
public function set_external_id( string $external_id ): self { |
| 200 |
$this->external_id = $external_id; |
| 201 |
|
| 202 |
return $this; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* @throws WC_Vipps_Recurring_Missing_Value_Exception |
| 207 |
*/ |
| 208 |
public function to_array( bool $check_required = false ): array { |
| 209 |
if ( $check_required ) { |
| 210 |
$this->check_required(); |
| 211 |
} |
| 212 |
|
| 213 |
return array_merge( |
| 214 |
[ |
| 215 |
"orderId" => $this->order_id, |
| 216 |
"amount" => $this->amount, |
| 217 |
"description" => $this->description, |
| 218 |
"due" => $this->serialize_value( $this->due, 'Y-m-d' ), |
| 219 |
"retryDays" => $this->retry_days, |
| 220 |
"processingMode" => $this->processing_mode, |
| 221 |
], |
| 222 |
$this->conditional( "type", $this->type ), |
| 223 |
$this->conditional( "transactionType", $this->transaction_type ), |
| 224 |
$this->conditional( "transactionId", $this->transaction_id ), |
| 225 |
$this->conditional( "externalId", $this->external_id ) |
| 226 |
); |
| 227 |
} |
| 228 |
} |
| 229 |
|