Skip to content

Commit

Permalink
Update mappers to map from/to decimal places (paysera#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas7777 authored Apr 6, 2020
1 parent ac32b47 commit daa9edc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 2.2.0
- Changed keys by how transaction is being encoded to use plain amount instead of cents
- Changed decoding to use decimals instead of cents

## 2.1.0
- Added `\Paysera_WalletApi_Mapper::decodeWalletBalance` mapping from decimal

Expand Down
34 changes: 17 additions & 17 deletions src/Paysera/WalletApi/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function encodePayment(Paysera_WalletApi_Entity_Payment $payment)
}
$price = $payment->getPrice();
if ($price !== null) {
$result['price'] = $price->getAmountInCents();
$result['price_decimal'] = $price->getAmount();
$result['currency'] = $price->getCurrency();
}
$commission = $payment->getCommission();
Expand All @@ -59,7 +59,7 @@ public function encodePayment(Paysera_WalletApi_Entity_Payment $payment)
if ($price !== null && $price->getCurrency() !== $cashback->getCurrency()) {
throw new Paysera_WalletApi_Exception_LogicException('Price and cashback currency must be the same');
}
$result['cashback'] = $cashback->getAmountInCents();
$result['cashback_decimal'] = $cashback->getAmount();
$result['currency'] = $cashback->getCurrency();
}
if ($payment->hasItems()) {
Expand Down Expand Up @@ -91,7 +91,7 @@ public function encodePayment(Paysera_WalletApi_Entity_Payment $payment)
$result['funds_source'] = $this->encodeFundsSource($fundsSource);
}

if (!(isset($result['description']) && isset($result['price']) || isset($result['items']))) {
if (!(isset($result['description']) && isset($result['price_decimal']) || isset($result['items']))) {
throw new Paysera_WalletApi_Exception_LogicException(
'Description and price are required if items are not set'
);
Expand Down Expand Up @@ -265,14 +265,14 @@ public function decodePayment($data)
$this->setProperty($payment, 'status', $data['status']);

$payment->setPrice(
Paysera_WalletApi_Entity_Money::create()->setAmountInCents($data['price'])->setCurrency($data['currency'])
Paysera_WalletApi_Entity_Money::create()->setAmount($data['price_decimal'])->setCurrency($data['currency'])
);
if (isset($data['commission'])) {
$payment->setCommission($this->decodeCommission($data['commission'], $data['currency']));
}
if (isset($data['cashback'])) {
if (isset($data['cashback_decimal'])) {
$payment->setCashback(
Paysera_WalletApi_Entity_Money::create()->setAmountInCents($data['cashback'])->setCurrency($data['currency'])
Paysera_WalletApi_Entity_Money::create()->setAmount($data['cashback_decimal'])->setCurrency($data['currency'])
);
}
if (isset($data['wallet'])) {
Expand Down Expand Up @@ -356,11 +356,11 @@ protected function encodeCommission(Paysera_WalletApi_Entity_Commission $commiss
$result = array();

if ($commission->getOutCommission() !== null) {
$result['out_commission'] = $commission->getOutCommission()->getAmountInCents();
$result['out_commission_decimal'] = $commission->getOutCommission()->getAmount();
}

if ($commission->getInCommission() !== null) {
$result['in_commission'] = $commission->getInCommission()->getAmountInCents();
$result['in_commission_decimal'] = $commission->getInCommission()->getAmount();
}

return $result;
Expand All @@ -378,15 +378,15 @@ protected function decodeCommission($data, $currency)
{
$commission = new Paysera_WalletApi_Entity_Commission();

if (isset($data['in_commission'])) {
if (isset($data['in_commission_decimal'])) {
$commission->setInCommission(
Paysera_WalletApi_Entity_Money::create()->setAmountInCents($data['in_commission'])->setCurrency($currency)
Paysera_WalletApi_Entity_Money::create()->setAmount($data['in_commission_decimal'])->setCurrency($currency)
);
}

if (isset($data['out_commission'])) {
if (isset($data['out_commission_decimal'])) {
$commission->setOutCommission(
Paysera_WalletApi_Entity_Money::create()->setAmountInCents($data['out_commission'])->setCurrency($currency)
Paysera_WalletApi_Entity_Money::create()->setAmount($data['out_commission_decimal'])->setCurrency($currency)
);
}

Expand Down Expand Up @@ -415,14 +415,14 @@ protected function encodeItem(Paysera_WalletApi_Entity_Item $item)
if (($imageUri = $item->getImageUri()) !== null) {
$result['image_uri'] = $imageUri;
}
$result['price'] = $item->getPrice()->getAmountInCents();
$result['price_decimal'] = $item->getPrice()->getAmount();
$result['currency'] = $item->getPrice()->getCurrency();
$result['quantity'] = $item->getQuantity();
if (($parameters = $item->getParameters()) !== null) {
$result['parameters'] = $parameters;
}
if ($item->getTotalPrice() !== null) {
$result['total_price'] = $item->getTotalPrice()->getAmountInCents();
$result['total_price_decimal'] = $item->getTotalPrice()->getAmount();
}
return $result;
}
Expand All @@ -439,7 +439,7 @@ protected function decodeItem($data)
$item = new Paysera_WalletApi_Entity_Item();
$item->setTitle($data['title']);
$price = Paysera_WalletApi_Entity_Money::create()
->setAmountInCents($data['price'])
->setAmount($data['price_decimal'])
->setCurrency($data['currency']);
$item->setPrice($price);
$item->setQuantity($data['quantity']);
Expand All @@ -452,10 +452,10 @@ protected function decodeItem($data)
if (isset($data['parameters'])) {
$item->setParameters($data['parameters']);
}
if (isset($data['total_price'])) {
if (isset($data['total_price_decimal'])) {
$item->setTotalPrice(
Paysera_WalletApi_Entity_Money::create()
->setAmountInCents($data['total_price'])
->setAmount($data['total_price_decimal'])
->setCurrency($data['currency'])
);
}
Expand Down

0 comments on commit daa9edc

Please sign in to comment.