Skip to content

Commit

Permalink
Price to int
Browse files Browse the repository at this point in the history
  • Loading branch information
budziam committed Mar 24, 2020
1 parent a57b452 commit bcdd8b4
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
9 changes: 2 additions & 7 deletions includes/Http/Services/PriceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand All @@ -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;
}
}
7 changes: 4 additions & 3 deletions includes/Payment/DirectBilling/DirectBillingChargeWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\System\Heart;
use App\System\Settings;
use App\Verification\Abstracts\SupportDirectBilling;
use UnexpectedValueException;

class DirectBillingChargeWallet implements IChargeWallet
{
Expand Down Expand Up @@ -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()],
Expand All @@ -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,
]);
}
Expand Down
8 changes: 4 additions & 4 deletions includes/Payment/Transfer/TransferChargeWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}

Expand Down
2 changes: 1 addition & 1 deletion includes/Verification/PaymentModules/Cashbill.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion includes/Verification/PaymentModules/Hostplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion includes/Verification/PaymentModules/Microsms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion includes/Verification/PaymentModules/OneShotOneKill.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']}]");
Expand Down
4 changes: 2 additions & 2 deletions includes/Verification/PaymentModules/SimPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion includes/Verification/PaymentModules/TPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
14 changes: 14 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/Http/Api/Shop/ChargeWalletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"] .
""
),
Expand All @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Payment/TransferPaymentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}
}

0 comments on commit bcdd8b4

Please sign in to comment.