Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #61 from pmclain/feature/precision
Browse files Browse the repository at this point in the history
Fix amount when using zero-decimal currencies
  • Loading branch information
pmclain authored Aug 11, 2018
2 parents 5ec45ea + d1778c5 commit 47f4419
Show file tree
Hide file tree
Showing 42 changed files with 1,271 additions and 327 deletions.
10 changes: 9 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
sudo: required
services:
- docker
language: php
php:
- 7.1
matrix:
include:
- env:
Expand All @@ -15,13 +18,18 @@ matrix:
cache:
directories:
- $HOME/.composer/cache
install:
- mkdir -p build/logs && cd build && composer init --no-interaction && composer require php-coveralls/php-coveralls --no-interaction && cd ..
before_script:
- echo "COMPOSER_MAGENTO_USERNAME=$COMPOSER_MAGENTO_USERNAME" >> dev/composer.env
- echo "COMPOSER_MAGENTO_PASSWORD=$COMPOSER_MAGENTO_PASSWORD" >> dev/composer.env
- echo "COMPOSER_GITHUB_TOKEN=$COMPOSER_GITHUB_TOKEN" >> dev/composer.env
- sed -E -i "s/MAGENTO_VERSION=.*/MAGENTO_VERSION=${MAGENTO_VERSION}/" dev/docker-compose.yml
- docker-compose -f dev/docker-compose.yml build
- docker-compose -f dev/docker-compose.yml up -d
- docker-compose -f dev/docker-compose.yml exec app module-installer
script:
- docker-compose -f dev/docker-compose.yml exec app test-${TEST_SUITE}
after_success:
- cp dev/magento/dev/tests/${TEST_SUITE}/coverage/clover.xml build/logs/clover.xml
- sed -E -i "s/\/module/\./" build/logs/clover.xml
- travis_retry php build/vendor/bin/php-coveralls -v
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Magento 2 Stripe Integration
[![Build Status](https://travis-ci.org/pmclain/module-stripe.svg?branch=master)](https://travis-ci.org/pmclain/module-stripe)

[![Build Status](https://travis-ci.org/pmclain/module-stripe.svg?branch=master)](https://travis-ci.org/pmclain/module-stripe)
[![Coverage Status](https://coveralls.io/repos/github/pmclain/module-stripe/badge.svg?branch=master)](https://coveralls.io/github/pmclain/module-stripe?branch=master)
[![Latest Stable Version](https://poser.pugx.org/pmclain/module-stripe/v/stable)](https://packagist.org/packages/pmclain/module-stripe)
[![Total Downloads](https://poser.pugx.org/pmclain/module-stripe/downloads)](https://packagist.org/packages/pmclain/module-stripe)
[![License](https://poser.pugx.org/pmclain/module-stripe/license)](https://packagist.org/packages/pmclain/module-stripe)

Accept credit card payments through the Stripe payment gateway.

Expand Down
4 changes: 2 additions & 2 deletions dev/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3"
services:
web:
build: ./nginx
image: dfaarc44/m2-nginx
ports:
- "80:80"
- "443:443"
Expand All @@ -17,7 +17,7 @@ services:
- VIRTUAL_HOST=stripe.docker

app:
build: ./php71
image: dfaarc44/m2-stripe-app:php71
links:
- db
- redis
Expand Down
6 changes: 5 additions & 1 deletion src/Controller/ThreeDSecure/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\LocalizedException;
use Pmclain\Stripe\Model\Helper\OrderPlace;

class Redirect extends Action
Expand Down Expand Up @@ -62,14 +63,17 @@ public function execute()
} catch (\InvalidArgumentException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$resultRedirect->setPath('checkout/cart', ['_secure' => true]);
} catch (\Exception $e) {
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$resultRedirect->setPath('checkout/cart', ['_secure' => true]);
}

return $resultRedirect;
}

/**
* @param \Magento\Quote\Model\Quote $quote
*/
private function validateQuote($quote)
{
if (!$quote || !$quote->getItemsCount()) {
Expand Down
7 changes: 6 additions & 1 deletion src/Gateway/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Config extends \Magento\Payment\Gateway\Config\Config
const KEY_TEST_PUBLISHABLE_KEY = 'test_publishable_key';
const KEY_TEST_SECRET_KEY = 'test_secret_key';
const KEY_CURRENCY = 'currency';
const KEY_CURRENCY_PRECISION = 'currency_precision';
const KEY_CC_TYPES = 'cctypes';
const KEY_CC_TYPES_STRIPE_MAPPER = 'cctypes_stripe_mapper';
const KEY_USE_CCV = 'useccv';
Expand Down Expand Up @@ -58,7 +59,8 @@ public function getCcTypesMapper()
}

/**
* @return mixed
* @codeCoverageIgnore
* @return string
*/
public function getCurrency()
{
Expand Down Expand Up @@ -120,6 +122,7 @@ public function isTestMode()
}

/**
* @codeCoverageIgnore
* @return bool
*/
public function isDebugOn()
Expand All @@ -128,6 +131,7 @@ public function isDebugOn()
}

/**
* @codeCoverageIgnore
* @return bool
*/
public function isRequireThreeDSecure()
Expand All @@ -136,6 +140,7 @@ public function isRequireThreeDSecure()
}

/**
* @codeCoverageIgnore
* @return float
*/
public function getThreeDSecureThreshold()
Expand Down
81 changes: 81 additions & 0 deletions src/Gateway/Helper/PriceFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Pmclain_Stripe extension
* NOTICE OF LICENSE
*
* This source file is subject to the OSL 3.0 License
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/osl-3.0.php
*
* @category Pmclain
* @package Pmclain_Stripe
* @copyright Copyright (c) 2017-2018
* @license Open Software License (OSL 3.0)
*/

namespace Pmclain\Stripe\Gateway\Helper;

use Pmclain\Stripe\Gateway\Config\Config;

class PriceFormatter
{
/**
* @var Config
*/
private $config;

/**
* Formatter constructor.
* @param Config $config
*/
public function __construct(
Config $config
) {
$this->config = $config;
}

/**
* @param string $price
* @return string
*/
public function formatPrice($price)
{
$price = sprintf('%.' . ($this->isZeroDecimalCurrency() ? '0' : '2') . 'F', $price);

return str_replace('.', '', $price);
}

/**
* @return bool
*/
private function isZeroDecimalCurrency()
{
return in_array($this->config->getCurrency(), $this->getZeroDecimalCurrencies(), true);
}

/**
* @return array
*/
private function getZeroDecimalCurrencies()
{
return [
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'UGX',
'VND',
'VUV',
'XAF',
'XOF',
'XPF',
];
}
}
2 changes: 2 additions & 0 deletions src/Gateway/Helper/SubjectReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function readResponseObject(array $subject)
}

/**
* @codeCoverageIgnore
* @param array $subject
* @return \Magento\Payment\Gateway\Data\PaymentDataObjectInterface
*/
Expand All @@ -65,6 +66,7 @@ public function readTransaction(array $subject)
}

/**
* @codeCoverageIgnore
* @param array $subject
* @return mixed
*/
Expand Down
14 changes: 8 additions & 6 deletions src/Gateway/Request/CaptureDataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,33 @@

namespace Pmclain\Stripe\Gateway\Request;

use Pmclain\Stripe\Gateway\Request\PaymentDataBuilder;
use Magento\Framework\Exception\LocalizedException;
use Pmclain\Stripe\Gateway\Helper\SubjectReader;
use Magento\Payment\Gateway\Request\BuilderInterface;
use Pmclain\Stripe\Helper\Payment\Formatter;
use Pmclain\Stripe\Gateway\Helper\PriceFormatter;

class CaptureDataBuilder implements BuilderInterface
{
use Formatter;

const TRANSACTION_ID = 'transaction_id';

/**
* @var SubjectReader
*/
private $subjectReader;

private $priceFormatter;

/**
* CaptureDataBuilder constructor.
* @param SubjectReader $subjectReader
* @param PriceFormatter $priceFormatter
*/
public function __construct(
SubjectReader $subjectReader
SubjectReader $subjectReader,
PriceFormatter $priceFormatter
) {
$this->subjectReader = $subjectReader;
$this->priceFormatter = $priceFormatter;
}

/**
Expand All @@ -60,7 +62,7 @@ public function build(array $subject)

return [
self::TRANSACTION_ID => $transactionId,
PaymentDataBuilder::AMOUNT => $this->formatPrice($this->subjectReader->readAmount($subject))
PaymentDataBuilder::AMOUNT => $this->priceFormatter->formatPrice($this->subjectReader->readAmount($subject))
];
}
}
Loading

0 comments on commit 47f4419

Please sign in to comment.