Completed
Branch webservice-support (1bfa77)
by John
01:47
created

WebservicePurchaseRequest::sendData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 48
ccs 0
cts 27
cp 0
rs 9.125
c 1
b 0
f 0
cc 1
eloc 15
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Omnipay\Redsys\Message;
4
5
use SimpleXMLElement;
6
7
/**
8
 * Redsys Webservice Purchase Request
9
 */
10
class WebservicePurchaseRequest extends PurchaseRequest
11
{
12
    /** @var string */
13
    protected $liveWsdl = __DIR__ . "/redsys-webservice-live.wsdl";
14
    /** @var string */
15
    protected $testWsdl = __DIR__ . "/redsys-webservice-test.wsdl";
16
    /** @var string */
17
    protected $liveEndpoint = "https://sis.redsys.es/sis/services/SerClsWSEntrada";
18
    /** @var string */
19
    protected $testEndpoint = "https://sis-t.redsys.es:25443/sis/services/SerClsWSEntrada";
20
 
21
    public function getData()
22
    {
23
        $this->validate('merchantId', 'terminalId', 'amount', 'currency', 'card');
24
        $card = $this->getCard();
25
        // @todo given test card doesn't validate?
26
        if (!$this->getTestMode()) {
27
            $card->validate();
28
        }
29
30
        $data = array(
31
            'DS_MERCHANT_AMOUNT'          => $this->getAmountInteger(),
32
            'DS_MERCHANT_ORDER'           => $this->getTransactionId(),
33
            'DS_MERCHANT_MERCHANTCODE'    => $this->getMerchantId(),
34
            'DS_MERCHANT_CURRENCY'        => $this->getCurrencyNumeric(),  // uses ISO-4217 codes
35
            'DS_MERCHANT_PAN'             => $card->getNumber(),
36
            'DS_MERCHANT_CVV2'            => $card->getCvv(),
37
            'DS_MERCHANT_TRANSACTIONTYPE' => 'A',                          // 'Traditional payment'
38
            'DS_MERCHANT_TERMINAL'        => $this->getTerminalId(),
39
            'DS_MERCHANT_EXPIRYDATE'      => $card->getExpiryDate('ym'),
40
        );
41
42
43
        $request = new SimpleXMLElement('<REQUEST/>');
44
        $requestData = $request->addChild('DATOSENTRADA');
45
        foreach ($data as $tag => $value) {
46
            $requestData->addChild($tag, $value);
47
        }
48
49
        $security = new Security;
50
51
        $request->addChild('DS_SIGNATUREVERSION', Security::VERSION);
52
        $request->addChild('DS_SIGNATURE', $security->createSignature(
53
            $requestData->asXML(),
54
            $data['DS_MERCHANT_ORDER'],
55
            base64_decode($this->getHmacKey())
56
        ));
57
58
        return $request;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $request; (SimpleXMLElement) is incompatible with the return type of the parent method Omnipay\Redsys\Message\PurchaseRequest::getData of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
59
        // array(
60
        //     'DATOSENTRADA'        => $data,
61
        //     'DS_SIGNATUREVERSION' => $request->DS_SIGNATUREVERSION,
62
        //     'DS_SIGNATURE'        => $request->DS_SIGNATURE,
63
        // );
64
    }
65
66
    public function sendData($data)
67
    {
68
69
        // @todo either use SOAP client here, or wrap in soap yourself and just guzzle it
70
        /*
71
            use SoapClient;
72
            $s = new SoapClient($fs, array('trace' => $this->debug, 'exceptions' => true));
73
            $result = $s->trataPeticion(
74
                // $data   as nested array rather than XML?
75
            );
76
77
        */
78
79
        // DIY SOAP WRAPPER
80
        $envelope = "<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
                    ".htmlspecialchars($data->asXML())."
86
                  </impl:datosEntrada>
87
                </impl:trataPeticion>
88
              </soapenv:Body>
89
            </soapenv:Envelope>";
90
91
        $httpResponse = $this->httpClient->post(
92
            $this->getEndpoint(),
93
            array('SOAPAction' => 'trataPeticion'),
94
            $envelope
95
        )->send();
96
97
        // unwrap httpResponse into actual data as SimpleXMLElement tree
98
        $envelope = $httpResponse->xml();
99
        $response_data = new SimpleXMLElement(htmlspecialchars_decode(
100
            $envelope->children("http://schemas.xmlsoap.org/soap/envelope/")
101
            ->Body->children("http://webservice.sis.sermepa.es")
102
            ->trataPeticionResponse
103
            ->trataPeticionReturn
104
        ));
105
106
        // remove any reflected request data (this happens on error, including card number)
107
        // if (isset($response_data->RECIBIDO)) {
108
        //     unset($response_data->RECIBIDO);
109
        // }
110
111
112
        return $this->response = new WebservicePurchaseResponse($this, $response_data);
113
    }
114
}
115