| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Subscriptions\DataTransferObjects; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class SubscriptionArgs |
| 7 |
* @since 2.18.0 |
| 8 |
*/ |
| 9 |
final class SubscriptionArgs |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
public $period; |
| 15 |
/** |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
public $times; |
| 19 |
/** |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $frequency; |
| 23 |
/** |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
public $formTitle; |
| 27 |
/** |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
public $formId; |
| 31 |
/** |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
public $priceId; |
| 35 |
/** |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public $price; |
| 39 |
/** |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public $status; |
| 43 |
/** |
| 44 |
* @var float|int|string |
| 45 |
*/ |
| 46 |
public $initialAmount; |
| 47 |
/** |
| 48 |
* @var float|int|string |
| 49 |
*/ |
| 50 |
public $recurringAmount; |
| 51 |
/** |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
public $periodInterval; |
| 55 |
/** |
| 56 |
* @var float|int |
| 57 |
*/ |
| 58 |
public $frequencyIntervalCount; |
| 59 |
/** |
| 60 |
* @var int |
| 61 |
*/ |
| 62 |
public $billTimes; |
| 63 |
/** |
| 64 |
* @var int|mixed |
| 65 |
*/ |
| 66 |
public $recurringFeeAmount; |
| 67 |
/** |
| 68 |
* @var mixed|string |
| 69 |
*/ |
| 70 |
public $profileId; |
| 71 |
/** |
| 72 |
* @var mixed|string |
| 73 |
*/ |
| 74 |
public $transactionId; |
| 75 |
|
| 76 |
/** |
| 77 |
* Convert data from request into DTO |
| 78 |
* |
| 79 |
* @since 2.18.0 |
| 80 |
* |
| 81 |
* @return self |
| 82 |
*/ |
| 83 |
public static function fromRequest(array $request) |
| 84 |
{ |
| 85 |
$self = new static(); |
| 86 |
|
| 87 |
$self->period = $request['period']; |
| 88 |
$self->times = $request['times']; |
| 89 |
$self->frequency = $request['frequency']; |
| 90 |
$self->formTitle = $request['formTitle']; |
| 91 |
$self->formId = $request['formId']; |
| 92 |
$self->priceId = $request['priceId']; |
| 93 |
$self->price = $request['price']; |
| 94 |
$self->status = $request['status']; |
| 95 |
$self->initialAmount = give_sanitize_amount_for_db($self->price); |
| 96 |
$self->recurringAmount = give_sanitize_amount_for_db($self->price); |
| 97 |
$self->recurringFeeAmount = isset($request['recurringFeeAmount']) ? $request['recurringFeeAmount'] : 0; |
| 98 |
$self->profileId = isset($request['profileId']) ? $request['profileId'] : ''; |
| 99 |
$self->transactionId = isset($request['transactionId']) ? $request['transactionId'] : ''; |
| 100 |
$self->periodInterval = self::get_interval($self->period, $self->frequency); |
| 101 |
$self->frequencyIntervalCount = self::get_interval_count($self->period, $self->frequency); |
| 102 |
|
| 103 |
// @phpstan-ignore-next-line function is undefined in add-on. Also, the calling class, CreateSubscriptionAction, does not seem to be used... |
| 104 |
$self->billTimes = give_recurring_calculate_times($self->times, $self->frequency); |
| 105 |
|
| 106 |
return $self; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @since 2.18.0 |
| 111 |
* |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
public function toArray() |
| 115 |
{ |
| 116 |
return [ |
| 117 |
'name' => $this->formTitle, |
| 118 |
'id' => $this->formId, |
| 119 |
// @TODO Deprecate w/ backwards compatiblity. |
| 120 |
'form_id' => $this->formId, |
| 121 |
'price_id' => $this->priceId, |
| 122 |
'initial_amount' => $this->initialAmount, |
| 123 |
// add fee here in future. |
| 124 |
'recurring_amount' => $this->recurringAmount, |
| 125 |
'period' => $this->periodInterval, |
| 126 |
'frequency' => $this->frequencyIntervalCount, |
| 127 |
// Passed interval. Example: charge every 3 weeks. |
| 128 |
'bill_times' => $this->billTimes, |
| 129 |
'profile_id' => '', |
| 130 |
// Profile ID for this subscription - This is set by the payment gateway. |
| 131 |
'transaction_id' => '', |
| 132 |
// Transaction ID for this subscription - This is set by the payment gateway. |
| 133 |
'status' => 'pending', |
| 134 |
]; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Gets interval length and interval unit for Authorize.net based on Give subscription period. |
| 139 |
* |
| 140 |
* @since 2.18.0 |
| 141 |
* |
| 142 |
* @param int $frequency |
| 143 |
* |
| 144 |
* @param string $period |
| 145 |
* |
| 146 |
* @return string |
| 147 |
*/ |
| 148 |
private static function get_interval($period, $frequency) |
| 149 |
{ |
| 150 |
$interval = $period; |
| 151 |
|
| 152 |
if ($period === 'quarter') { |
| 153 |
$interval = 'month'; |
| 154 |
} |
| 155 |
|
| 156 |
return $interval; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Gets interval length and interval unit for Authorize.net based on Give subscription period. |
| 161 |
* |
| 162 |
* @since 2.18.0 |
| 163 |
* |
| 164 |
* @param int $frequency |
| 165 |
* |
| 166 |
* @param string $period |
| 167 |
* |
| 168 |
* @return float|int |
| 169 |
*/ |
| 170 |
private static function get_interval_count($period, $frequency) |
| 171 |
{ |
| 172 |
$interval_count = $frequency; |
| 173 |
|
| 174 |
if ($period === 'quarter') { |
| 175 |
$interval_count = 3 * $frequency; |
| 176 |
} |
| 177 |
|
| 178 |
return $interval_count; |
| 179 |
} |
| 180 |
} |
| 181 |
|