Skip to content

Commit 1c0358b

Browse files
committed
Support token parameter for providing opaque data.
1 parent 7be1f84 commit 1c0358b

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ and for creating transactions using a card token.
3939
## API Authorize/Purchase (Credit Card)
4040

4141
The following example is a simple authorize with supplied card details.
42-
You would normally avoid allowing card details near your merchanet site
42+
You would normally avoid allowing card details near your merchant site
4343
back end for PCI compliance reasons,
4444
supplying a tokenised card reference instead (see later section for this).
4545

@@ -236,13 +236,22 @@ or
236236
$request->setOpaqueData($opaqueDataDescriptor, $opaqueDataValue);
237237
```
238238

239-
The authorizatiob or purchase should then go ahead as though the card
239+
or join with a colon (:) to handle as a card token:
240+
241+
```php
242+
$request->setToken($opaqueDataDescriptor . ':' . $opaqueDataValue);
243+
```
244+
245+
The authorize or purchase should then go ahead as though the card
240246
details were provided directly. In the result, the last four digits
241247
of the card will be made available in case a refund needs to be performed.
242248

243249
Further details can be
244250
[fouund in the officual documentation](https://developer.authorize.net/api/reference/features/acceptjs.html).
245251

252+
Note also that the opaque data is used for other payment sources, such as
253+
bank accounts and PayPal.
254+
246255
## API Void
247256

248257
An authorized transaction can be voided:

src/Message/AuthorizeRequest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929

3030
class AuthorizeRequest extends AbstractRequest
3131
{
32+
/**
33+
* @var string The separator used to join the opaque data
34+
* descriptor and value, when used as a card token.
35+
*/
36+
const CARD_TOKEN_SEPARATOR = ':';
37+
3238
/**
3339
* Return the complete transaction object which will later be wrapped in
3440
* a \Academe\AuthorizeNet\Request\CreateTransaction object.
@@ -309,8 +315,34 @@ public function getOpaqueDataValue()
309315
*/
310316
public function setOpaqueData($descriptor, $value)
311317
{
312-
$this->setOpaqueDataValue($value);
313318
$this->setOpaqueDataDataDescriptor($descriptor);
319+
$this->setOpaqueDataValue($value);
320+
314321
return $this;
315322
}
323+
324+
/**
325+
* The opaque data comes in two parts, but Omnipay uses just
326+
* one parameter for a card token.
327+
* Join the descriptor and the value with a colon.
328+
*/
329+
public function setToken($value)
330+
{
331+
list($opaqueDataDescriptor, $opaqueDataValue) = explode(static::CARD_TOKEN_SEPARATOR, $value, 2);
332+
333+
$this->setOpaqueDataDescriptor($opaqueDataDescriptor);
334+
$this->setOpaqueDataValue($opaqueDataValue);
335+
336+
return $this;
337+
}
338+
339+
public function getToken()
340+
{
341+
$opaqueDataDescriptor = $this->getOpaqueDataDescriptor();
342+
$opaqueDataValue = $this->getOpaqueDataValue();
343+
344+
if ($opaqueDataDescriptor && $opaqueDataValue) {
345+
return $opaqueDataDescriptor . static::CARD_TOKEN_SEPARATOR . $opaqueDataValue;
346+
}
347+
}
316348
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Omnipay\AuthorizeNetApi;
4+
5+
use Omnipay\Tests\TestCase;
6+
7+
class AuthorizeRequestTest extends TestCase
8+
{
9+
public function setUp()
10+
{
11+
parent::setUp();
12+
13+
$this->gateway = new ApiGateway(
14+
$this->getHttpClient(),
15+
$this->getHttpRequest()
16+
);
17+
}
18+
19+
public function testOpaqueData()
20+
{
21+
$opaqueDescriptor = 'COMMON.ACCEPT.INAPP.PAYMENT';
22+
$opaqueValue = str_shuffle(str_repeat('1234567890ABCDEFGHIJ', 10));
23+
24+
$cardToken = $opaqueDescriptor . ':' . $opaqueValue;
25+
26+
$request = $this->gateway->authorize([
27+
'opaqueDataDescriptor' => $opaqueDescriptor,
28+
'opaqueDataValue' => $opaqueValue,
29+
]);
30+
31+
$this->assertSame($cardToken, $request->getToken());
32+
33+
$this->assertSame($opaqueDescriptor, $request->getOpaqueDataDescriptor());
34+
$this->assertSame($opaqueValue, $request->getOpaqueDataValue());
35+
}
36+
37+
public function testCardToken()
38+
{
39+
$opaqueDescriptor = 'COMMON.ACCEPT.INAPP.PAYMENT';
40+
$opaqueValue = str_shuffle(str_repeat('1234567890ABCDEFGHIJ', 10));
41+
42+
$cardToken = $opaqueDescriptor . ':' . $opaqueValue;
43+
44+
$request = $this->gateway->authorize([
45+
'token' => $cardToken,
46+
]);
47+
48+
$this->assertSame($cardToken, $request->getToken());
49+
50+
$this->assertSame($opaqueDescriptor, $request->getOpaqueDataDescriptor());
51+
$this->assertSame($opaqueValue, $request->getOpaqueDataValue());
52+
}
53+
}

0 commit comments

Comments
 (0)