Completed
Push — master ( 8bbda0...ede634 )
by ARCANEDEV
8s
created

Subscription::deleteDiscount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php namespace Arcanedev\Stripe\Resources;
2
3
use Arcanedev\Stripe\Contracts\Resources\SubscriptionInterface;
4
use Arcanedev\Stripe\Exceptions\InvalidRequestException;
5
use Arcanedev\Stripe\StripeResource;
6
7
/**
8
 * Class     Subscription
9
 *
10
 * @package  Arcanedev\Stripe\Resources
11
 * @author   ARCANEDEV <[email protected]>
12
 * @link     https://stripe.com/docs/api/php#subscription_object
13
 *
14
 * @property  string                            id
15
 * @property  string                            object                   // "subscription"
16
 * @property  boolean                           cancel_at_period_end
17
 * @property  string                            customer
18
 * @property  Plan                              plan
19
 * @property  int                               quantity
20
 * @property  int                               start
21
 * @property  string                            status
22
 * @property  float                             application_fee_percent
23
 * @property  int                               canceled_at
24
 * @property  int                               current_period_start
25
 * @property  int                               current_period_end
26
 * @property  Object                            discount
27
 * @property  int                               ended_at
28
 * @property  \Arcanedev\Stripe\AttachedObject  metadata
29
 * @property  int                               trial_end
30
 * @property  int                               trial_start
31
 *
32
 * @todo:     Complete the properties.
33
 */
34
class Subscription extends StripeResource implements SubscriptionInterface
35
{
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Main Functions
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Get the instance URL.
42
     *
43
     * @throws InvalidRequestException
44
     *
45
     * @return string
46
     */
47 5
    public function instanceUrl()
48
    {
49 5
        $id         = $this['id'];
50
51 5
        if ( ! $id) {
52 5
            throw new InvalidRequestException(
53 5
                'Could not determine which URL to request: class instance has invalid ID: '. $id,
54 1
                null
55 4
            );
56
        }
57
58
        $base           = self::classUrl('Arcanedev\\Stripe\\Resources\\Customer');
59
        $customerId     = urlencode(str_utf8($this['customer']));
60
        $subscriptionId = urlencode(str_utf8($id));
61
62
        return "$base/$customerId/subscriptions/$subscriptionId";
63
    }
64
65
    /* ------------------------------------------------------------------------------------------------
66
     |  CRUD Functions
67
     | ------------------------------------------------------------------------------------------------
68
     */
69
    /**
70
     * Cancel a Subscription.
71
     *
72
     * @link   https://stripe.com/docs/api/php#cancel_subscription
73
     *
74
     * @param  array|null         $params
75
     * @param  array|string|null  $options
76
     *
77
     * @return self
78
     */
79
    public function cancel($params = [], $options = null)
80
    {
81
        return self::scopedDelete($params, $options);
82
    }
83
84
    /**
85
     * Update/Save a Subscription.
86
     *
87
     * @link   https://stripe.com/docs/api/php#update_subscription
88
     *
89
     * @param  array|string|null  $options
90
     *
91
     * @return self
92
     */
93
    public function save($options = null)
94
    {
95
        return self::scopedSave($options);
96
    }
97
98
    /**
99
     * Delete a Subscription Discount.
100
     *
101
     * @link   https://stripe.com/docs/api/curl#delete_subscription_discount
102
     *
103
     * @return self
104
     */
105
    public function deleteDiscount()
106
    {
107
        $url = $this->instanceUrl() . '/discount';
108
        list($response, $opts) = $this->request('delete', $url);
109
110
        $this->refreshFrom(['discount' => null], $opts, true);
111
        unset($response);
112
113
        return $this;
114
    }
115
}
116