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

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