Completed
Pull Request — master (#22)
by ARCANEDEV
07:43
created

Charge::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

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 0
cts 2
cp 0
crap 2
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
53
    const FRAUDULENT = 'fraudulent';
54
55
    /* ------------------------------------------------------------------------------------------------
56
     |  CRUD Functions
57
     | ------------------------------------------------------------------------------------------------
58
     */
59
    /**
60
     * List all Charges.
61
     * @link   https://stripe.com/docs/api/php#list_charges
62
     *
63
     * @param  array|null         $params
64
     * @param  array|string|null  $options
65
     *
66
     * @return \Arcanedev\Stripe\Collection|array
67
     */
68 10
    public static function all($params = [], $options = null)
69
    {
70 10
        return self::scopedAll($params, $options);
71
    }
72
73
    /**
74
     * Retrieve a Charge.
75
     * @link   https://stripe.com/docs/api/php#retrieve_charge
76
     *
77
     * @param  string             $id
78
     * @param  array|string|null  $options
79
     *
80
     * @return self
81
     */
82 40
    public static function retrieve($id, $options = null)
83
    {
84 40
        return self::scopedRetrieve($id, $options);
85
    }
86
87
    /**
88
     * Create a new charge (charging a credit card).
89
     * @link   https://stripe.com/docs/api/php#create_charge
90
     *
91
     * @param  array              $params
92
     * @param  array|string|null  $options
93
     *
94
     * @return self|array
95
     */
96 120
    public static function create($params = [], $options = null)
97
    {
98 120
        return self::scopedCreate($params, $options);
99
    }
100
101
    /**
102
     * Save/Update a Charge.
103
     * @link   https://stripe.com/docs/api/php#update_charge
104
     *
105
     * @param  array|string|null  $options
106
     *
107
     * @return self
108
     */
109 10
    public function save($options = null)
110
    {
111 10
        return self::scopedSave($options);
112
    }
113
114
    /**
115
     * Update a Charge.
116
     * @link   https://stripe.com/docs/api/php#update_charge
117
     *
118
     * @param  string             $id
119
     * @param  array|null         $params
120
     * @param  array|string|null  $options
121
     *
122
     * @return self
123
     */
124
    public static function update($id, $params = [], $options = null)
125
    {
126
        return self::scopedUpdate($id, $params, $options);
127
    }
128
129
    /**
130
     * Creating a new refund.
131
     * @link   https://stripe.com/docs/api/php#create_refund
132
     *
133
     * @param  array|null         $params
134
     * @param  array|string|null  $options
135
     *
136
     * @return self
137
     */
138 10
    public function refund($params = [], $options = null)
139
    {
140 10
        return self::scopedPostCall(
141 10
            $this->instanceUrl() . '/refund', $params, $options
142 8
        );
143
    }
144
145
    /**
146
     * Capture a charge.
147
     * @link   https://stripe.com/docs/api/php#capture_charge
148
     *
149
     * @param  array|null         $params
150
     * @param  array|string|null  $options
151
     *
152
     * @return self
153
     */
154 5
    public function capture($params = [], $options = null)
155
    {
156 5
        return self::scopedPostCall(
157 5
            $this->instanceUrl() . '/capture', $params, $options
158 4
        );
159
    }
160
161
    /**
162
     * Mark charge as Fraudulent.
163
     * @param  array|string|null  $options
164
     *
165
     * @return self
166
     */
167 5
    public function markAsFraudulent($options = null)
168
    {
169 5
        return $this->updateFraudDetails(false);
170
    }
171
172
    /**
173
     * Mark charge as Safe.
174
     * @param  array|string|null  $options
175
     *
176
     * @return self
177
     */
178 5
    public function markAsSafe($options = null)
179
    {
180 5
        return $this->updateFraudDetails(true);
181
    }
182
183
    /* ------------------------------------------------------------------------------------------------
184
     |  Other Functions
185
     | ------------------------------------------------------------------------------------------------
186
     */
187
    /**
188
     * Update charge's fraud details.
189
     *
190
     * @param  bool               $safe
191
     * @param  array|string|null  $options
192
     *
193
     * @return self
194
     */
195 10
    private function updateFraudDetails($safe = false, $options = null)
196
    {
197
        $params = [
198
            'fraud_details' => [
199 10
                'user_report' => $safe ? self::SAFE : self::FRAUDULENT,
200 8
            ],
201 8
        ];
202
203 10
        return $this->scopedPostCall($this->instanceUrl(), $params, $options);
204
    }
205
}
206