Passed
Pull Request — v2.0 (#481)
by Raza
01:58
created

Helpers::addAnnualPlan()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 12
rs 10
1
<?php
2
3
namespace Srmklive\PayPal\Traits\PayPalAPI\Subscriptions;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Str;
7
use Throwable;
8
9
trait Helpers
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $trial_pricing = [];
15
16
    /**
17
     * @var int
18
     */
19
    protected $payment_failure_threshold = 3;
20
21
    /**
22
     * @var array
23
     */
24
    protected $product;
25
26
    /**
27
     * @var array
28
     */
29
    protected $billing_plan;
30
31
    /**
32
     * Setup a subscription.
33
     *
34
     * @param string $customer_name
35
     * @param string $customer_email
36
     * @param string $start_date
37
     *
38
     * @throws Throwable
39
     *
40
     * @return array|\Psr\Http\Message\StreamInterface|string
41
     */
42
    public function setupSubscription($customer_name, $customer_email, $start_date = '')
43
    {
44
        $start_date = isset($start_date) ? Carbon::parse($start_date)->toIso8601String() : Carbon::now()->toIso8601String();
45
46
        $subscription = $this->createSubscription([
0 ignored issues
show
Bug introduced by
It seems like createSubscription() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        /** @scrutinizer ignore-call */ 
47
        $subscription = $this->createSubscription([
Loading history...
47
            'plan_id'    => $this->billing_plan['id'],
48
            'start_time' => $start_date,
49
            'quantity'   => 1,
50
            'subscriber' => [
51
                'name'          => [
52
                    'given_name' => $customer_name,
53
                ],
54
                'email_address' => $customer_email,
55
            ],
56
        ]);
57
58
        unset($this->product);
59
        unset($this->billing_plan);
60
        unset($this->trial_pricing);
61
62
        return $subscription;
63
    }
64
65
    /**
66
     * Add a subscription trial pricing tier.
67
     *
68
     * @param string    $interval_type
69
     * @param string    $interval_count
70
     * @param float|int $price
71
     *
72
     * @return \Srmklive\PayPal\Services\PayPal
73
     */
74
    public function addPlanTrialPricing($interval_type, $interval_count, $price = 0)
75
    {
76
        $this->trial_pricing = $this->addPlanBillingCycle($interval_type, $interval_count, $price, true);
0 ignored issues
show
Bug introduced by
$interval_count of type string is incompatible with the type integer expected by parameter $interval_count of Srmklive\PayPal\Traits\P...::addPlanBillingCycle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $this->trial_pricing = $this->addPlanBillingCycle($interval_type, /** @scrutinizer ignore-type */ $interval_count, $price, true);
Loading history...
77
78
        return $this;
79
    }
80
81
    /**
82
     * Create a recurring daily billing plan.
83
     *
84
     * @param string    $name
85
     * @param string    $description
86
     * @param float|int $price
87
     *
88
     * @throws Throwable
89
     *
90
     * @return \Srmklive\PayPal\Services\PayPal
91
     */
92
    public function addDailyPlan($name, $description, $price)
93
    {
94
        if (isset($this->billing_plan)) {
95
            return $this;
96
        }
97
98
        $plan_pricing = $this->addPlanBillingCycle('DAY', 1, $price);
99
        $billing_cycles = collect([$this->trial_pricing, $plan_pricing])->filter()->toArray();
100
101
        $this->addBillingPlan($name, $description, $billing_cycles);
102
103
        return $this;
104
    }
105
106
    /**
107
     * Create a recurring weekly billing plan.
108
     *
109
     * @param string    $name
110
     * @param string    $description
111
     * @param float|int $price
112
     *
113
     * @throws Throwable
114
     *
115
     * @return \Srmklive\PayPal\Services\PayPal
116
     */
117
    public function addWeeklyPlan($name, $description, $price)
118
    {
119
        if (isset($this->billing_plan)) {
120
            return $this;
121
        }
122
123
        $plan_pricing = $this->addPlanBillingCycle('WEEK', 1, $price);
124
        $billing_cycles = collect([$this->trial_pricing, $plan_pricing])->filter()->toArray();
125
126
        $this->addBillingPlan($name, $description, $billing_cycles);
127
128
        return $this;
129
    }
130
131
    /**
132
     * Create a recurring monthly billing plan.
133
     *
134
     * @param string    $name
135
     * @param string    $description
136
     * @param float|int $price
137
     *
138
     * @throws Throwable
139
     *
140
     * @return \Srmklive\PayPal\Services\PayPal
141
     */
142
    public function addMonthlyPlan($name, $description, $price)
143
    {
144
        if (isset($this->billing_plan)) {
145
            return $this;
146
        }
147
148
        $plan_pricing = $this->addPlanBillingCycle('MONTH', 1, $price);
149
        $billing_cycles = collect([$this->trial_pricing, $plan_pricing])->filter()->toArray();
150
151
        $this->addBillingPlan($name, $description, $billing_cycles);
152
153
        return $this;
154
    }
155
156
    /**
157
     * Create a recurring annual billing plan.
158
     *
159
     * @param string    $name
160
     * @param string    $description
161
     * @param float|int $price
162
     *
163
     * @throws Throwable
164
     *
165
     * @return \Srmklive\PayPal\Services\PayPal
166
     */
167
    public function addAnnualPlan($name, $description, $price)
168
    {
169
        if (isset($this->billing_plan)) {
170
            return $this;
171
        }
172
173
        $plan_pricing = $this->addPlanBillingCycle('YEAR', 1, $price);
174
        $billing_cycles = collect([$this->trial_pricing, $plan_pricing])->filter()->toArray();
175
176
        $this->addBillingPlan($name, $description, $billing_cycles);
177
178
        return $this;
179
    }
180
181
    /**
182
     * Add Plan's Billing cycle.
183
     *
184
     * @param string $interval_unit
185
     * @param int    $interval_count
186
     * @param float  $price
187
     * @param bool   $trial
188
     *
189
     * @return array
190
     */
191
    protected function addPlanBillingCycle($interval_unit, $interval_count, $price, $trial = false)
192
    {
193
        $pricing_scheme = [
194
            'fixed_price' => [
195
                'value'         => $price,
196
                'currency_code' => $this->getCurrency(),
0 ignored issues
show
Bug introduced by
It seems like getCurrency() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

196
                'currency_code' => $this->/** @scrutinizer ignore-call */ getCurrency(),
Loading history...
197
            ],
198
        ];
199
200
        return [
201
            'frequency' => [
202
                'interval_unit'  => $interval_unit,
203
                'interval_count' => $interval_count,
204
            ],
205
            'tenure_type'    => ($trial === true) ? 'TRIAL' : 'REGULAR',
206
            'sequence'       => ($trial === true) ? 1 : 2,
207
            'total_cycles'   => ($trial === true) ? 1 : 0,
208
            'pricing_scheme' => $pricing_scheme,
209
        ];
210
    }
211
212
    /**
213
     * Create a product for a subscription's billing plan.
214
     *
215
     * @param string $name
216
     * @param string $description
217
     * @param string $type
218
     * @param string $category
219
     *
220
     * @throws Throwable
221
     *
222
     * @return \Srmklive\PayPal\Services\PayPal
223
     */
224
    public function addProduct($name, $description, $type, $category)
225
    {
226
        if (isset($this->product)) {
227
            return $this;
228
        }
229
230
        $request_id = Str::random();
231
232
        $this->product = $this->createProduct([
0 ignored issues
show
Bug introduced by
It seems like createProduct() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

232
        /** @scrutinizer ignore-call */ 
233
        $this->product = $this->createProduct([
Loading history...
233
            'name'          => $name,
234
            'description'   => $description,
235
            'type'          => $type,
236
            'category'      => $category,
237
        ], $request_id);
238
239
        return $this;
240
    }
241
242
    /**
243
     * Add subscription's billing plan's product by ID.
244
     *
245
     * @param string $product_id
246
     *
247
     * @return \Srmklive\PayPal\Services\PayPal
248
     */
249
    public function addProductById($product_id)
250
    {
251
        $this->product = [
252
            'id' => $product_id,
253
        ];
254
255
        return $this;
256
    }
257
258
    /**
259
     * Add subscription's billing plan by ID.
260
     *
261
     * @param string $plan_id
262
     *
263
     * @return \Srmklive\PayPal\Services\PayPal
264
     */
265
    public function addBillingPlanById($plan_id)
266
    {
267
        $this->billing_plan = [
268
            'id' => $plan_id,
269
        ];
270
271
        return $this;
272
    }
273
274
    /**
275
     * Create a product for a subscription's billing plan.
276
     *
277
     * @param string $name
278
     * @param string $description
279
     * @param array  $billing_cycles
280
     *
281
     * @throws Throwable
282
     *
283
     * @return void
284
     */
285
    protected function addBillingPlan($name, $description, array $billing_cycles)
286
    {
287
        $request_id = Str::random();
288
289
        $plan_params = [
290
            'product_id'          => $this->product['id'],
291
            'name'                => $name,
292
            'description'         => $description,
293
            'status'              => 'ACTIVE',
294
            'billing_cycles'      => $billing_cycles,
295
            'payment_preferences' => [
296
                'auto_bill_outstanding'     => true,
297
                'setup_fee_failure_action'  => 'CONTINUE',
298
                'payment_failure_threshold' => $this->payment_failure_threshold,
299
            ],
300
        ];
301
302
        $this->billing_plan = $this->createPlan($plan_params, $request_id);
0 ignored issues
show
Bug introduced by
It seems like createPlan() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

302
        /** @scrutinizer ignore-call */ 
303
        $this->billing_plan = $this->createPlan($plan_params, $request_id);
Loading history...
303
    }
304
}
305