Skip to content

Commit

Permalink
Merge pull request #2 from fraterblack/master
Browse files Browse the repository at this point in the history
Add methods to set extraAmount in purchaseRequest
  • Loading branch information
abdala authored Feb 3, 2017
2 parents ca158db + c34f545 commit da53d77
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Omnipay\PagSeguro\Item;
use Omnipay\PagSeguro\Support\Customer\Customer;
use Omnipay\Common\Exception\InvalidRequestException;

class PurchaseRequest extends AbstractRequest
{
Expand Down Expand Up @@ -41,6 +42,36 @@ public function setCustomer(Customer $value)
return $this->setParameter('customer', $value);
}

public function getExtraAmount()
{
$extraAmount = $this->getParameter('extraAmount');

if ($extraAmount !== null && $extraAmount != 0) {
if ($this->getCurrencyDecimalPlaces() > 0) {
if (is_int($extraAmount) || (is_string($extraAmount) && false === strpos((string) $extraAmount, '.'))) {
throw new InvalidRequestException(
'Please specify extra amount as a string or float, with decimal places.'
);
};
}

$extraAmount = $this->toFloat($extraAmount);

// Check for rounding that may occur if too many significant decimal digits are supplied.
$decimal_count = strlen(substr(strrchr(sprintf('%.8g', $extraAmount), '.'), 1));
if ($decimal_count > $this->getCurrencyDecimalPlaces()) {
throw new InvalidRequestException('Amount precision is too high for currency.');
}

return $this->formatCurrency($extraAmount);
}
}

public function setExtraAmount($value)
{
return $this->setParameter('extraAmount', $value);
}

public function setItems($items)
{
$serializedItems = [];
Expand Down Expand Up @@ -132,6 +163,7 @@ public function getData()

$data = [
'currency' => $this->getCurrency(),
'extraAmount' => $this->getExtraAmount(),
'reference' => $this->getTransactionReference(),
'redirectURL' => $this->getReturnUrl(),
'notificationURL' => $this->getNotifyUrl(),
Expand Down

0 comments on commit da53d77

Please sign in to comment.