Passed
Push — main ( db3674...c4348d )
by Gabriel
05:36 queued 02:36
created

CompletePurchaseResponse::canProcessModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace ByTIC\Payments\Stripe\Message;
4
5
use ByTIC\Omnipay\Common\Message\Traits\HtmlResponses\ConfirmHtmlTrait;
6
use Omnipay\Common\Message\AbstractResponse;
7
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Message\Traits\CompletePurchaseResponseTrait;
8
use Stripe\PaymentIntent;
9
10
/**
11
 * Class CompletePurchaseResponse
12
 * @package ByTIC\Payments\Gateways\Providers\Paylike\Message
13
 */
14
class CompletePurchaseResponse extends AbstractResponse
15
{
16
    use ConfirmHtmlTrait;
17
    use CompletePurchaseResponseTrait;
18
19
    /**
20
     * @var bool
21
     */
22
    private $successful = false;
23
24
    /**
25
     * @inheritDoc
26
     */
27
    public function __construct(CompletePurchaseRequest $request, $data)
28
    {
29
        parent::__construct($request, $data);
30
31
        if (isset($data['notification']['paymentIntent'])) {
32
            /** @var \Stripe\PaymentIntent $paymentIntent */
33
            $paymentIntent = $data['notification']['paymentIntent'];
34
35
            $this->internalTransactionRef = $paymentIntent->id;
0 ignored issues
show
Bug Best Practice introduced by
The property internalTransactionRef does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
37
            // Amazingly there doesn't seem to be a simple code nor message when the payment succeeds (or fails).
38
            // For now, just use the status for the message, and leave the code blank.
39
            switch ($paymentIntent->status) {
40
                case PaymentIntent::STATUS_SUCCEEDED:
41
                    $this->successful = true;
42
                    $this->message = $paymentIntent->status;
0 ignored issues
show
Bug Best Practice introduced by
The property message does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
                    break;
44
                case PaymentIntent::STATUS_CANCELED:
45
                    $this->successful = false;
46
                    $this->message = 'Canceled by customer';
47
                    break;
48
                default:
49
                    // We don't know what happened, so act accordingly. Would be nice to make this better over time.
50
                    $this->successful = false;
51
                    $this->message = 'Unknown error';
52
                    break;
53
54
            }
55
        } else {
56
            $this->successful = false; // Just make sure.
57
            $this->message = 'Could not retrieve payment';
58
        }
59
    }
60
61
    public function isSuccessful()
62
    {
63
        return $this->successful;
64
    }
65
66
    public function getMessage()
67
    {
68
        return $this->message;
69
    }
70
71
    public function getCode()
72
    {
73
        return $this->code;
0 ignored issues
show
Bug Best Practice introduced by
The property code does not exist on ByTIC\Payments\Stripe\Me...ompletePurchaseResponse. Did you maybe forget to declare it?
Loading history...
74
    }
75
76
77
    /** @noinspection PhpMissingParentCallCommonInspection
78
     * @return bool
79
     */
80
    protected function canProcessModel()
81
    {
82
        return true;
83
    }
84
85
    /**
86
     * @return []
0 ignored issues
show
Documentation Bug introduced by
The doc comment [] at position 0 could not be parsed: Unknown type name '[' at position 0 in [].
Loading history...
87
     */
88
    public function getSessionDebug(): array
89
    {
90
        $notification = $this->getDataProperty('notification');
91
        return [
92
            'session' => $notification['session']->toArray(),
93
            'paymentIntent' => $notification['paymentIntent']->toArray()
94
        ];
95
    }
96
}
97