diff --git a/includes/Http/Controllers/Api/Admin/WalletChargeCollection.php b/includes/Http/Controllers/Api/Admin/WalletChargeCollection.php index f6344279a..b027f03e9 100644 --- a/includes/Http/Controllers/Api/Admin/WalletChargeCollection.php +++ b/includes/Http/Controllers/Api/Admin/WalletChargeCollection.php @@ -49,7 +49,7 @@ public function post( } $editedUser = $heart->getUser($userId); - $quantity = intval($validated['quantity'] * 100); + $quantity = price_to_int($validated['quantity']); // Zmiana wartości quantity, aby stan konta nie zszedł poniżej zera $quantity = max($quantity, -$editedUser->getWallet()); diff --git a/includes/Http/Services/PriceService.php b/includes/Http/Services/PriceService.php index 67ef7c792..91d274355 100644 --- a/includes/Http/Services/PriceService.php +++ b/includes/Http/Services/PriceService.php @@ -13,8 +13,8 @@ class PriceService { public function createValidator(array $body) { - $transferPrice = $this->getFloatValueAsInt($body, "transfer_price"); - $directBillingPrice = $this->getFloatValueAsInt($body, "direct_billing_price"); + $directBillingPrice = price_to_int(array_get($body, "direct_billing_price")); + $transferPrice = price_to_int(array_get($body, "transfer_price")); return new Validator( array_merge($body, [ @@ -34,9 +34,4 @@ public function createValidator(array $body) ] ); } - - private function getFloatValueAsInt(array $body, $key) - { - return strlen(array_get($body, $key)) ? intval(array_get($body, $key) * 100) : null; - } } diff --git a/includes/Payment/DirectBilling/DirectBillingChargeWallet.php b/includes/Payment/DirectBilling/DirectBillingChargeWallet.php index 5d963921e..583ae5da7 100644 --- a/includes/Payment/DirectBilling/DirectBillingChargeWallet.php +++ b/includes/Payment/DirectBilling/DirectBillingChargeWallet.php @@ -12,6 +12,7 @@ use App\System\Heart; use App\System\Settings; use App\Verification\Abstracts\SupportDirectBilling; +use UnexpectedValueException; class DirectBillingChargeWallet implements IChargeWallet { @@ -43,7 +44,7 @@ public function setup(Purchase $purchase, array $body) { $validator = new Validator( [ - 'direct_billing_price' => as_float(array_get($body, 'direct_billing_price')), + 'direct_billing_price' => price_to_int(array_get($body, 'direct_billing_price')), ], [ 'direct_billing_price' => [new RequiredRule(), new NumberRule()], @@ -57,11 +58,11 @@ public function setup(Purchase $purchase, array $body) ); if (!($paymentModule instanceof SupportDirectBilling)) { - throw new \UnexpectedValueException("Payment module doesn't support direct billing"); + throw new UnexpectedValueException("Payment module doesn't support direct billing"); } $purchase->setPayment([ - Purchase::PAYMENT_PRICE_DIRECT_BILLING => intval($price * 100), + Purchase::PAYMENT_PRICE_DIRECT_BILLING => $price, Purchase::PAYMENT_DISABLED_DIRECT_BILLING => false, ]); } diff --git a/includes/Payment/Transfer/TransferChargeWallet.php b/includes/Payment/Transfer/TransferChargeWallet.php index f5e756fab..0473fd907 100644 --- a/includes/Payment/Transfer/TransferChargeWallet.php +++ b/includes/Payment/Transfer/TransferChargeWallet.php @@ -44,21 +44,21 @@ public function setup(Purchase $purchase, array $body) { $validator = new Validator( [ - 'transfer_price' => as_float(array_get($body, 'transfer_price')), + 'transfer_price' => array_get($body, 'transfer_price'), ], [ 'transfer_price' => [new RequiredRule(), new NumberRule(), new MinValueRule(1.01)], ] ); $validated = $validator->validateOrFail(); - $transferPrice = $validated["transfer_price"]; + $transferPrice = price_to_int($validated["transfer_price"]); $purchase->setPayment([ - Purchase::PAYMENT_PRICE_TRANSFER => intval($transferPrice * 100), + Purchase::PAYMENT_PRICE_TRANSFER => $transferPrice, Purchase::PAYMENT_DISABLED_TRANSFER => false, ]); $purchase->setOrder([ - Purchase::ORDER_QUANTITY => intval($transferPrice * 100), + Purchase::ORDER_QUANTITY => $transferPrice, ]); } diff --git a/includes/Verification/PaymentModules/Cashbill.php b/includes/Verification/PaymentModules/Cashbill.php index 176114cef..aaa397b6d 100644 --- a/includes/Verification/PaymentModules/Cashbill.php +++ b/includes/Verification/PaymentModules/Cashbill.php @@ -108,7 +108,7 @@ public function prepareTransfer(Purchase $purchase, $dataFilename) public function finalizeTransfer(array $query, array $body) { - $amount = intval($body['amount'] * 100); + $amount = price_to_int($body['amount']); $finalizedPayment = new FinalizedPayment(); $finalizedPayment->setStatus($this->isPaymentValid($body)); diff --git a/includes/Verification/PaymentModules/Hostplay.php b/includes/Verification/PaymentModules/Hostplay.php index 37319d399..ebecb9316 100644 --- a/includes/Verification/PaymentModules/Hostplay.php +++ b/includes/Verification/PaymentModules/Hostplay.php @@ -54,7 +54,7 @@ public function verifySms($returnCode, $number) } $content = $response->json(); - $responseNumber = $this->getSmsNumberByProvision(intval($content['kwota'] * 100)); + $responseNumber = $this->getSmsNumberByProvision(price_to_int($content['kwota'])); if (strtoupper($content['status']) === 'OK') { if ($responseNumber == $number) { diff --git a/includes/Verification/PaymentModules/Microsms.php b/includes/Verification/PaymentModules/Microsms.php index dc341d8e3..93601ccbf 100644 --- a/includes/Verification/PaymentModules/Microsms.php +++ b/includes/Verification/PaymentModules/Microsms.php @@ -157,7 +157,7 @@ public function prepareTransfer(Purchase $purchase, $dataFilename) public function finalizeTransfer(array $query, array $body) { $isTest = strtolower(array_get($body, 'test')) === "true"; - $amount = intval(array_get($body, 'amountPay') * 100); + $amount = price_to_int(array_get($body, 'amountPay')); $finalizedPayment = new FinalizedPayment(); $finalizedPayment->setStatus($this->isPaymentValid($body)); diff --git a/includes/Verification/PaymentModules/OneShotOneKill.php b/includes/Verification/PaymentModules/OneShotOneKill.php index 86d5afaad..6441adfba 100644 --- a/includes/Verification/PaymentModules/OneShotOneKill.php +++ b/includes/Verification/PaymentModules/OneShotOneKill.php @@ -80,7 +80,7 @@ public function verifySms($returnCode, $number) switch ($content['status']) { case 'ok': - $responseNumber = $this->getSmsNumberByProvision(intval($content['amount'] * 100)); + $responseNumber = $this->getSmsNumberByProvision(price_to_int($content['amount'])); if ($responseNumber === null) { $this->fileLogger->error("1s1k invalid amount [{$content['amount']}]"); diff --git a/includes/Verification/PaymentModules/SimPay.php b/includes/Verification/PaymentModules/SimPay.php index 1c5126b2f..91ba4393b 100644 --- a/includes/Verification/PaymentModules/SimPay.php +++ b/includes/Verification/PaymentModules/SimPay.php @@ -173,8 +173,8 @@ public function finalizeDirectBilling(array $query, array $body) $this->tryToFetchIps(); $id = array_get($body, "id"); - $valueGross = intval(array_get($body, "valuenet_gross") * 100); - $valuePartner = intval(array_get($body, "valuepartner") * 100); + $valueGross = price_to_int(array_get($body, "valuenet_gross")); + $valuePartner = price_to_int(array_get($body, "valuepartner")); $control = array_get($body, "control"); $finalizedPayment = new FinalizedPayment(); diff --git a/includes/Verification/PaymentModules/TPay.php b/includes/Verification/PaymentModules/TPay.php index fc2ef6392..4d3f3b1cc 100644 --- a/includes/Verification/PaymentModules/TPay.php +++ b/includes/Verification/PaymentModules/TPay.php @@ -72,7 +72,8 @@ public function prepareTransfer(Purchase $purchase, $dataFilename) public function finalizeTransfer(array $query, array $body) { - $amount = intval(array_get($body, 'tr_amount') * 100); + // e.g. "40.80" + $amount = price_to_int(array_get($body, 'tr_amount')); $finalizedPayment = new FinalizedPayment(); $finalizedPayment->setStatus($this->isPaymentValid($body)); diff --git a/includes/functions.php b/includes/functions.php index adbaa7080..23e0b85c1 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -548,6 +548,20 @@ function as_datetime_string($value) return $date ? $date->format("Y-m-d H:i:s") : ""; } +/** + * @param string|float $value + * @return int|null + */ +function price_to_int($value) +{ + if ($value === null || $value === "") { + return null; + } + + // We do it that way because of the floating point issues + return (int) str_replace(".", "", number_format($value, 2)); +} + // https://stackoverflow.com/questions/7153000/get-class-name-from-file/44654073 function get_class_from_file($path) { diff --git a/tests/Feature/Http/Api/Shop/ChargeWalletTest.php b/tests/Feature/Http/Api/Shop/ChargeWalletTest.php index f7099caa5..831336e34 100644 --- a/tests/Feature/Http/Api/Shop/ChargeWalletTest.php +++ b/tests/Feature/Http/Api/Shop/ChargeWalletTest.php @@ -47,7 +47,7 @@ public function charges_using_transfer() $validationResponse = $this->post("/api/purchases", [ "service_id" => ChargeWalletServiceModule::MODULE_ID, "method" => Purchase::METHOD_TRANSFER, - "transfer_price" => 2.5, + "transfer_price" => 40.8, ]); $this->assertSame(200, $validationResponse->getStatusCode()); $json = $this->decodeJsonResponse($validationResponse); @@ -62,14 +62,14 @@ public function charges_using_transfer() $response = $this->post("/api/ipn/transfer/{$paymentPlatform->getId()}", [ "tr_id" => 1, - "tr_amount" => 2.5, + "tr_amount" => "40.80", "tr_crc" => $json["data"]["crc"], "id" => 1, "test_mode" => 1, "md5sum" => md5( array_get($paymentPlatform->getData(), "account_id") . "1" . - "2.50" . + "40.80" . $json["data"]["crc"] . "" ), @@ -78,7 +78,7 @@ public function charges_using_transfer() ]); $this->assertSame(200, $response->getStatusCode()); $freshUser = $this->userRepository->get($user->getUid()); - $this->assertSame(250, $freshUser->getWallet()); + $this->assertSame(4080, $freshUser->getWallet()); } /** @test */ diff --git a/tests/Feature/Payment/TransferPaymentServiceTest.php b/tests/Feature/Payment/TransferPaymentServiceTest.php index f0003b4fe..6c1890baa 100644 --- a/tests/Feature/Payment/TransferPaymentServiceTest.php +++ b/tests/Feature/Payment/TransferPaymentServiceTest.php @@ -46,7 +46,7 @@ public function pays_with_transfer() $price = $this->factory->price([ 'service_id' => $serviceId, 'server_id' => $server->getId(), - 'transfer_price' => 190, + 'transfer_price' => 4080, ]); $purchase = new Purchase(new User()); @@ -80,6 +80,6 @@ public function pays_with_transfer() // then $paymentTransfer = $paymentTransferRepository->get($finalizedPayment->getOrderId()); $this->assertNotNull($paymentTransfer); - $this->assertEquals(190, $paymentTransfer->getIncome()); + $this->assertEquals(4080, $paymentTransfer->getIncome()); } }