Skip to content

Commit

Permalink
Merge branch 'release/4.0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Oct 25, 2023
2 parents e75710f + dbc7e65 commit 398095f
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Stripe for Craft Commerce

## 4.0.1.1 - 2023-10-25

- Restored support for backend payments using the old payment form.
- Fixed missing icon.

## 4.0.1 - 2023-09-28

- Fixed a PHP error that occurred when switching a subscription’s plan.
Expand Down
50 changes: 50 additions & 0 deletions src/gateways/PaymentIntents.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use craft\commerce\base\Plan as BasePlan;
use craft\commerce\base\RequestResponseInterface;
use craft\commerce\base\SubscriptionResponseInterface;
use craft\commerce\behaviors\CustomerBehavior;
use craft\commerce\elements\Subscription;
use craft\commerce\errors\PaymentSourceCreatedLaterException;
use craft\commerce\errors\SubscriptionException;
Expand All @@ -31,6 +32,7 @@
use craft\commerce\stripe\responses\CheckoutSessionResponse;
use craft\commerce\stripe\responses\PaymentIntentResponse;
use craft\commerce\stripe\web\assets\elementsform\ElementsFormAsset;
use craft\commerce\stripe\web\assets\intentsform\IntentsFormAsset;
use craft\elements\User;
use craft\helpers\Json;
use craft\helpers\StringHelper;
Expand Down Expand Up @@ -81,6 +83,54 @@ public function showPaymentFormSubmitButton(): bool
return false;
}

/**
* @inheritdoc
*/
public function getOldPaymentFormHtml(array $params): ?string
{
$defaults = [
'gateway' => $this,
'paymentForm' => $this->getPaymentFormModel(),
'scenario' => 'payment',
'handle' => $this->handle,
];

$params = array_merge($defaults, $params);

// If there's no order passed, add the current cart if we're not messing around in backend.
if (!isset($params['order']) && !Craft::$app->getRequest()->getIsCpRequest()) {
if ($cart = Commerce::getInstance()->getCarts()->getCart()) {
$billingAddress = $cart->getBillingAddress();

/** @var User|CustomerBehavior|null $user */
$user = $cart->getCustomer();
if (!$billingAddress && $user) {
$billingAddress = $user->getPrimaryBillingAddress();
}
}
} else {
$billingAddress = $params['order']->getBillingAddress();
}

if ($billingAddress) {
$params['billingAddress'] = $billingAddress;
}

$view = Craft::$app->getView();

$previousMode = $view->getTemplateMode();
$view->setTemplateMode(View::TEMPLATE_MODE_CP);

$view->registerScript('', View::POS_END, ['src' => 'https://js.stripe.com/v3/']); // we need this to load at end of body
$view->registerAssetBundle(IntentsFormAsset::class);

$html = $view->renderTemplate('commerce-stripe/paymentForms/oldIntentsForm', $params);

$view->setTemplateMode($previousMode);

return $html;
}

/**
* @inheritdoc
*/
Expand Down
108 changes: 108 additions & 0 deletions src/templates/paymentForms/oldIntentsForm.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{% set paymentFormNamespace = handle|commercePaymentFormNamespace %}
<div class="stripe-payment-intents-form"
data-payment-form-namespace="{{ paymentFormNamespace }}"
data-publishablekey="{{ parseEnv(gateway.getPublishableKey()) }}"
{% if clientSecret is defined %}data-client-secret="{{clientSecret}}" {% endif %}
{% if scenario is defined %}data-scenario="{{scenario}}" {% endif %}
>

<div class="payment-form-fields hidden">
{% namespace paymentFormNamespace %}
{% import "_includes/forms" as forms %}

<fieldset class="card-holder">
<legend>{{ 'Card Holder'|t('commerce') }}</legend>

<div class="md:flex md:-mx-4">
<!-- Card Holder Name -->
<div class="md:w-1/2 md:mx-4 my-2">
{{ forms.text({
name: 'firstName',
maxlength: 70,
placeholder: "First Name"|t('commerce'),
autocomplete: false,
class: 'card-holder-first-name'~(paymentForm.getErrors('firstName') ? ' error'),

required: true,
}) }}
</div>

<div class="md:w-1/2 md:mx-4 my-2">
{{ forms.text({
name: 'lastName',
maxlength: 70,
placeholder: "Last Name"|t('commerce'),
autocomplete: false,
class: 'card-holder-last-name'~(paymentForm.getErrors('lastName') ? ' error'),

required: true,
}) }}
</div>
</div>

{% set errors = [] %}
{% for attributeKey in ['firstName', 'lastName'] %}
{% set errors = errors|merge(paymentForm.getErrors(attributeKey)) %}
{% endfor %}

{{ forms.errorList(errors) }}
</fieldset>

<!-- Card Number -->
<fieldset class="card-data">
<legend>{{ 'Card'|t('commerce') }}</legend>

<div>
<div>
{{ forms.text({
name: 'number',
maxlength: 19,
placeholder: "Card Number"|t('commerce'),
autocomplete: false,
class: 'card-number'~(paymentForm.getErrors('number') ? ' error'),

}) }}

</div>

<div>
{{ forms.text({
class: 'card-expiry'~(paymentForm.getErrors('month') or paymentForm.getErrors('year') ? ' error'),
type: 'text',
name: 'expiry',
placeholder: "MM"|t('commerce')~' / '~"YYYY"|t('commerce'),

}) }}

{{ forms.text({
type: 'tel',
name: 'cvv',
placeholder: "CVV"|t('commerce'),
class: 'card-cvc'~(paymentForm.getErrors('cvv') ? ' error'),

}) }}
</div>
</div>

{% set errors = [] %}
{% for attributeKey in ['number', 'month', 'year', 'cvv'] %}
{% set errors = errors|merge(paymentForm.getErrors(attributeKey)) %}
{% endfor %}

{{ forms.errorList(errors) }}

</fieldset>
{% endnamespace %}
</div>

<div class="card-errors" role="alert"></div>
{% if billingAddress is defined %}
<div class="stripe-address hidden">
<input type="hidden" name="stripe-line1" value="{{ billingAddress.addressLine1 }}" />
<input type="hidden" name="stripe-postal-code" value="{{ billingAddress.getPostalCode() }}" />
<input type="hidden" name="stripe-city" value="{{ billingAddress.getLocality() }}" />
<input type="hidden" name="stripe-country" value="{{ billingAddress.getCountryCode() }}" />
<input type="hidden" name="stripe-state" value="{{ billingAddress.getAdministrativeArea() }}" />
</div>
{% endif %}
</div>
2 changes: 1 addition & 1 deletion src/utilities/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function id(): string
*/
public static function iconPath(): ?string
{
return Craft::getAlias('@vendor') . '/craftcms/stripe/src/icon-mask.svg';
return Craft::getAlias('@vendor') . '/craftcms/commerce-stripe/src/icon-mask.svg';
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/web/assets/intentsform/IntentsFormAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/

namespace craft\commerce\stripe\web\assets\intentsform;

use yii\web\AssetBundle;
use yii\web\JqueryAsset;

/**
* Asset bundle for the Payment Form
*/
class IntentsFormAsset extends AssetBundle
{
/**
* @inheritdoc
*/
public function init()
{
$this->sourcePath = __DIR__;

$this->css = [
'css/paymentForm.css',
];

$this->js = [
'js/paymentForm.js',
];

$this->depends = [
JqueryAsset::class,
];

parent::init();
}
}
3 changes: 3 additions & 0 deletions src/web/assets/intentsform/css/paymentForm.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.stripe-payment-intents-form {
width: 310px;
}
Loading

0 comments on commit 398095f

Please sign in to comment.