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