Completed
Push — master ( 66a1c0...6efa89 )
by ARCANEDEV
08:32
created

Charge::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php namespace Arcanedev\Stripe\Resources;
2
3
use Arcanedev\Stripe\Contracts\Resources\ChargeInterface;
4
use Arcanedev\Stripe\StripeResource;
5
6
/**
7
 * Class     Charge
8
 *
9
 * @package  Arcanedev\Stripe\Resources
10
 * @author   ARCANEDEV <[email protected]>
11
 * @link     https://stripe.com/docs/api/php#charge_object
12
 *
13
 * @property  string                               id
14
 * @property  string                               object               // 'charge'
15
 * @property  int                                  amount
16
 * @property  int                                  amount_refunded
17
 * @property  string|null                          application_fee
18
 * @property  string                               balance_transaction
19
 * @property  bool                                 captured
20
 * @property  int                                  created              // timestamp
21
 * @property  string                               currency
22
 * @property  string                               customer
23
 * @property  string                               description
24
 * @property  string                               destination
25
 * @property  \Arcanedev\Stripe\Resources\Dispute  dispute
26
 * @property  string                               failure_code
27
 * @property  string                               failure_message
28
 * @property  array                                fraud_details
29
 * @property  string                               invoice
30
 * @property  bool                                 livemode
31
 * @property  \Arcanedev\Stripe\AttachedObject     metadata
32
 * @property  string                               order
33
 * @property  bool                                 paid
34
 * @property  string                               receipt_email
35
 * @property  string                               receipt_number
36
 * @property  bool                                 refunded
37
 * @property  \Arcanedev\Stripe\Collection         refunds
38
 * @property  array                                shipping
39
 * @property  \Arcanedev\Stripe\Resources\Card     source
40
 * @property  string                               source_transfer
41
 * @property  string                               statement_descriptor
42
 * @property  string                               status
43
 * @property  string                               transfer
44
 */
45
class Charge extends StripeResource implements ChargeInterface
46
{
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Constants
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    const SAFE       = 'safe';
52
    const FRAUDULENT = 'fraudulent';
53
54
    /* ------------------------------------------------------------------------------------------------
55
     |  CRUD Functions
56
     | ------------------------------------------------------------------------------------------------
57
     */
58
    /**
59
     * List all Charges.
60
     * @link   https://stripe.com/docs/api/php#list_charges
61
     *
62
     * @param  array|null         $params
63
     * @param  array|string|null  $options
64
     *
65
     * @return \Arcanedev\Stripe\Collection|array
66
     */
67 10
    public static function all($params = [], $options = null)
68
    {
69 10
        return self::scopedAll($params, $options);
70
    }
71
72
    /**
73
     * Retrieve a Charge.
74
     * @link   https://stripe.com/docs/api/php#retrieve_charge
75
     *
76
     * @param  string             $id
77
     * @param  array|string|null  $options
78
     *
79
     * @return self
80
     */
81 50
    public static function retrieve($id, $options = null)
82
    {
83 50
        return self::scopedRetrieve($id, $options);
84
    }
85
86
    /**
87
     * Create a new charge (charging a credit card).
88
     * @link   https://stripe.com/docs/api/php#create_charge
89
     *
90
     * @param  array              $params
91
     * @param  array|string|null  $options
92
     *
93
     * @return self|array
94
     */
95 135
    public static function create($params = [], $options = null)
96
    {
97 135
        return self::scopedCreate($params, $options);
98
    }
99
100
    /**
101
     * Save/Update a Charge.
102
     * @link   https://stripe.com/docs/api/php#update_charge
103
     *
104
     * @param  array|string|null  $options
105
     *
106
     * @return self
107
     */
108 10
    public function save($options = null)
109
    {
110 10
        return self::scopedSave($options);
111
    }
112
113
    /**
114
     * Update a Charge.
115
     * @link   https://stripe.com/docs/api/php#update_charge
116
     *
117
     * @param  string             $id
118
     * @param  array|null         $params
119
     * @param  array|string|null  $options
120
     *
121
     * @return self
122
     */
123 5
    public static function update($id, $params = [], $options = null)
124
    {
125 5
        return self::scopedUpdate($id, $params, $options);
126
    }
127
128
    /**
129
     * Creating a new refund.
130
     * @link   https://stripe.com/docs/api/php#create_refund
131
     *
132
     * @param  array|null         $params
133
     * @param  array|string|null  $options
134
     *
135
     * @return self
136
     */
137 10
    public function refund($params = [], $options = null)
138
    {
139 10
        return self::scopedPostCall(
140 10
            $this->instanceUrl() . '/refund', $params, $options
141 8
        );
142
    }
143
144
    /**
145
     * Capture a charge.
146
     * @link   https://stripe.com/docs/api/php#capture_charge
147
     *
148
     * @param  array|null         $params
149
     * @param  array|string|null  $options
150
     *
151
     * @return self
152
     */
153 5
    public function capture($params = [], $options = null)
154
    {
155 5
        return self::scopedPostCall(
156 5
            $this->instanceUrl() . '/capture', $params, $options
157 4
        );
158
    }
159
160
    /**
161
     * Mark charge as Fraudulent.
162
     * @param  array|string|null  $options
163
     *
164
     * @return self
165
     */
166 5
    public function markAsFraudulent($options = null)
167
    {
168 5
        return $this->updateFraudDetails(false);
169
    }
170
171
    /**
172
     * Mark charge as Safe.
173
     * @param  array|string|null  $options
174
     *
175
     * @return self
176
     */
177 5
    public function markAsSafe($options = null)
178
    {
179 5
        return $this->updateFraudDetails(true);
180
    }
181
182
    /* ------------------------------------------------------------------------------------------------
183
     |  Other Functions
184
     | ------------------------------------------------------------------------------------------------
185
     */
186
    /**
187
     * Update charge's fraud details.
188
     *
189
     * @param  bool               $safe
190
     * @param  array|string|null  $options
191
     *
192
     * @return self
193
     */
194 10
    private function updateFraudDetails($safe = false, $options = null)
195
    {
196
        $params = [
197
            'fraud_details' => [
198 10
                'user_report' => $safe ? self::SAFE : self::FRAUDULENT,
199 8
            ],
200 8
        ];
201
202 10
        return $this->scopedPostCall($this->instanceUrl(), $params, $options);
203
    }
204
}
205