Skip to content

Commit

Permalink
2.0.0: #28
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-fediuk committed Oct 12, 2017
1 parent 91a8989 commit f7add2d
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 12 deletions.
29 changes: 29 additions & 0 deletions Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Dfe\Stripe;
use Magento\Framework\App\ScopeInterface as IScope;
use Magento\Store\Model\Store;
/**
* 2017-10-12
* «JCB, Discover, and Diners Club cards can only be charged in USD»:
* https://github.com/mage2pro/stripe/issues/28
* @method Method m()
*/
final class Currency extends \Df\Payment\Currency {
/**
* 2017-10-12
* Note 1. «JCB, Discover, and Diners Club cards can only be charged in USD»:
* https://github.com/mage2pro/stripe/issues/28
* Note 2.
* `brand`: «Card brand.
* Can be `Visa`, `American Express`, `MasterCard`, `Discover`, `JCB`, `Diners Club`, or `Unknown`.»
* https://stripe.com/docs/api#card_object-brand
* @override
* @see \Df\Payment\Currency::_iso3()
* @used-by \Df\Payment\Currency::iso3()
* @param null|string|int|IScope|Store $s [optional]
* @return string
*/
protected function _iso3($s = null) {return
in_array($this->m()->cardType(), ['Discover', 'JCB', 'Diners Club']) ? 'USD' : parent::_iso3($s)
;}
}
6 changes: 4 additions & 2 deletions Facade/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ function __construct($p) {$this->_p = is_array($p) ? $p : $p->__toArray();}
* @override
* @see \Df\StripeClone\Facade\ICard::brand()
* @used-by \Df\StripeClone\CardFormatter::ii()
* @used-by \Df\StripeClone\CardFormatter::label()
* @used-by \Df\StripeClone\CardFormatter::label()
* @used-by \Dfe\Stripe\Method::cardType()
* @return string
*/
function brand() {return $this->_p['brand'];}
Expand Down Expand Up @@ -74,7 +75,8 @@ function expYear() {return $this->_p['exp_year'];}
* @override
* @see \Df\StripeClone\Facade\ICard::id()
* @used-by \Df\StripeClone\ConfigProvider::cards()
* @used-by \Df\StripeClone\Facade\Customer::cardIdForJustCreated()
* @used-by \Df\StripeClone\Facade\Customer::cardIdForJustCreated()
* @used-by \Dfe\Stripe\Method::cardType()
* @return string
*/
function id() {return $this->_p['id'];}
Expand Down
7 changes: 7 additions & 0 deletions Facade/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ protected function _get($id) {/** @var C $c */return dfo($c = C::retrieve($id),

/**
* 2017-02-11
* 2017-10-12
* `sources`:
* «The customer’s payment sources, if any.»
* https://stripe.com/docs/api#customer_object-sources
* `data`:
* «The list contains all payment sources that have been attached to the customer.»
* https://stripe.com/docs/api#customer_object-sources-data
* @override
* @see \Df\StripeClone\Facade\Customer::cardsData()
* @used-by \Df\StripeClone\Facade\Customer::cards()
Expand Down
58 changes: 58 additions & 0 deletions Method.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Dfe\Stripe;
use Df\Core\Exception as DFE;
use Df\Payment\Token;
use Df\StripeClone\Facade\Customer as fCustomer;
use Dfe\Stripe\Facade\Card;
use Magento\Sales\Model\Order as O;
use Magento\Sales\Model\Order\Creditmemo as CM;
use Magento\Sales\Model\Order\Payment as OP;
Expand All @@ -16,6 +19,45 @@ final class Method extends \Df\StripeClone\Method {
*/
function canCapturePartial() {return true;}

/**
* 2017-10-12 It will be null for non-card payments (such payments are not implemented yet).
* @used-by \Dfe\Stripe\Currency::_iso3()
* @return string|null
*/
function cardType() {return dfc($this, function() {
/** @var string $r */
if (!($r = $this->iia(self::$II_CARD_TYPE))) {
/**
* 2017-10-12
* A payment with a previously used card case.
* In this case we can detect the type of the previously used card
* by an additional Stripe API request:
* https://stripe.com/docs/api#retrieve_customer
* @see \Dfe\Stripe\Facade\Customer::cardsData()
*
* @var string|null $cardId
* $cardId will be null in the non-payment scenarios.
*/
if (
($cardId = Token::get($this->ii(), false))
&& $this->fCharge()->isCardId($cardId)
&& ($customerId = df_ci_get($this))
) {
$fc = fCustomer::s($this); /** @var FCustomer $fc */
$this->s()->init();
if ($customer = $fc->get($customerId) /** @var object|null $customer */) {
/** @var Card|null $card */
if ($card = df_find(function(Card $card) use($cardId) {return
$cardId === $card->id()
;}, $fc->cards($customer))) {
$r = $card->brand();
}
}
}
}
return $r;
});}

/**
* 2016-11-13
* https://stripe.com/docs/api/php#create_charge-amount
Expand Down Expand Up @@ -64,6 +106,15 @@ protected function convertException(\Exception $e) {return
$e instanceof lException ? new Exception($e) : $e
;}

/**
* 2017-10-12
* @override
* @see \Df\StripeClone\Method::iiaKeys()
* @used-by \Df\Payment\Method::assignData()
* @return string[]
*/
protected function iiaKeys() {return array_merge(parent::iiaKeys(), [self::$II_CARD_TYPE]);}

/**
* 2016-12-26
* Хотя Stripe использует для страниц транзакций адреса вида
Expand Down Expand Up @@ -116,4 +167,11 @@ private function minimumAmount($c) {return
,'HKD' => 4, 'JPY' => 50, 'MXN' => 10, 'NOK' => 3, 'SEK' => 3, 'SGD' => .5, 'USD' => .5
], $c, .5)
;}

/**
* 2017-10-12
* @used-by cardType()
* @used-by iiaKeys()
*/
private static $II_CARD_TYPE = 'cardType';
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mage2pro/stripe"
,"version": "1.12.5"
,"version": "2.0.0"
,"description": "The «Stripe» payment extension for Magento 2."
,"type": "magento2-module"
,"homepage": "https://mage2.pro/c/extensions/stripe"
Expand All @@ -11,7 +11,7 @@
"homepage": "https://mage2.pro/users/dmitry_fedyuk",
"role": "Developer"
}]
,"require": {"mage2pro/core": ">=3.1.0", "stripe/stripe-php": "3.*"}
,"require": {"mage2pro/core": ">=3.2.0", "stripe/stripe-php": "3.*"}
,"autoload": {"files": ["registration.php"], "psr-4": {"Dfe\\Stripe\\": ""}}
,"keywords": [
"API"
Expand Down
52 changes: 44 additions & 8 deletions view/frontend/web/main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,52 @@
// 2016-03-01
// 2017-10-12 «Stripe.js v2 Reference»: https://stripe.com/docs/stripe.js/v2
define([
'Df_StripeClone/main', 'Magento_Checkout/js/model/quote', 'https://js.stripe.com/v2/'
], function(parent, quote) {'use strict';
/** 2017-09-06 @uses Class::extend() https://github.com/magento/magento2/blob/2.2.0-rc2.3/app/code/Magento/Ui/view/base/web/js/lib/core/class.js#L106-L140 */
return parent.extend({
/**
* 2017-10-12
* r looks like:
* {
* card: {
* address_city: "Palo Alto",
* address_country: "US",
* address_line1: "12 Main Street",
* address_line2: "Apt 42",
* address_state: "CA",
* address_zip: "94301",
* brand: "Visa",
* country: "US",
* exp_month: 2,
* exp_year: 2018,
* funding: "credit",
* last4: "4242",
* name: null,
* object: "card"
* },
* created: 1507803936,
* id: "tok_8DPg4qjJ20F1aM",
* livemode: true,
* object: "token",
* type: "card",
* used: false
* }
* https://stripe.com/docs/stripe.js/v2
* @override
* @see Df_StripeClone/main::dfDataFromTokenResp()
* @used-by Df_StripeClone/main::dfData()
* @param {Object} r
* @returns {Object}
*/
dfDataFromTokenResp: function(r) {return {cardType: r.card.brand};},
/**
* 2016-03-01
* 2016-03-08
* Раньше реализация была такой:
* return _.keys(this.getCcAvailableTypes())
*
* https://support.stripe.com/questions/which-cards-and-payment-types-can-i-accept-with-stripe
* https://web.archive.org/web/20160321062153/https://support.stripe.com/questions/which-cards-and-payment-types-can-i-accept-with-stripe
* «Which cards and payment types can I accept with Stripe?
* With Stripe, you can charge almost any kind of credit or debit card:
* U.S. businesses can accept
Expand All @@ -24,9 +60,13 @@ return parent.extend({
* А изменить этот порядок коротко не получается:
* https://github.com/magento/magento2/blob/487f5f45/app/code/Magento/Payment/Model/CcGenericConfigProvider.php#L105-L124
*
* 2017-02-05
* The bank card network codes: https://mage2.pro/t/2647
* 2017-02-05 The bank card network codes: https://mage2.pro/t/2647
*
* 2017-10-12
* Note 1. «JCB, Discover, and Diners Club cards can only be charged in USD»:
* https://github.com/mage2pro/stripe/issues/28
* Note 2. «Can a non-USA merchant accept the JCB, Discover, and Diners Club bank cards?»
* https://mage2.pro/t/4670
* @returns {String[]}
*/
getCardTypes: function() {return(
Expand All @@ -39,11 +79,7 @@ return parent.extend({
* https://github.com/mage2pro/core/blob/2.4.21/Payment/view/frontend/web/card.js#L77-L110
* @returns {exports}
*/
initialize: function() {
this._super();
Stripe.setPublishableKey(this.publicKey());
return this;
},
initialize: function() {this._super(); Stripe.setPublishableKey(this.publicKey()); return this;},
/**
* 2017-02-16
* @override
Expand Down

0 comments on commit f7add2d

Please sign in to comment.