WebservicePurchaseRequest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 107
ccs 62
cts 62
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sendData() 0 49 3
A getData() 0 46 3
1
<?php
2
3
namespace Omnipay\Redsys\Message;
4
5
use SimpleXMLElement;
6
7
/**
8
 * Redsys Webservice Purchase Request.
9
 */
10
class WebservicePurchaseRequest extends AbstractRequest
11
{
12
    use WebserviceTrait;
13
14 5
    public function getData()
15
    {
16 5
        $this->validate('merchantId', 'terminalId', 'amount', 'currency', 'card');
17
18 5
        $card = $this->getCard();
19
        // test cards aparently don't validate
20 5
        if (!$this->getTestMode()) {
21 1
            $card->validate();
22 1
        }
23
24
        $data = [
25 5
            'DS_MERCHANT_AMOUNT' => $this->getAmountInteger(),
26 5
            'DS_MERCHANT_ORDER' => $this->getTransactionId(),
27 5
            'DS_MERCHANT_MERCHANTCODE' => $this->getMerchantId(),
28 5
            'DS_MERCHANT_CURRENCY' => $this->getCurrencyNumeric(),  // uses ISO-4217 codes
29 5
            'DS_MERCHANT_PAN' => $card->getNumber(),
30 5
            'DS_MERCHANT_CVV2' => $card->getCvv(),
31 5
            'DS_MERCHANT_TRANSACTIONTYPE' => 'A',                          // 'Traditional payment'
32 5
            'DS_MERCHANT_TERMINAL' => $this->getTerminalId(),
33 5
            'DS_MERCHANT_EXPIRYDATE' => $card->getExpiryDate('ym'),
34
            // undocumented fields
35 5
            'DS_MERCHANT_MERCHANTDATA' => $this->getMerchantData(),
36 5
            'DS_MERCHANT_MERCHANTNAME' => $this->getMerchantName(),
37 5
            'DS_MERCHANT_CONSUMERLANGUAGE' => $this->getConsumerLanguage(),
38 5
        ];
39
40 5
        $request = new SimpleXMLElement('<REQUEST/>');
41 5
        $requestData = $request->addChild('DATOSENTRADA');
42 5
        foreach ($data as $tag => $value) {
43 5
            $requestData->addChild($tag, $value);
44 5
        }
45
46 5
        $security = new Security();
47
48 5
        $request->addChild('DS_SIGNATUREVERSION', Security::VERSION);
49 5
        $request->addChild('DS_SIGNATURE', $security->createSignature(
50 5
            $requestData->asXML(),
0 ignored issues
show
Bug introduced by
It seems like $requestData->asXML() can also be of type true; however, parameter $message of Omnipay\Redsys\Message\Security::createSignature() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            /** @scrutinizer ignore-type */ $requestData->asXML(),
Loading history...
51 5
            $data['DS_MERCHANT_ORDER'],
52 5
            $this->getHmacKey()
53 5
        ));
54
55
        // keep data as nested array for method signature compatibility
56
        return [
57 5
            'DATOSENTRADA' => $data,
58 5
            'DS_SIGNATUREVERSION' => (string) $request->DS_SIGNATUREVERSION,
59 5
            'DS_SIGNATURE' => (string) $request->DS_SIGNATURE,
60 5
        ];
61
    }
62
63
    /**
64
     * Send the data.
65
     *
66
     * Uses its own SOAP wrapper instead of PHP's SoapClient
67
     */
68 4
    public function sendData($data)
69
    {
70
        // re-create the XML
71 4
        $request = new SimpleXMLElement('<REQUEST/>');
72 4
        $requestData = $request->addChild('DATOSENTRADA');
73 4
        foreach ($data['DATOSENTRADA'] as $tag => $value) {
74 4
            $requestData->addChild($tag, $value);
75 4
        }
76 4
        $request->addChild('DS_SIGNATUREVERSION', $data['DS_SIGNATUREVERSION']);
77 4
        $request->addChild('DS_SIGNATURE', $data['DS_SIGNATURE']);
78
79
        // wrap in SOAP envelope
80
        $requestEnvelope = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
81
              <soapenv:Header/>
82
              <soapenv:Body>
83
                <impl:trataPeticion xmlns:impl='http://webservice.sis.sermepa.es'>
84
                  <impl:datosEntrada>
85 4
                    ".htmlspecialchars($request->asXML()).'
0 ignored issues
show
Bug introduced by
It seems like $request->asXML() can also be of type true; however, parameter $string of htmlspecialchars() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
                    ".htmlspecialchars(/** @scrutinizer ignore-type */ $request->asXML()).'
Loading history...
86
                  </impl:datosEntrada>
87
                </impl:trataPeticion>
88
              </soapenv:Body>
89 4
            </soapenv:Envelope>';
90
91
        // send the actual SOAP request
92 4
        $httpResponse = $this->httpClient->request(
93 4
            'POST',
94 4
            $this->getEndpoint(),
95 4
            ['SOAPAction' => 'trataPeticion'],
96
            $requestEnvelope
97 4
        );
98
99
        // unwrap httpResponse into actual data as SimpleXMLElement tree
100 4
        $responseEnvelope = simplexml_load_string($httpResponse->getBody()->getContents());
101 4
        $responseData = new SimpleXMLElement(htmlspecialchars_decode(
102 4
            $responseEnvelope->children('http://schemas.xmlsoap.org/soap/envelope/')
103 4
            ->Body->children('http://webservice.sis.sermepa.es')
104 4
            ->trataPeticionResponse
105 4
            ->trataPeticionReturn
106 4
        ));
107
108
        // remove any reflected request data (this happens on SIS errors, and includes card number)
109 4
        if (isset($responseData->RECIBIDO)) {
110 1
            unset($responseData->RECIBIDO);
111 1
        }
112
113
        // convert to nested arrays (drop the 'true' to use simple objects)
114 4
        $responseData = json_decode(json_encode($responseData), true);
115
116 4
        return $this->response = new WebservicePurchaseResponse($this, $responseData);
117
    }
118
}
119