Skip to content

Commit

Permalink
transactions: add reference key
Browse files Browse the repository at this point in the history
Add the `reference_key` attribute `AbstractTransactions`. That will be
usefull because this attribute avoid transactions duplication
accidentally. This attribute should have an unique value and has 20
caracters at least.
  • Loading branch information
leonampd committed May 22, 2018
1 parent d9436c1 commit def2fb3
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 31 deletions.
10 changes: 10 additions & 0 deletions lib/Transaction/AbstractTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ abstract class AbstractTransaction
*/
protected $id;

/**
* @var string
*/
protected $referenceKey;

/**
* @var string
*/
Expand Down Expand Up @@ -174,6 +179,11 @@ public function getId()
return $this->id;
}

public function getReferenceKey()
{
return $this->referenceKey;
}

/**
* @return string
* @codeCoverageIgnore
Expand Down
3 changes: 2 additions & 1 deletion lib/Transaction/Request/TransactionCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function getPayload()
'amount' => $this->transaction->getAmount(),
'payment_method' => $this->transaction->getPaymentMethod(),
'postback_url' => $this->transaction->getPostbackUrl(),
'metadata' => $this->transaction->getMetadata()
'metadata' => $this->transaction->getMetadata(),
'reference_key' => $this->transaction->getReferenceKey()
];

$customerData = [
Expand Down
80 changes: 69 additions & 11 deletions tests/unit/Transaction/Request/BoletoTransactionCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class BoletoTransactionCreateTest extends \PHPUnit_Framework_TestCase
{
use FakeReferenceKey;

const PATH = 'transactions';

const CARD_ID = 1;
Expand All @@ -20,9 +22,9 @@ public function boletoOptions()
$customer = $this->getCustomerMock();

return [
[null],
[date('Y-m-d', strtotime("tomorrow"))],
[date('Y-m-d', strtotime("+15 days"))]
[null, null],
[date('Y-m-d', strtotime("tomorrow")), $this->getFakeReferenceKey()],
[date('Y-m-d', strtotime("+15 days")), null]
];
}

Expand Down Expand Up @@ -63,7 +65,55 @@ public function mustPayloadBeCorrect($expirationDate)
'metadata' => null,
'async' => null,
'boleto_instructions' => null,
'soft_descriptor' => null
'soft_descriptor' => null,
'reference_key' => null
],
$transactionCreate->getPayload()
);
}

/**
* @test
* @dataProvider boletoOptions
*/
public function mustContainsTheRightReferenceKey(
$expirationDate,
$referenceKey
)
{
$transaction = $this->createTransaction($expirationDate);
$transactionCreate = new BoletoTransactionCreate($transaction);

$this->assertEquals(
[
'amount' => 1337,
'payment_method' => 'boleto',
'postback_url' => 'example.com/postback',
'boleto_expiration_date' => $expirationDate,
'customer' => [
'name' => 'Eduardo Nascimento',
'born_at' => '15071991',
'document_number' => '10586649727',
'email' => '[email protected]',
'sex' => 'M',
'address' => [
'street' => 'rua teste',
'street_number' => 42,
'neighborhood' => 'centro',
'zipcode' => '01227200',
'complementary' => null
],
'phone' => [
'ddi' => 55,
'ddd' => 15,
'number' => 987523421
]
],
'metadata' => null,
'async' => null,
'boleto_instructions' => null,
'soft_descriptor' => null,
'reference_key' => null
],
$transactionCreate->getPayload()
);
Expand Down Expand Up @@ -98,7 +148,8 @@ public function mustPayloadContainSplitRule()
'postback_url' => 'example.com/postback',
'customer' => $customerMock,
'boleto_expiration_date' => $expirationDate,
'split_rules' => $rules
'split_rules' => $rules,
'referenceKey' => null
]
);

Expand Down Expand Up @@ -151,7 +202,8 @@ public function mustPayloadContainSplitRule()
'metadata' => null,
'async' => null,
'boleto_instructions' => null,
'soft_descriptor' => null
'soft_descriptor' => null,
'reference_key' => null
],
$transactionCreate->getPayload()
);
Expand Down Expand Up @@ -185,16 +237,20 @@ public function mustMethodBeCorrect()
$this->assertEquals(RequestInterface::HTTP_POST, $transactionCreate->getMethod());
}

private function createTransaction($expirationDate)
private function createTransaction(
$expirationDate,
$referenceKey = null
)
{
$customerMock = $this->getCustomerMock();

$transaction = new BoletoTransaction(
[
'amount' => 1337,
'postback_url' => 'example.com/postback',
'postback_url' => 'example.com/postback',
'customer' => $customerMock,
'boleto_expiration_date' => $expirationDate
'boleto_expiration_date' => $expirationDate,
'reference_key' => $referenceKey
]
);

Expand Down Expand Up @@ -244,7 +300,8 @@ public function mustPayloadContainSoftDescriptor()
'amount' => 1338,
'postback_url' => 'example.com/postback',
'customer' => $customerMock,
'soft_descriptor' => "Minha loja"
'soft_descriptor' => "Minha loja",
'referenceKey' => null
]
);

Expand Down Expand Up @@ -280,7 +337,8 @@ public function mustPayloadContainSoftDescriptor()
'metadata' => null,
'async' => null,
'boleto_instructions' => null,
'soft_descriptor' => 'Minha loja'
'soft_descriptor' => 'Minha loja',
'reference_key' => null
],
$transactionCreate->getPayload()
);
Expand Down
110 changes: 91 additions & 19 deletions tests/unit/Transaction/Request/CreditCardTransactionCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@

class CreditCardTransactionCreateTest extends \PHPUnit_Framework_TestCase
{
use FakeReferenceKey;

const PATH = 'transactions';

const CARD_ID = 1;
const CARD_HASH = 'FC1mH7XLFU5fjPAzDsP0ogeAQh3qXRpHzkIrgDz64lITBUGwio67zm';

public function referenceKeyProvider()
{
return [
[1, true, null, null],
[1, true, null, $this->getFakeReferenceKey()],
];
}

public function installmentsProvider()
{
return [
Expand Down Expand Up @@ -76,7 +86,8 @@ public function mustPayloadBeCorrect(
],
'metadata' => null,
'soft_descriptor' => $softDescriptor,
'async' => $async
'async' => $async,
'reference_key' => null
],
$transactionCreate->getPayload()
);
Expand All @@ -101,7 +112,8 @@ public function mustNotContainAdressAndPhoneDataOnPayload(
'customer' => $customerMock,
'installments' => $installments,
'capture' => $capture,
'postbackUrl' => $postbackUrl
'postbackUrl' => $postbackUrl,
'referenceKey' => null
]
);

Expand All @@ -124,7 +136,59 @@ public function mustNotContainAdressAndPhoneDataOnPayload(
],
'metadata' => null,
'soft_descriptor' => null,
'async' => null
'async' => null,
'reference_key' => null
],
$transactionCreate->getPayload()
);
}

/**
* @dataProvider referenceKeyProvider
* @test
*/
public function mustContainsTheRightReferenceKey(
$installments,
$capture,
$postbackUrl,
$referenceKey
) {
$customerMock = $this->getBlankCustomerMock();
$cardMock = $this->getCardMock();

$transaction = new CreditCardTransaction(
[
'amount' => 1337,
'card' => $cardMock,
'customer' => $customerMock,
'installments' => $installments,
'capture' => $capture,
'postbackUrl' => $postbackUrl,
'referenceKey' => $referenceKey
]
);

$transactionCreate = new CreditCardTransactionCreate($transaction);

$this->assertEquals(
[
'amount' => 1337,
'card_id' => self::CARD_ID,
'installments' => $installments,
'payment_method' => 'credit_card',
'capture' => $capture,
'postback_url' => $postbackUrl,
'customer' => [
'name' => null,
'born_at' => null,
'document_number' => null,
'email' => null,
'sex' => null
],
'metadata' => null,
'soft_descriptor' => null,
'async' => null,
'reference_key' => $referenceKey
],
$transactionCreate->getPayload()
);
Expand All @@ -147,7 +211,8 @@ public function mustPayloadContainCustomerId()
'customer' => $customer,
'installments' => 1,
'capture' => false,
'postback_url' => null
'postback_url' => null,
'referenceKey' => null
]
);

Expand All @@ -171,8 +236,8 @@ public function mustPayloadContainCustomerId()
],
'metadata' => null,
'soft_descriptor' => null,
'async' => null

'async' => null,
'reference_key' => null
],
$transactionCreate->getPayload()
);
Expand Down Expand Up @@ -208,7 +273,8 @@ public function mustPayloadContainMonetarySplitRules()
'installments' => 1,
'capture' => false,
'postback_url' => null,
'split_rules' => $rules
'split_rules' => $rules,
'referenceKey' => null
]
);

Expand Down Expand Up @@ -257,7 +323,8 @@ public function mustPayloadContainMonetarySplitRules()
],
'metadata' => null,
'soft_descriptor' => null,
'async' => null
'async' => null,
'reference_key' => null
],
$transactionCreate->getPayload()
);
Expand Down Expand Up @@ -287,13 +354,14 @@ public function mustPayloadContainPercentageSplitRules()

$transaction = new CreditCardTransaction(
[
'amount' => 1337,
'card' => $cardMock,
'customer' => $customerMock,
'installments' => 1,
'capture' => false,
'postback_url' => null,
'split_rules' => $rules
'amount' => 1337,
'card' => $cardMock,
'customer' => $customerMock,
'installments' => 1,
'capture' => false,
'postback_url' => null,
'split_rules' => $rules,
'reference_key' => null
]
);

Expand Down Expand Up @@ -342,7 +410,8 @@ public function mustPayloadContainPercentageSplitRules()
],
'metadata' => null,
'soft_descriptor' => null,
'async' => null
'async' => null,
'reference_key' => null
],
$transactionCreate->getPayload()
);
Expand Down Expand Up @@ -397,7 +466,8 @@ public function mustPayloadContainCardHash($installments, $capture, $postbackUrl
'customer' => $customerMock,
'installments' => $installments,
'capture' => $capture,
'postbackUrl' => $postbackUrl
'postbackUrl' => $postbackUrl,
'referenceKey' => null
]
);

Expand Down Expand Up @@ -432,7 +502,8 @@ public function mustPayloadContainCardHash($installments, $capture, $postbackUrl
],
'metadata' => null,
'soft_descriptor' => null,
'async' => null
'async' => null,
'reference_key' => null
],
$transactionCreate->getPayload()
);
Expand Down Expand Up @@ -493,7 +564,7 @@ private function getTransaction(
'capture' => $capture,
'postbackUrl' => $postbackUrl,
'softDescriptor' => $softDescriptor,
'async' => $async
'async' => $async,
]
);

Expand Down Expand Up @@ -559,4 +630,5 @@ public function getBlankCustomerMock()

return $customerMock;
}

}
Loading

0 comments on commit def2fb3

Please sign in to comment.