Skip to content

Commit a8bc875

Browse files
author
epriestley
committedApr 25, 2013
Clean up rendering of credit card form
Summary: General cleanup and separation into generic vs Stripe blocks of code. - There was an old CC form view for Stripe stuff that I never cleaned up; clean that up. - Move non-Stripe CC form rendering into a base class (Balanced can reuse it). - Move non-Stripe CC form JS into a shareable class. - Simplify JS a bit (JX.Workflow can add extra parameters to a request, so we don't need hidden inputs). - Genericize CSS. - Depend on Stripe JS directly, if they're down we're not going to be able to add cards anyway. Ref T2787. Test Plan: Hit all Stripe errors and added new cards. Reviewers: btrahan, chad Reviewed By: btrahan CC: aran Maniphest Tasks: T2787 Differential Revision: https://secure.phabricator.com/D5758
1 parent 9c43029 commit a8bc875

10 files changed

+205
-271
lines changed
 

‎src/__celerity_resource_map__.php

+22-19
Original file line numberDiff line numberDiff line change
@@ -2258,14 +2258,15 @@
22582258
),
22592259
'javelin-behavior-stripe-payment-form' =>
22602260
array(
2261-
'uri' => '/res/e4149d37/rsrc/js/application/phortune/behavior-stripe-payment-form.js',
2261+
'uri' => '/res/62dc91b4/rsrc/js/application/phortune/behavior-stripe-payment-form.js',
22622262
'type' => 'js',
22632263
'requires' =>
22642264
array(
22652265
0 => 'javelin-behavior',
22662266
1 => 'javelin-dom',
22672267
2 => 'javelin-json',
22682268
3 => 'javelin-workflow',
2269+
4 => 'phortune-credit-card-form',
22692270
),
22702271
'disk' => '/rsrc/js/application/phortune/behavior-stripe-payment-form.js',
22712272
),
@@ -3588,6 +3589,26 @@
35883589
),
35893590
'disk' => '/rsrc/css/application/pholio/pholio-inline-comments.css',
35903591
),
3592+
'phortune-credit-card-form' =>
3593+
array(
3594+
'uri' => '/res/7be5799a/rsrc/js/application/phortune/phortune-credit-card-form.js',
3595+
'type' => 'js',
3596+
'requires' =>
3597+
array(
3598+
0 => 'javelin-install',
3599+
1 => 'javelin-dom',
3600+
),
3601+
'disk' => '/rsrc/js/application/phortune/phortune-credit-card-form.js',
3602+
),
3603+
'phortune-credit-card-form-css' =>
3604+
array(
3605+
'uri' => '/res/563c8c6d/rsrc/css/application/phortune/phortune-credit-card-form.css',
3606+
'type' => 'css',
3607+
'requires' =>
3608+
array(
3609+
),
3610+
'disk' => '/rsrc/css/application/phortune/phortune-credit-card-form.css',
3611+
),
35913612
'phrequent-css' =>
35923613
array(
35933614
'uri' => '/res/9d6f3eb7/rsrc/css/application/phrequent/phrequent.css',
@@ -3921,24 +3942,6 @@
39213942
),
39223943
'disk' => '/rsrc/css/sprite-tokens.css',
39233944
),
3924-
'stripe-core' =>
3925-
array(
3926-
'uri' => '/res/fc74303d/rsrc/externals/stripe-js/stripe_core.js',
3927-
'type' => 'js',
3928-
'requires' =>
3929-
array(
3930-
),
3931-
'disk' => '/rsrc/externals/stripe-js/stripe_core.js',
3932-
),
3933-
'stripe-payment-form-css' =>
3934-
array(
3935-
'uri' => '/res/634a6371/rsrc/css/application/phortune/stripe-payment-form.css',
3936-
'type' => 'css',
3937-
'requires' =>
3938-
array(
3939-
),
3940-
'disk' => '/rsrc/css/application/phortune/stripe-payment-form.css',
3941-
),
39423945
'syntax-highlighting-css' =>
39433946
array(
39443947
'uri' => '/res/cb3b9dc0/rsrc/css/core/syntax.css',

‎src/__phutil_library_map__.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1583,11 +1583,13 @@
15831583
'PhortuneCart' => 'applications/phortune/storage/PhortuneCart.php',
15841584
'PhortuneCharge' => 'applications/phortune/storage/PhortuneCharge.php',
15851585
'PhortuneController' => 'applications/phortune/controller/PhortuneController.php',
1586+
'PhortuneCreditCardForm' => 'applications/phortune/view/PhortuneCreditCardForm.php',
15861587
'PhortuneDAO' => 'applications/phortune/storage/PhortuneDAO.php',
15871588
'PhortuneLandingController' => 'applications/phortune/controller/PhortuneLandingController.php',
15881589
'PhortuneMonthYearExpiryControl' => 'applications/phortune/control/PhortuneMonthYearExpiryControl.php',
15891590
'PhortuneMultiplePaymentProvidersException' => 'applications/phortune/exception/PhortuneMultiplePaymentProvidersException.php',
15901591
'PhortuneNoPaymentProviderException' => 'applications/phortune/exception/PhortuneNoPaymentProviderException.php',
1592+
'PhortuneNotImplementedException' => 'applications/phortune/exception/PhortuneNotImplementedException.php',
15911593
'PhortunePaymentMethod' => 'applications/phortune/storage/PhortunePaymentMethod.php',
15921594
'PhortunePaymentMethodEditController' => 'applications/phortune/controller/PhortunePaymentMethodEditController.php',
15931595
'PhortunePaymentMethodListController' => 'applications/phortune/controller/PhortunePaymentMethodListController.php',
@@ -1604,7 +1606,6 @@
16041606
'PhortuneProductTransactionQuery' => 'applications/phortune/query/PhortuneProductTransactionQuery.php',
16051607
'PhortuneProductViewController' => 'applications/phortune/controller/PhortuneProductViewController.php',
16061608
'PhortunePurchase' => 'applications/phortune/storage/PhortunePurchase.php',
1607-
'PhortuneStripePaymentFormView' => 'applications/phortune/view/PhortuneStripePaymentFormView.php',
16081609
'PhortuneStripePaymentProvider' => 'applications/phortune/provider/PhortuneStripePaymentProvider.php',
16091610
'PhortuneTestExtraPaymentProvider' => 'applications/phortune/provider/__tests__/PhortuneTestExtraPaymentProvider.php',
16101611
'PhortuneTestPaymentProvider' => 'applications/phortune/provider/PhortuneTestPaymentProvider.php',
@@ -3309,6 +3310,7 @@
33093310
'PhortuneMonthYearExpiryControl' => 'AphrontFormControl',
33103311
'PhortuneMultiplePaymentProvidersException' => 'Exception',
33113312
'PhortuneNoPaymentProviderException' => 'Exception',
3313+
'PhortuneNotImplementedException' => 'Exception',
33123314
'PhortunePaymentMethod' =>
33133315
array(
33143316
0 => 'PhortuneDAO',
@@ -3332,7 +3334,6 @@
33323334
'PhortuneProductTransactionQuery' => 'PhabricatorApplicationTransactionQuery',
33333335
'PhortuneProductViewController' => 'PhortuneController',
33343336
'PhortunePurchase' => 'PhortuneDAO',
3335-
'PhortuneStripePaymentFormView' => 'AphrontView',
33363337
'PhortuneStripePaymentProvider' => 'PhortunePaymentProvider',
33373338
'PhortuneTestExtraPaymentProvider' => 'PhortunePaymentProvider',
33383339
'PhortuneTestPaymentProvider' => 'PhortunePaymentProvider',

‎src/applications/phortune/provider/PhortunePaymentProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ abstract protected function executeCharge(
8181
PhortuneCharge $charge);
8282

8383

84-
8584
/* -( Adding Payment Methods )--------------------------------------------- */
8685

8786

@@ -112,4 +111,5 @@ public function renderCreatePaymentMethodForm(
112111
throw new PhortuneNotImplementedException($this);
113112
}
114113

114+
115115
}

‎src/applications/phortune/provider/PhortuneStripePaymentProvider.php

+12-66
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public function createPaymentMethodFromRequest(
8585

8686
$card_errors = $request->getStr('cardErrors');
8787
$stripe_token = $request->getStr('stripeToken');
88+
89+
$errors = array();
8890
if ($card_errors) {
8991
$raw_errors = json_decode($card_errors);
9092
$errors = $this->parseRawCreatePaymentMethodErrors($raw_errors);
@@ -140,77 +142,21 @@ public function renderCreatePaymentMethodForm(
140142
AphrontRequest $request,
141143
array $errors) {
142144

143-
$e_card_number = isset($errors['number']) ? pht('Invalid') : true;
144-
$e_card_cvc = isset($errors['cvc']) ? pht('Invalid') : true;
145-
$e_card_exp = isset($errors['exp']) ? pht('Invalid') : null;
146-
147-
$user = $request->getUser();
148-
149-
$form_id = celerity_generate_unique_node_id();
150-
require_celerity_resource('stripe-payment-form-css');
151-
require_celerity_resource('aphront-tooltip-css');
152-
Javelin::initBehavior('phabricator-tooltips');
153-
154-
$form = id(new AphrontFormView())
155-
->setID($form_id)
156-
->appendChild(
157-
id(new AphrontFormMarkupControl())
158-
->setLabel('')
159-
->setValue(
160-
javelin_tag(
161-
'div',
162-
array(
163-
'class' => 'credit-card-logos',
164-
'sigil' => 'has-tooltip',
165-
'meta' => array(
166-
'tip' => 'We support Visa, Mastercard, American Express, '.
167-
'Discover, JCB, and Diners Club.',
168-
'size' => 440,
169-
)
170-
))))
171-
->appendChild(
172-
id(new AphrontFormTextControl())
173-
->setLabel('Card Number')
174-
->setDisableAutocomplete(true)
175-
->setSigil('number-input')
176-
->setError($e_card_number))
177-
->appendChild(
178-
id(new AphrontFormTextControl())
179-
->setLabel('CVC')
180-
->setDisableAutocomplete(true)
181-
->setSigil('cvc-input')
182-
->setError($e_card_cvc))
183-
->appendChild(
184-
id(new PhortuneMonthYearExpiryControl())
185-
->setLabel('Expiration')
186-
->setUser($user)
187-
->setError($e_card_exp))
188-
->appendChild(
189-
javelin_tag(
190-
'input',
191-
array(
192-
'hidden' => true,
193-
'name' => 'stripeToken',
194-
'sigil' => 'stripe-token-input',
195-
)))
196-
->appendChild(
197-
javelin_tag(
198-
'input',
199-
array(
200-
'hidden' => true,
201-
'name' => 'cardErrors',
202-
'sigil' => 'card-errors-input'
203-
)));
204-
205-
require_celerity_resource('stripe-core');
145+
$ccform = id(new PhortuneCreditCardForm())
146+
->setUser($request->getUser())
147+
->setCardNumberError(isset($errors['number']) ? pht('Invalid') : true)
148+
->setCardCVCError(isset($errors['cvc']) ? pht('Invalid') : true)
149+
->setCardExpirationError(isset($errors['exp']) ? pht('Invalid') : null)
150+
->addScript('https://js.stripe.com/v2/');
151+
206152
Javelin::initBehavior(
207153
'stripe-payment-form',
208154
array(
209-
'stripePublishKey' => $this->getPublishableKey(),
210-
'root' => $form_id,
155+
'stripePublishableKey' => $this->getPublishableKey(),
156+
'formID' => $ccform->getFormID(),
211157
));
212158

213-
return $form;
159+
return $ccform->buildForm();
214160
}
215161

216162

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
final class PhortuneCreditCardForm {
4+
5+
private $formID;
6+
private $scripts = array();
7+
private $user;
8+
9+
private $cardNumberError;
10+
private $cardCVCError;
11+
private $cardExpirationError;
12+
13+
public function setUser(PhabricatorUser $user) {
14+
$this->user = $user;
15+
return $this;
16+
}
17+
18+
public function setCardExpirationError($card_expiration_error) {
19+
$this->cardExpirationError = $card_expiration_error;
20+
return $this;
21+
}
22+
23+
public function setCardCVCError($card_cvc_error) {
24+
$this->cardCVCError = $card_cvc_error;
25+
return $this;
26+
}
27+
28+
public function setCardNumberError($card_number_error) {
29+
$this->cardNumberError = $card_number_error;
30+
return $this;
31+
}
32+
33+
public function addScript($script_uri) {
34+
$this->scripts[] = $script_uri;
35+
return $this;
36+
}
37+
38+
public function getFormID() {
39+
if (!$this->formID) {
40+
$this->formID = celerity_generate_unique_node_id();
41+
}
42+
return $this->formID;
43+
}
44+
45+
public function buildForm() {
46+
$form_id = $this->getFormID();
47+
48+
require_celerity_resource('phortune-credit-card-form-css');
49+
require_celerity_resource('phortune-credit-card-form');
50+
51+
require_celerity_resource('aphront-tooltip-css');
52+
Javelin::initBehavior('phabricator-tooltips');
53+
54+
$form = new AphrontFormView();
55+
56+
foreach ($this->scripts as $script) {
57+
$form->appendChild(
58+
phutil_tag(
59+
'script',
60+
array(
61+
'type' => 'text/javascript',
62+
'src' => $script,
63+
)));
64+
}
65+
66+
$form
67+
->setID($form_id)
68+
->appendChild(
69+
id(new AphrontFormMarkupControl())
70+
->setLabel('')
71+
->setValue(
72+
javelin_tag(
73+
'div',
74+
array(
75+
'class' => 'credit-card-logos',
76+
'sigil' => 'has-tooltip',
77+
'meta' => array(
78+
'tip' => 'We support Visa, Mastercard, American Express, '.
79+
'Discover, JCB, and Diners Club.',
80+
'size' => 440,
81+
)
82+
))))
83+
->appendChild(
84+
id(new AphrontFormTextControl())
85+
->setLabel('Card Number')
86+
->setDisableAutocomplete(true)
87+
->setSigil('number-input')
88+
->setError($this->cardNumberError))
89+
->appendChild(
90+
id(new AphrontFormTextControl())
91+
->setLabel('CVC')
92+
->setDisableAutocomplete(true)
93+
->setSigil('cvc-input')
94+
->setError($this->cardCVCError))
95+
->appendChild(
96+
id(new PhortuneMonthYearExpiryControl())
97+
->setLabel('Expiration')
98+
->setUser($this->user)
99+
->setError($this->cardExpirationError));
100+
101+
return $form;
102+
}
103+
}

0 commit comments

Comments
 (0)
Failed to load comments.