Passed
Pull Request — main (#4)
by
unknown
09:56
created

PurchaseRequest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 17
eloc 45
c 4
b 0
f 0
dl 0
loc 90
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 3 1
A sendData() 0 17 2
F getData() 0 64 14
1
<?php
2
3
namespace Omnipay\WindcaveHpp\Message;
4
5
use Omnipay\Common\Exception\InvalidRequestException;
6
7
/**
8
 * Windcave HPP Purchase Request
9
 */
10
class PurchaseRequest extends BaseRequest
11
{
12
    public function initialize(array $parameters = [])
13
    {
14
        return parent::initialize($parameters);
15
    }
16
17
    public function getData()
18
    {
19
        $this->validate('apiUsername', 'apiKey', 'amount', 'currency');
20
21
        $data = [];
22
23
        $data['type'] = $this->getType();
24
        $data['amount'] = $this->getAmount();
25
        $data['currency'] = $this->getCurrency();
26
        $data['callbackUrls'] = [];
27
28
        if ( $this->getStoreCard() ) {
29
            $data['storeCard'] = true;
30
        }
31
32
        if ( $this->getStoredCardIndicator() ) {
33
            $data['storedCardIndicator'] = $this->getStoredCardIndicator();
34
        }
35
36
        if ( $this->getRecurringExpiry() ) {
37
            $data['recurringExpiry'] = $this->getRecurringExpiry();
38
        }
39
40
        if ( $this->getRecurringFrequency() ) {
41
            $data['recurringFrequency'] = $this->getRecurringFrequency();
42
        }
43
44
        if ( $this->getToken() ) {
45
            $data['cardId'] = $this->getToken();
46
        }
47
48
        if ( is_array($this->getPaymentMethods()) ) {
49
            $data['methods'] = $this->getPaymentMethods();
50
        }
51
52
        if ( is_array($this->getCardTypes()) ) {
53
            $data['cardTypes'] = $this->getCardTypes();
54
        }
55
56
        if ( is_array($this->getMetadata()) ) {
57
            $data['metaData'] = $this->getMetadata();
58
        }
59
60
        if ( $this->getMerchantReference() ) {
61
            $data['merchantReference'] = $this->getMerchantReference();
62
        }
63
64
        if ( $this->getReturnUrl() ) {
65
            $data['callbackUrls']['approved'] = $this->getReturnUrl();
66
        }
67
68
        if ( $this->getDeclineUrl() ) {
69
            $data['callbackUrls']['declined'] = $this->getDeclineUrl();
70
        }
71
72
        if ( $this->getCancelUrl() ) {
73
            $data['callbackUrls']['cancelled'] = $this->getCancelUrl();
74
        }
75
76
        if ( $this->getNotifyUrl() ) {
77
            $data['notificationUrl'] = $this->getNotifyUrl();
78
        }
79
80
        return $data;
81
    }
82
83
    public function sendData($data)
84
    {
85
        $headers = [
86
            'Accept'        => 'application/json',
87
            'Content-Type'  => 'application/json',
88
            'Authorization' => 'Basic ' . $this->getAuthorization(),
89
        ];
90
91
        $httpResponse = $this->httpClient->request('POST', $this->getEndpoint('sessions'), $headers, json_encode($data));
92
93
        try {
94
            $responseData = json_decode($httpResponse->getBody()->getContents());
95
        } catch (\Exception $exception) {
96
            $responseData = [];
97
        }
98
99
        return $this->response = new PurchaseResponse($this, $responseData ?? []);
100
    }
101
}