Skip to content

Commit

Permalink
Keep old payment form around for now
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Oct 4, 2023
1 parent e779b6f commit b105031
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/gateways/PaymentIntents.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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 +82,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();

Check failure on line 105 in src/gateways/PaymentIntents.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

PHPDoc tag @var for variable $user contains unknown class craft\commerce\stripe\gateways\CustomerBehavior.
if (!$billingAddress && $user) {
$billingAddress = $user->getPrimaryBillingAddress();

Check failure on line 107 in src/gateways/PaymentIntents.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Call to method getPrimaryBillingAddress() on an unknown class craft\commerce\stripe\gateways\CustomerBehavior.
}
}
} 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>
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 b105031

Please sign in to comment.