1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnipay\FirstAtlanticCommerce\Message; |
4
|
|
|
|
5
|
|
|
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest; |
6
|
|
|
use Omnipay\FirstAtlanticCommerce\CreditCard; |
7
|
|
|
use Omnipay\FirstAtlanticCommerce\ParameterTrait; |
8
|
|
|
use SimpleXMLElement; |
9
|
|
|
|
10
|
|
|
abstract class AbstractRequest extends BaseAbstractRequest |
11
|
|
|
{ |
12
|
|
|
use ParameterTrait; |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
|
|
|
|
15
|
|
|
* FACPG2 live endpoint URL |
|
|
|
|
16
|
|
|
* |
|
|
|
|
17
|
|
|
* @var string |
|
|
|
|
18
|
|
|
*/ |
|
|
|
|
19
|
|
|
protected $liveEndpoint = 'https://marlin.firstatlanticcommerce.com/PGServiceXML/'; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
|
|
|
|
22
|
|
|
* FACPG2 test endpoint URL |
|
|
|
|
23
|
|
|
* |
|
|
|
|
24
|
|
|
* @var string |
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
protected $testEndpoint = 'https://ecm.firstatlanticcommerce.com/PGServiceXML/'; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* FACPG2 XML namespace |
|
|
|
|
30
|
|
|
* |
|
|
|
|
31
|
|
|
* @var string |
|
|
|
|
32
|
|
|
*/ |
|
|
|
|
33
|
|
|
protected $namespace = 'http://schemas.firstatlanticcommerce.com/gateway/data'; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* FACPG2 XML root |
|
|
|
|
37
|
|
|
* |
|
|
|
|
38
|
|
|
* @var string |
|
|
|
|
39
|
|
|
*/ |
|
|
|
|
40
|
|
|
protected $requestName; |
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* Returns the amount formatted to match FAC's expectations. |
|
|
|
|
44
|
|
|
* |
|
|
|
|
45
|
|
|
* @return string The amount padded with zeros on the left to 12 digits and no decimal place. |
|
|
|
|
46
|
|
|
*/ |
|
|
|
|
47
|
|
|
protected function formatAmount() |
|
|
|
|
48
|
|
|
{ |
|
|
|
|
49
|
|
|
$amount = $this->getAmount(); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$amount = str_replace('.', '', $amount); |
|
|
|
|
52
|
|
|
$amount = str_pad($amount, 12, '0', STR_PAD_LEFT); |
|
|
|
|
53
|
|
|
|
54
|
|
|
return $amount; |
|
|
|
|
55
|
|
|
} |
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
|
|
|
|
58
|
|
|
* Returns the live or test endpoint depending on TestMode. |
|
|
|
|
59
|
|
|
* |
|
|
|
|
60
|
|
|
* @return string Endpoint URL |
|
|
|
|
61
|
|
|
*/ |
|
|
|
|
62
|
|
|
protected function getEndpoint() |
|
|
|
|
63
|
|
|
{ |
|
|
|
|
64
|
|
|
return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint; |
|
|
|
|
65
|
|
|
} |
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
|
|
|
|
68
|
|
|
* Return the response object |
|
|
|
|
69
|
|
|
* |
|
|
|
|
70
|
|
|
* @param \SimpleXMLElement $xml Response xml object |
|
|
|
|
71
|
|
|
* |
|
|
|
|
72
|
|
|
* @return ResponseInterface |
|
|
|
|
73
|
|
|
*/ |
|
|
|
|
74
|
|
|
abstract protected function newResponse($xml); |
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
|
|
|
|
77
|
|
|
* Send the request payload |
|
|
|
|
78
|
|
|
* |
|
|
|
|
79
|
|
|
* @param array $data Request payload |
|
|
|
|
80
|
|
|
* |
|
|
|
|
81
|
|
|
* @return ResponseInterface |
|
|
|
|
82
|
|
|
*/ |
|
|
|
|
83
|
|
|
public function sendData($data) |
|
|
|
|
84
|
|
|
{ |
|
|
|
|
85
|
|
|
$httpResponse = $this->httpClient->post( |
|
|
|
|
86
|
|
|
$this->getEndpoint(), |
|
|
|
|
87
|
|
|
['Content-Type' => 'text/xml; charset=utf-8'], |
|
|
|
|
88
|
|
|
$this->xmlSerialize($data) |
|
|
|
|
89
|
|
|
)->send(); |
|
|
|
|
90
|
|
|
|
91
|
|
|
return $this->response = $this->newResponse( $httpResponse->xml() ); |
|
|
|
|
92
|
|
|
} |
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
|
|
|
|
95
|
|
|
* Serializes an array into XML |
|
|
|
|
96
|
|
|
* |
|
|
|
|
97
|
|
|
* @param array $data Array to serialize into XML. |
|
|
|
|
98
|
|
|
* @param \SimpleXMLElement $xml SimpleXMLElement object. |
|
|
|
|
99
|
|
|
* |
|
|
|
|
100
|
|
|
* @return string XML |
|
|
|
|
101
|
|
|
*/ |
|
|
|
|
102
|
|
|
protected function xmlSerialize($data, $xml = null) |
|
|
|
|
103
|
|
|
{ |
|
|
|
|
104
|
|
|
if ( !$xml instanceof SimpleXMLElement ) |
|
|
|
|
105
|
|
|
{ |
|
|
|
|
106
|
|
|
$xml = new SimpleXMLElement('<'. $this->requestName .' xmlns="'. $this->namespace .'" />'); |
|
|
|
|
107
|
|
|
} |
|
|
|
|
108
|
|
|
|
109
|
|
|
foreach ($data as $key => $value) |
|
|
|
|
110
|
|
|
{ |
|
|
|
|
111
|
|
|
if ( !isset($value) ) |
|
|
|
|
112
|
|
|
{ |
|
|
|
|
113
|
|
|
continue; |
|
|
|
|
114
|
|
|
} |
|
|
|
|
115
|
|
|
|
116
|
|
|
if ( is_array($value) ) |
|
|
|
|
117
|
|
|
{ |
|
|
|
|
118
|
|
|
$node = $xml->addChild($key); |
|
|
|
|
119
|
|
|
$this->xmlSerialize($value, $node); |
|
|
|
|
120
|
|
|
} |
|
|
|
|
121
|
|
|
else |
|
|
|
|
122
|
|
|
{ |
|
|
|
|
123
|
|
|
$xml->addChild($key, $value); |
|
|
|
|
124
|
|
|
} |
|
|
|
|
125
|
|
|
} |
|
|
|
|
126
|
|
|
|
127
|
|
|
return $xml->asXML(); |
|
|
|
|
128
|
|
|
} |
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
|
|
|
|
131
|
|
|
* Sets the card. |
|
|
|
|
132
|
|
|
* |
|
|
|
|
133
|
|
|
* @param CreditCard $value |
|
|
|
|
134
|
|
|
* |
|
|
|
|
135
|
|
|
* @return AbstractRequest Provides a fluent interface |
|
|
|
|
136
|
|
|
*/ |
|
|
|
|
137
|
|
|
public function setCard($value) |
|
|
|
|
138
|
|
|
{ |
|
|
|
|
139
|
|
|
if ($value && !$value instanceof CreditCard) |
|
|
|
|
140
|
|
|
{ |
|
|
|
|
141
|
|
|
$value = new CreditCard($value); |
|
|
|
|
142
|
|
|
} |
|
|
|
|
143
|
|
|
|
144
|
|
|
return $this->setParameter('card', $value); |
|
|
|
|
145
|
|
|
} |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|