Skip to content

Commit 1fa282a

Browse files
committed
Issue #112 Recode Sage Pay Form data from UTF-8 to ISO8859-1 by default.
Provide an option to disable this for when the merchant site handles it already. In the vast number of cases, the conversion should stand. That is, until Sage Pay get their ISO8859 technical debt sorted out for good.
1 parent 471a4fb commit 1fa282a

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,15 @@ $gateway = OmniPay::create('SagePay\Form')->initialize([
622622

623623
The `encryptionKey` is generated in "My Sage Pay" when logged in as the administrator.
624624

625+
Note that this gateway will assume all inout data (names, addresses etc.)
626+
are UTF-8 encoded.
627+
It will then recode the data to ISO8859-1 before encrypting it for the gateway,
628+
as the gateway strictly accepts ISO8859-1 only, regardless of what encoding is
629+
used to submit the form from the merchant site.
630+
If you do not want this conversion to happen, it can be disabled with this parameter:
631+
632+
'disableUtf8Decode' => true,
633+
625634
The authorize must be given a `returnUrl` (the return URL on success, or on failure
626635
if no separate `failureUrl` is provided).
627636

@@ -639,10 +648,11 @@ At the gateway, the user will authenticate or authorise their credit card,
639648
perform any 3D Secure actions that may be requested, then will return to the
640649
merchant site.
641650

642-
To get the result, the transaction is "completed":
651+
To get the result details, the transaction is "completed":
643652

644653
```php
645-
// The result will in read and decrypted from the return URL query parameters:
654+
// The result will be read and decrypted from the return URL (or failure URL)
655+
// query parameters:
646656

647657
$result = $gateway->completeAuthorize()->send();
648658

@@ -651,6 +661,14 @@ $result->getTransactionReference();
651661
// etc.
652662
```
653663

664+
If you already have the encrypted response string, then it can be optionally
665+
passed in:
666+
667+
$result = $gateway->completeAuthorize(['crypt' => $crypt])->send();
668+
669+
This should normally not be necessary, but is handy for testing or if the
670+
current page query parameters are not available in a particular architecture.
671+
654672
### Form Purchase
655673

656674
This is the same as `authorize()`, but the `purchase()` request is used instead,

src/Message/Form/AuthorizeRequest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,26 @@ public function generateCrypt(array $data)
189189

190190
// Build the data in a query string.
191191

192-
// CHECKME: what happens with UTF-8 data? Do we need to convert
193-
// any special characters not in the correct ranges?
194-
// What about options for URL encoding of other characters?
192+
// The encrypted data MUST be ISO8859-1 regardless of what encoding
193+
// is used to submit the form, because that is how the gateway treats
194+
// the data internally.
195+
// This package assumes input data will be UTF-8 by default, and will
196+
// comvert it accordingly. This can be disabled if the data is already
197+
// ISO8859-1.
198+
// For the Server and Direct gateway methods, the POST encoding type
199+
// will tell the gateway how to interpret the character encoding, and
200+
// the gateway will do any encoding conversions necessary.
195201

196202
// We cannot use http_build_query() because the gateway does
197203
// not decode the string as any standard encoded query string.
198204
// We just join the names and values with "=" and "&" and the
199205
// gateway somehow decodes ambiguous strings.
200206

207+
$disableUtf8Decode = (bool)$this->getDisableUtf8Decode();
208+
201209
$query = [];
202210
foreach ($data as $name => $value) {
203-
$query[] = $name . '=' . $value;
211+
$query[] = $name . '=' . ($disableUtf8Decode ? $value : utf8_decode($value));
204212
}
205213
$query = implode('&', $query);
206214

src/Traits/GatewayParamsTrait.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,24 @@ public function setBillingForShipping($value)
237237
{
238238
return $this->setParameter('billingForShipping', $value);
239239
}
240+
241+
/**
242+
* @return mixed
243+
*/
244+
public function getDisableUtf8Decode()
245+
{
246+
return $this->getParameter('disableUtf8Decode');
247+
}
248+
249+
/**
250+
* The Form API will convert all input data from an assumed UTF-8
251+
* encoding to ISO8859-1 by default, unless disabled here.
252+
*
253+
* @param mixed $value Will be evaluated as boolean.
254+
* @return $this
255+
*/
256+
public function setDisableUtf8Decode($value)
257+
{
258+
return $this->setParameter('disableUtf8Decode', $value);
259+
}
240260
}

0 commit comments

Comments
 (0)