Completed
Push — main ( deac67...53d69c )
by Stas
06:40
created

ValidationResponse::getPurchaseType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Huawei\IAP\Response;
4
5
abstract class ValidationResponse
6
{
7
    /**
8
     * @var int
9
     * Success
10
     */
11
    const RESPONSE_CODE_0 = 0;
12
    /**
13
     * @var int
14
     * The parameter passed to the API is invalid.
15
     * This error may also indicate that an agreement is not signed
16
     * or parameters are not set correctly for the in-app purchase
17
     * settlement in HUAWEI IAP, or the required permission is not in the list.
18
     *
19
     * Solution: Check whether the parameter passed to the API is correctly set.
20
     * If so, check whether required settings in HUAWEI IAP are correctly configured.
21
     */
22
    const RESPONSE_CODE_5 = 5;
23
24
    /**
25
     * @var int
26
     * A critical error occurs during API operations.
27
     *
28
     * Solution: Rectify the fault based on the error information in the response.
29
     * If the fault persists, contact Huawei technical support.
30
     */
31
    const RESPONSE_CODE_6 = 6;
32
    /**
33
     * @var int
34
     * A user failed to consume or confirm a product because the user does not own the product.
35
     */
36
    const RESPONSE_CODE_8 = 8;
37
    /**
38
     * @var int
39
     * The product cannot be consumed or confirmed because it has been consumed or confirmed.
40
     */
41
    const RESPONSE_CODE_9 = 9;
42
    /**
43
     * @var int
44
     * The user account is abnormal, for example, the user has been deregistered.
45
     */
46
    const RESPONSE_CODE_11 = 11;
47
    /**
48
     * @var int
49
     * The order does not exist. Only the latest order of the specified product can be queried.
50
     * The order in this query may not the latest one.
51
     *
52
     * Solution: Token verification is not required for historical orders.
53
     * Make sure that your integration is performed based on the guide.
54
     * If the problem persists, contact Huawei technical support.
55
     */
56
    const RESPONSE_CODE_12 = 12;
57
58
    protected $dataModel = [];
59
    protected $responseCode;
60
    protected $responseMessage;
61
62 1
    public function __construct(array $raw)
63
    {
64 1
        $this->parseDataModel($raw);
65 1
        $this->parseResonseDetails($raw);
66 1
    }
67
68
    abstract protected function parseDataModel(array $raw);
69
70 1
    protected function parseResonseDetails(array $raw)
71
    {
72 1
        $responseCode = $raw['responseCode'] ?? null;
73
74 1
        if ($responseCode !== null) {
75 1
            $this->responseCode = (int)$responseCode;
76
        }
77
78 1
        $responseMessage = $raw['responseMessage'] ?? null;
79
80 1
        if ($responseMessage !== null) {
81
            $this->responseMessage = (string)$responseMessage;
82
        }
83 1
    }
84
85
    public function getResponseMessage(): ?string
86
    {
87
        return $this->responseMessage;
88
    }
89
90
    public function getResponseCode(): ?int
91
    {
92
        return $this->responseCode;
93
    }
94
95
    public function isResponseValid(): bool
96
    {
97
        return $this->responseCode === self::RESPONSE_CODE_0;
98
    }
99
100
    /**
101
     * Returns TRUE if such property exists in dataModel,
102
     * Otherwise - FALSE
103
     *
104
     * @param string $property
105
     *
106
     * @return bool
107
     */
108
    public function isDatumExists(string $property): bool
109
    {
110
        return array_key_exists($property, $this->dataModel);
111
    }
112
113
    /**
114
     * Get dataModel property if exists
115
     *
116
     * @param string $property
117
     *
118
     * @return mixed|null
119
     */
120
    public function getDatum(string $property)
121
    {
122
        return $this->dataModel[$property] ?? null;
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    public function getDataModel()
129
    {
130
        return $this->dataModel;
131
    }
132
133
    /**
134
     * For consumables or non-consumables, the value is always false.
135
     *
136
     * For subscriptions, the options are as follows:
137
     * - true:  A subscription is in active state and will be automatically renewed
138
     *          on the next renewal date.
139
     * - false: A user has canceled the subscription. The user can access the subscribed
140
     *          content before the next renewal date but will be unable to access
141
     *          the content after the date unless the user enables automatic renewal.
142
     *          If a grace period is provided, this value remains TRUE for the subscription
143
     *          as long as it is still in the grace period. The next settlement date
144
     *          is automatically extended every day until the grace period ends
145
     *          or the user changes the payment method.
146
     *
147
     * @return bool
148
     */
149
    public function isAutoRenewing(): bool
150
    {
151
        return $this->dataModel['autoRenewing'];
152
    }
153
154
    /**
155
     * App ID
156
     *
157
     * @return string
158
     */
159
    public function getApplicationId(): string
160
    {
161
        return $this->dataModel['applicationId'];
162
    }
163
164
    /**
165
     * Product type. The options are as follows:
166
     * 0: consumable
167
     * 1: non-consumable
168
     * 2: subscription
169
     *
170
     * @return int
171
     */
172
    public function getKind(): int
173
    {
174
        return $this->dataModel['kind'];
175
    }
176
    /**
177
     * App package name.
178
     *
179
     * @return string|null
180
     */
181
    public function getPackageName(): ?string
182
    {
183
        return $this->dataModel['packageName'] ?? null;
184
    }
185
186
    /**
187
     * Product ID. Each product must have a unique ID,
188
     * which is maintained in the PMS or passed when the app initiates a purchase.
189
     *
190
     * @return string
191
     */
192
    public function getProductId(): string
193
    {
194
        return $this->dataModel['productId'];
195
    }
196
197
    /**
198
     * Order ID, which uniquely identifies a transaction and is generated
199
     * by the Huawei IAP server during payment.
200
     *
201
     * A different order ID is generated for each fee deduction.
202
     *
203
     * @return string
204
     */
205
    public function getOrderId(): string
206
    {
207
        return $this->dataModel['orderId'];
208
    }
209
210
    /**
211
     * ID of the subscription group to which a subscription belongs.
212
     *
213
     * @note Returned only in the subscription scenario.
214
     * @return string|null
215
     */
216
    public function getProductGroup(): ?string
217
    {
218
        return $this->dataModel['productGroup'] ?? null;
219
    }
220
221
    /**
222
     * Timestamp of the purchase time, which is the number of milliseconds
223
     * from 00:00:00 on January 1, 1970 to the purchase time.
224
     *
225
     * If the purchase is not complete, this parameter is left empty.
226
     *
227
     * @note Returned only in the subscription scenario.
228
     * @return int|null
229
     */
230
    public function getPurchaseTime(): ?int
231
    {
232
        return $this->dataModel['purchaseTime'] ?? null;
233
    }
234
235
    /**
236
     * Transaction status. The options are as follows:
237
     * -1: initialized
238
     *  0: purchased
239
     *  1: canceled
240
     *  2: refunded
241
     *
242
     * @return int
243
     */
244 1
    public function getPurchaseState(): int
245
    {
246 1
        return $this->dataModel['purchaseState'];
247
    }
248
249
    /**
250
     * Information stored on the merchant side, which is passed by the app
251
     * when the payment API is called.
252
     *
253
     * @return null|string
254
     */
255
    public function getDeveloperPayload(): ?string
256
    {
257
        return $this->dataModel['developerPayload'] ?? null;
258
    }
259
260
    /**
261
     * Challenge defined when an app initiates a consumption request.
262
     * The challenge uniquely identifies the consumption request
263
     * and exists only for one-off products.
264
     *
265
     * @return string|null
266
     */
267
    public function getDeveloperChallenge(): ?string
268
    {
269
        return $this->dataModel['developerChallenge'] ?? null;
270
    }
271
272
    /**
273
     * Consumption status, which exists only for one-off products. The options are as follows:
274
     * 0: not consumed
275
     * 1: consumed
276
     *
277
     * @return string|null
278
     */
279
    public function getConsumptionState()
280
    {
281
        return $this->dataModel['consumptionState'] ?? null;
282
    }
283
284
    /**
285
     * Purchase token, which uniquely identifies the mapping between a product and a user.
286
     * It is generated by the Huawei IAP server when the payment is complete.
287
     *
288
     * This parameter uniquely identifies the mapping between a product and a user.
289
     * It does not change when the subscription is renewed.
290
     *
291
     * If the value needs to be stored, you are advised to reserve 128 characters.
292
     *
293
     * @return string
294
     */
295
    public function getPurchaseToken(): string
296
    {
297
        return $this->dataModel['purchaseToken'];
298
    }
299
300
    /**
301
     * Purchase type. The options are as follows:
302
     * 0: in the sandbox
303
     * 1: in the promotion period (currently unsupported)
304
     *
305
     * This parameter is not returned during formal purchase.
306
     *
307
     * @return int
308
     */
309 1
    public function getPurchaseType(): ?int
310
    {
311 1
        return $this->dataModel['purchaseType'] ?? null;
312
    }
313
314
    /**
315
     * Currency. The value must comply with the ISO 4217 standard.
316
     *
317
     * @return string|null
318
     */
319
    public function getCurrency(): ?string
320
    {
321
        return $this->dataModel['currency'] ?? null;
322
    }
323
324
    /**
325
     * Value after the actual price of a product is multiplied by 100.
326
     * The actual price is accurate to two decimal places.
327
     *
328
     * For example, if the value of this parameter is 501, the actual product price is 5.01.
329
     *
330
     * @return int|null
331
     */
332
    public function getPrice(): ?int
333
    {
334
        return $this->dataModel['price'] ?? null;
335
    }
336
337
    /**
338
     * Country or region code, which is used to identify a country or region.
339
     * The value must comply with the ISO 3166 standard.
340
     *
341
     * @return string|null
342
     */
343
    public function getCountry(): ?string
344
    {
345
        return $this->dataModel['country'] ?? null;
346
    }
347
348
    /**
349
     * Transaction order ID.
350
     *
351
     * @return string|null
352
     */
353
    public function getPayOrderId(): ?string
354
    {
355
        return $this->dataModel['payOrderId'] ?? null;
356
    }
357
358
    /**
359
     * Payment method. For details about the value, please refer to
360
     * @see https://developer.huawei.com/consumer/en/doc/HMSCore-References-V5/server-data-model-0000001050986133-V5#EN-US_TOPIC_0000001050986133__section135412662210
361
     *
362
     * @return string|null
363
     */
364
    public function getPayType(): ?string
365
    {
366
        return $this->dataModel['payType'] ?? null;
367
    }
368
369
    /**
370
     * Confirmation status. The options are as follows:
371
     * 0: not confirmed
372
     * 1: confirmed
373
     *
374
     * If this parameter is left empty, no confirmation is required.
375
     *
376
     * @return int|null
377
     * @deprecated This parameter is used only for compatibility with earlier versions.
378
     *             If your app has the latest version integrated, just ignore this parameter.
379
     *
380
     */
381
    public function getConfirmed(): ?int
382
    {
383
        return $this->dataModel['confirmed'] ?? null;
384
    }
385
386
    /**
387
     * Account type. The options are as follows:
388
     * 0: HUAWEI ID
389
     * 1: AppTouch user account
390
     *
391
     * @return int|null
392
     */
393
    public function getAccountFlag(): ?int
394
    {
395
        return $this->dataModel['accountFlag'] ?? null;
396
    }
397
}
398