Skip to content

Commit

Permalink
Adds one-time setup fee to subscriptions when using new Stripe checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
andrelopez committed Sep 7, 2019
1 parent e234472 commit a04a483
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
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
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 a04a483

Please sign in to comment.