Skip to content

Commit

Permalink
Merge pull request #119 from pagarme/fix/object-manipulation
Browse files Browse the repository at this point in the history
Fix/object manipulation
  • Loading branch information
devdrops committed Feb 9, 2017
2 parents 5da38b9 + bcdf974 commit 56fba66
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 17 deletions.
33 changes: 19 additions & 14 deletions lib/Transaction/Request/TransactionCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
use PagarMe\Sdk\RequestInterface;
use PagarMe\Sdk\Transaction\Transaction;
use PagarMe\Sdk\SplitRule\SplitRuleCollection;
use PagarMe\Sdk\SplitRule\SplitRule;

class TransactionCreate implements RequestInterface
{
/**
* @var Transaction
* @var \PagarMe\Sdk\Transaction\Transaction
*/
protected $transaction;

/**
* @param Transaction $transaction
* @param \PagarMe\Sdk\Transaction\Transaction $transaction
*/
public function __construct(Transaction $transaction)
{
Expand All @@ -29,8 +28,14 @@ public function getPayload()
{
$customer = $this->transaction->getCustomer();

$address = $customer->getAddress();
$phone = $customer->getPhone();
$address = $customer->getAddress();
if (is_array($address)) {
$address = new \PagarMe\Sdk\Customer\Address($address);
}
$phone = $customer->getPhone();
if (is_array($phone)) {
$phone = new \PagarMe\Sdk\Customer\Phone($phone);
}

$transactionData = [
'amount' => $this->transaction->getAmount(),
Expand All @@ -43,15 +48,15 @@ public function getPayload()
'sex' => $customer->getGender(),
'born_at' => $customer->getBornAt(),
'address' => [
'street' => $address['street'],
'street_number' => $address['street_number'],
'complementary' => isset($address['complementary']) ? $address['complementary']: null,
'neighborhood' => $address['neighborhood'],
'zipcode' => $address['zipcode']
'street' => $address->getStreet(),
'street_number' => $address->getStreetNumber(),
'complementary' => $address->getComplementary(),
'neighborhood' => $address->getNeighborhood(),
'zipcode' => $address->getZipcode()
],
'phone' => [
'ddd' => (string) $phone['ddd'],
'number' => (string) $phone['number']
'ddd' => (string) $phone->getDdd(),
'number' => (string) $phone->getNumber()
]
],
'metadata' => $this->transaction->getMetadata()
Expand Down Expand Up @@ -83,7 +88,7 @@ public function getMethod()
}

/**
* @param PagarMe\Sdk\SplitRule\SplitRuleCollection $splitRules
* @param \PagarMe\Sdk\SplitRule\SplitRuleCollection $splitRules
* @return array
*/
private function getSplitRulesInfo(SplitRuleCollection $splitRules)
Expand All @@ -104,7 +109,7 @@ private function getSplitRulesInfo(SplitRuleCollection $splitRules)
}

/**
* @param PagarMe\Sdk\SplitRule\SplitRule $splitRule
* @param \PagarMe\Sdk\SplitRule\SplitRule $splitRule
* @return array
*/
private function getRuleValue($splitRule)
Expand Down
51 changes: 48 additions & 3 deletions tests/acceptance/TransactionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class TransactionContext extends BasicContext
private $creditCard;
private $customer;
private $transaction;
private $transactionList;
private $transactionList = [];
private $events;
private $metadata;

/**
/**
* @When register a card with :number, :holder and :expiration
*/
public function registerACardWithAnd($number, $holder, $expiration)
Expand Down Expand Up @@ -69,7 +69,6 @@ public function makeABoletoTransactionWith($amount)
->boletoTransaction($amount, $this->customer, self::POSTBACK_URL);
}


/**
* @Then a valid transaction must be created
*/
Expand All @@ -81,6 +80,38 @@ public function aValidTransactionMustBeCreated()
);
}

/**
* @Given make a boleto transaction with :amount, using Customers from the API
*/
public function makeABoletoTransactionWithAGivenAmountUsingCustomersFromTheAPI($amount)
{
$customersIdList = $this->getCustomerIdsFromAPI();

foreach($customersIdList as $id) {
/** @var $customer \PagarMe\Sdk\Customer\Customer */
$customer = self::getPagarMe()
->customer()
->get($id);

$this->transactionList[] = self::getPagarMe()
->transaction()
->boletoTransaction($amount, $customer, self::POSTBACK_URL);
}
}

/**
* @Then a list of valid transactions must be created
*/
public function aListOfValidTransactionsMustBeCreated()
{
foreach ($this->transactionList as $transaction) {
assertInstanceOf(
'PagarMe\Sdk\Transaction\AbstractTransaction',
$transaction
);
}
}

/**
* @Then a paid transaction must be created
*/
Expand Down Expand Up @@ -335,4 +366,18 @@ private function getRandomMetadata()
{
$this->metadata = [uniqid('key') => uniqid('value')];
}

private function getCustomerIdsFromAPI()
{
$ids = [];
$customerList = self::getPagarMe()
->customer()
->getList();

foreach ($customerList as $customer) {
$ids[] = $customer->getId();
}

return $ids;
}
}
7 changes: 7 additions & 0 deletions tests/acceptance/features/transaction.feature
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ Feature: Transaction
| 123456 |
| 1000001 |

Scenario Outline: Creating a Boleto Transaction using Customers from the API
Given make a boleto transaction with "<amount>", using Customers from the API
Then a list of valid transactions must be created
Examples:
| amount |
| 2345678 |

Scenario: Retrieving a Credit Card Transaction
Given a valid customer
And a valid card
Expand Down

0 comments on commit 56fba66

Please sign in to comment.