Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
andrelopez committed Sep 7, 2019
2 parents ab7d239 + ab3362b commit d77ed33
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 30 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Stripe Payments Changelog

## 2.0.4 - 2019.09.07

### Added
- Added one-time setup fee to subscriptions when using new Stripe checkout
- Added Portuguese and Polish languages
- Added the `oneTimeSetupFeeLabel` setting.

### Fixed
- Fixed redirect issue after subscription payment on new Stripe Checkout.
- Fixed issue when using the Norwegian language

## 2.0.3 - 2019.09.04

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "enupal/stripe",
"description": "Allows customers sign up for recurring and one-time payments with Stripe, perfect for orders, donations, subscriptions, and events. Create simple payment forms in seconds easily without coding. For Craft CMS 3.x",
"type": "craft-plugin",
"version": "2.0.3",
"version": "2.0.4",
"keywords": [
"craft",
"cms",
Expand Down
24 changes: 17 additions & 7 deletions src/controllers/StripeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,28 @@ public function actionFinishOrder()
return $this->redirect('/');
}
// Get the order from the payment intent id
$paymentIntent = StripePlugin::$app->paymentIntents->getPaymentIntent($checkoutSession['payment_intent']);
if ($paymentIntent === null){
Craft::error('Unable to find the payment intent id: '.$checkoutSession['payment_intent'], __METHOD__);
return $this->redirect('/');
$stripeId = null;
if ($checkoutSession['payment_intent'] !== null){
$paymentIntent = StripePlugin::$app->paymentIntents->getPaymentIntent($checkoutSession['payment_intent']);
if ($paymentIntent === null){
Craft::error('Unable to find the payment intent id: '.$checkoutSession['payment_intent'], __METHOD__);
return $this->redirect('/');
}

$stripeId = $paymentIntent['charges']['data'][0];
}else if($checkoutSession['subscription'] !== null){
$stripeId = $checkoutSession['subscription'];
}

$charge = $paymentIntent['charges']['data'][0];
if ($stripeId === null){
Craft::error('Unable to find the stripe id from the checkout session: ', __METHOD__);
return $this->redirect('/');
}

$order = StripePlugin::$app->orders->getOrderByStripeId($charge['id']);
$order = StripePlugin::$app->orders->getOrderByStripeId($stripeId);

if ($order === null){
Craft::error('Unable to find the order by stripe id: '.$charge['id'], __METHOD__);
Craft::error('Unable to find the order by stripe id: '.$stripeId, __METHOD__);
return $this->redirect('/');
}

Expand Down
5 changes: 0 additions & 5 deletions src/elements/PaymentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,11 +621,6 @@ public function getPublicData($options = null)

$paymentTypeIds = json_decode($this->paymentType, true);
$singlePlanSetupFee = $this->singlePlanSetupFee;
if ($this->settings->useSca){
// @todo one time fees amounts are not supported in SCA
$setupFees = [];
$singlePlanSetupFee = '';
}

$publicData = [
'useSca' => $this->settings->useSca,
Expand Down
1 change: 1 addition & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Settings extends Model
public $returnUrl;
public $defaultCurrency = 'USD';
public $chargeDescription = 'Order from {email}';
public $oneTimeSetupFeeLabel = 'One time set-up fee';
// Tax
public $enableTaxes = 0;
public $taxType = 0;
Expand Down
27 changes: 27 additions & 0 deletions src/services/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ private function handleSubscription(PaymentForm $paymentForm, $postData, $metada
// By default assume that is a single plan
$plan = $paymentForm->getSinglePlan();
$trialPeriodDays = null;
$settings = Stripe::$app->settings->getSettings();
$oneTineFee = [];

if ($paymentForm->subscriptionType == SubscriptionType::SINGLE_PLAN && $paymentForm->enableCustomPlanAmount) {
if ($data['amount'] > 0){
Expand Down Expand Up @@ -157,6 +159,15 @@ private function handleSubscription(PaymentForm $paymentForm, $postData, $metada
}

$plan = StripePlugin::$app->plans->getStripePlan($planId);
$setupFee = StripePlugin::$app->orders->getSetupFeeFromMatrix($planId, $paymentForm);
if ($setupFee){
$oneTineFee = [
'amount' => Stripe::$app->orders->convertToCents($setupFee, $paymentForm->currency),
'currency' => $paymentForm->currency,
'name' => $settings->oneTimeSetupFeeLabel,
'quantity' => 1
];
}
}

// Override plan if is a custom plan donation
Expand Down Expand Up @@ -184,6 +195,22 @@ private function handleSubscription(PaymentForm $paymentForm, $postData, $metada

$sessionParams['subscription_data'] = $subscriptionData;

// One time fees
if ($paymentForm->subscriptionType == SubscriptionType::SINGLE_PLAN){
if ($paymentForm->singlePlanSetupFee){
$oneTineFee = [
'amount' => Stripe::$app->orders->convertToCents($paymentForm->singlePlanSetupFee, $paymentForm->currency),
'currency' => $paymentForm->currency,
'name' => $settings->oneTimeSetupFeeLabel,
'quantity' => 1
];
}
}

if ($oneTineFee){
$sessionParams['line_items'] = [$oneTineFee];
}

return $sessionParams;
}

Expand Down
4 changes: 3 additions & 1 deletion src/services/PaymentForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,11 @@ public function getLanguageOptions()
'de' => 'German',
'it' => 'Italian',
'ja' => 'Japanese',
'no' => 'Norwegian',
'nb' => 'Norwegian',
'es' => 'Spanish',
'sv' => 'Swedish',
'pt' => 'Portuguese',
'pl' => 'Polish'
];

return $languages;
Expand Down
16 changes: 0 additions & 16 deletions src/services/PaymentIntents.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,11 @@ public function createOrderFromSubscription($subscription, $checkoutSession)
{
$metadata = $subscription['metadata'];
$formId = $metadata['stripe_payments_form_id'];
$paymentForm = StripePlugin::$app->paymentForms->getPaymentFormById($formId);
$userId = $metadata['stripe_payments_user_id'];
$quantity = $metadata['stripe_payments_quantity'];
$testMode = !$checkoutSession['livemode'];
$customer = Stripe::$app->customers->getStripeCustomer($subscription['customer']);
Stripe::$app->customers->registerCustomer($customer, $testMode);
$planId = $subscription['plan']['id'];

if ($paymentForm->subscriptionType == SubscriptionType::SINGLE_PLAN){
if ($paymentForm->singlePlanSetupFee){
// @todo One-time setup fee for SCA is not supported yet.
// StripePlugin::$app->orders->addOneTimeSetupFee($customer, $paymentForm->singlePlanSetupFee, $paymentForm);
}
}

if ($paymentForm->subscriptionType == SubscriptionType::MULTIPLE_PLANS){
$setupFee = StripePlugin::$app->orders->getSetupFeeFromMatrix($planId, $paymentForm);
if ($setupFee){
// StripePlugin::$app->orders->addOneTimeSetupFee($customer, $setupFee, $paymentForm);
}
}

$invoice = Stripe::$app->customers->getStripeInvoice($subscription['latest_invoice']);

Expand Down
10 changes: 10 additions & 0 deletions src/templates/settings/default.twig
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ If you instead want to cancel the subscription at the end of the current billing
value: settings.chargeDescription,
errors: settings.getErrors('chargeDescription')
}) }}

<hr>
{{ forms.textField({
label: "One time set-up fee label"|t('enupal-stripe'),
instructions: "It is displayed when a one-time setup fee to a subscription and the new Stripe Checkout is enabled"|t('enupal-stripe'),
id: 'oneTimeSetupFeeLabel',
name: 'oneTimeSetupFeeLabel',
value: settings.oneTimeSetupFeeLabel,
errors: settings.getErrors('oneTimeSetupFeeLabel')
}) }}
{% endnamespace %}

</div>
Expand Down

0 comments on commit d77ed33

Please sign in to comment.