Skip to content

Commit 94db75e

Browse files
committed
Pass amount as decimal instead of integer
1 parent afe8b3d commit 94db75e

File tree

74 files changed

+190
-141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+190
-141
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ $gateway = GatewayFactory::create('Stripe');
3030
$gateway->setApiKey('abc123');
3131

3232
$formData = ['number' => '4242424242424242', 'expiryMonth' => '6', 'expiryYear' => '2016', 'cvv' => '123'];
33-
$response = $gateway->purchase(['amount' => 1000, 'currency' => 'USD', 'card' => $formData])->send();
33+
$response = $gateway->purchase(['amount' => '10.00', 'currency' => 'USD', 'card' => $formData])->send();
3434

3535
if ($response->isSuccessful()) {
3636
// payment was successful: update database
@@ -252,7 +252,7 @@ Pass the options through to the method like so:
252252
```php
253253
$card = new CreditCard($formData);
254254
$request = $gateway->authorize([
255-
'amount' => 1000, // this represents $10.00
255+
'amount' => '10.00', // this represents $10.00
256256
'card' => $card,
257257
'returnUrl' => 'https://www.example.com/return',
258258
]);
@@ -281,7 +281,7 @@ For a successful responses, a reference will normally be generated, which can be
281281
at a later date. The following methods are always available:
282282

283283
```php
284-
$response = $gateway->purchase(['amount' => 1000, 'card' => $card])->send();
284+
$response = $gateway->purchase(['amount' => '10.00', 'card' => $card])->send();
285285

286286
$response->isSuccessful(); // is the response successful?
287287
$response->isRedirect(); // is the response a redirect?
@@ -299,7 +299,7 @@ POST (FormRedirectResponse). These could potentially be combined into a single r
299299
After processing a payment, the cart should check whether the response requires a redirect, and if so, redirect accordingly:
300300

301301
```php
302-
$response = $gateway->purchase(['amount' => 1000, 'card' => $card])->send();
302+
$response = $gateway->purchase(['amount' => '10.00', 'card' => $card])->send();
303303
if ($response->isSuccessful()) {
304304
// payment is complete
305305
} elseif ($response->isRedirect()) {
@@ -332,7 +332,7 @@ You can handle both scenarios by wrapping the entire request in a try-catch bloc
332332

333333
```php
334334
try {
335-
$response = $gateway->purchase(['amount' => 1000, 'card' => $card])->send();
335+
$response = $gateway->purchase(['amount' => '10.00', 'card' => $card])->send();
336336
if ($response->isSuccessful()) {
337337
// mark order as complete
338338
} elseif ($response->isRedirect()) {
@@ -359,7 +359,7 @@ are available:
359359

360360
Once you have a `cardReference`, you can use it instead of the `card` parameter when creating a charge:
361361

362-
$gateway->purchase(['amount' => 1000, 'cardReference' => 'abc']);
362+
$gateway->purchase(['amount' => '10.00', 'cardReference' => 'abc']);
363363

364364
## Recurring Billing
365365

src/Omnipay/AuthorizeNet/Message/AbstractRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function getBaseData()
7777
protected function getBillingData()
7878
{
7979
$data = array();
80-
$data['x_amount'] = $this->getAmountDecimal();
80+
$data['x_amount'] = $this->getAmount();
8181
$data['x_invoice_num'] = $this->getTransactionId();
8282
$data['x_description'] = $this->getDescription();
8383

src/Omnipay/AuthorizeNet/Message/CaptureRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function getData()
2323
$this->validate('amount', 'transactionReference');
2424

2525
$data = $this->getBaseData();
26-
$data['x_amount'] = $this->getAmountDecimal();
26+
$data['x_amount'] = $this->getAmount();
2727
$data['x_trans_id'] = $this->getTransactionReference();
2828

2929
return $data;

src/Omnipay/AuthorizeNet/Message/SIMCompleteAuthorizeRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function getData()
2929

3030
public function getHash()
3131
{
32-
return md5($this->getApiLoginId().$this->getTransactionId().$this->getAmountDecimal());
32+
return md5($this->getApiLoginId().$this->getTransactionId().$this->getAmount());
3333
}
3434

3535
public function send()

src/Omnipay/CardSave/Message/PurchaseRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getData()
5353

5454
$data->PaymentMessage->MerchantAuthentication['MerchantID'] = $this->getMerchantId();
5555
$data->PaymentMessage->MerchantAuthentication['Password'] = $this->getPassword();
56-
$data->PaymentMessage->TransactionDetails['Amount'] = $this->getAmount();
56+
$data->PaymentMessage->TransactionDetails['Amount'] = $this->getAmountInteger();
5757
$data->PaymentMessage->TransactionDetails['CurrencyCode'] = $this->getCurrencyNumeric();
5858
$data->PaymentMessage->TransactionDetails->OrderID = $this->getTransactionId();
5959
$data->PaymentMessage->TransactionDetails->OrderDescription = $this->getDescription();

src/Omnipay/Common/Message/AbstractRequest.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,31 @@ public function setCardReference($value)
163163

164164
public function getAmount()
165165
{
166-
return $this->getParameter('amount');
166+
$amount = $this->getParameter('amount');
167+
if ($amount) {
168+
if (!is_float($amount) &&
169+
$this->getCurrencyDecimalPlaces() > 0 &&
170+
false === strpos((string) $amount, '.')) {
171+
throw new InvalidRequestException("Please specify amount as a string or float, with decimal places (e.g. '10.00' to represent $10.00).");
172+
}
173+
174+
return number_format(
175+
$amount,
176+
$this->getCurrencyDecimalPlaces(),
177+
'.',
178+
''
179+
);
180+
}
167181
}
168182

169183
public function setAmount($value)
170184
{
171-
return $this->setParameter('amount', (int) $value);
185+
return $this->setParameter('amount', $value);
172186
}
173187

174-
public function getAmountDecimal()
188+
public function getAmountInteger()
175189
{
176-
return number_format(
177-
$this->getAmount() / $this->getCurrencyDecimalFactor(),
178-
$this->getCurrencyDecimalPlaces(),
179-
'.',
180-
''
181-
);
190+
return (int) round($this->getAmount() * $this->getCurrencyDecimalFactor());
182191
}
183192

184193
public function getCurrency()

src/Omnipay/GoCardless/Message/PurchaseRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function getData()
2828
$data['cancel_uri'] = $this->getCancelUrl();
2929
$data['bill'] = array();
3030
$data['bill']['merchant_id'] = $this->getMerchantId();
31-
$data['bill']['amount'] = $this->getAmountDecimal();
31+
$data['bill']['amount'] = $this->getAmount();
3232
$data['bill']['name'] = $this->getDescription();
3333

3434
if ($this->getCard()) {

src/Omnipay/Migs/Message/AbstractRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function getBaseData()
5656
$data['vpc_Version'] = '1';
5757
$data['vpc_Locale'] = 'en';
5858
$data['vpc_Command'] = $this->action;
59-
$data['vpc_Amount'] = $this->getAmount();
59+
$data['vpc_Amount'] = $this->getAmountInteger();
6060
$data['vpc_MerchTxnRef'] = $this->getTransactionId();
6161
$data['vpc_OrderInfo'] = $this->getDescription();
6262
$data['vpc_ReturnURL'] = $this->getReturnUrl();

src/Omnipay/Mollie/Message/PurchaseRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function getData()
2626
$data['returnurl'] = $this->getReturnUrl();
2727
$data['reporturl'] = $this->getNotifyUrl();
2828
$data['bank_id'] = $this->getIssuer();
29-
$data['amount'] = $this->getAmount();
29+
$data['amount'] = $this->getAmountInteger();
3030
$data['description'] = $this->getDescription();
3131

3232
return $data;

src/Omnipay/NetBanx/Gateway.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class Gateway extends AbstractGateway
2020
{
2121
const DECISION_ACCEPTED = 'ACCEPTED';
22-
const CREATE_CARD_AMOUNT = 100;
22+
const CREATE_CARD_AMOUNT = '1.00';
2323
const CODE_OK = '0';
2424

2525
/**

0 commit comments

Comments
 (0)