Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Stripe
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Extensions
Stripe
Commits
a976837a
Commit
a976837a
authored
9 years ago
by
Jamie McClelland
Browse files
Options
Downloads
Patches
Plain Diff
refactoring to properly load stripe on submission error
See
https://github.com/drastik/com.drastikbydesign.stripe/issues/76
parent
6ba9a51b
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
stripe.php
+49
-42
49 additions, 42 deletions
stripe.php
with
49 additions
and
42 deletions
stripe.php
+
49
−
42
View file @
a976837a
...
...
@@ -147,15 +147,15 @@ function stripe_civicrm_validateForm($formName, &$fields, &$files, &$form, &$err
* @param $form - reference to the form object
*/
function
stripe_civicrm_buildForm
(
$formName
,
&
$form
)
{
if
(
isset
(
$form
->
_paymentProcessor
[
'payment_processor_type'
])
&&
$form
->
_paymentProcessor
[
'payment_processor_type'
]
==
'Stripe'
)
{
$stripe_key
=
stripe_get_key
(
$form
);
if
(
!
empty
(
$stripe_key
))
{
if
(
!
stristr
(
$formName
,
'_Confirm'
)
&&
!
stristr
(
$formName
,
'_ThankYou'
))
{
// This is the 'Main', or first step of the form that collects CC data.
if
(
!
$form
->
elementExists
(
'stripe_token'
))
{
$form
->
setAttribute
(
'class'
,
$form
->
getAttribute
(
'class'
)
.
' stripe-payment-form'
);
$form
->
addElement
(
'hidden'
,
'stripe_token'
,
NULL
,
array
(
'id'
=>
'stripe-token'
));
$form
->
addElement
(
'hidden'
,
'stripe_id'
,
$form
->
_paymentProcessor
[
'id'
],
array
(
'id'
=>
'stripe-id'
));
stripe_add_stripe_js
(
$form
);
}
stripe_add_stripe_js
(
$stripe_key
,
$form
);
}
else
{
// This is a Confirm or Thank You (completed) form.
...
...
@@ -167,8 +167,6 @@ function stripe_civicrm_buildForm($formName, &$form) {
if
(
!
empty
(
$params
[
'stripe_token'
]))
{
// Stash the token (including its value) in Confirm, in case they go backwards.
$form
->
addElement
(
'hidden'
,
'stripe_token'
,
$params
[
'stripe_token'
],
array
(
'id'
=>
'stripe-token'
));
// Stash stripe payment processor id
$form
->
addElement
(
'hidden'
,
'stripe_id'
,
$form
->
_paymentProcessor
[
'id'
],
array
(
'id'
=>
'stripe-id'
));
}
}
}
...
...
@@ -180,14 +178,14 @@ function stripe_civicrm_buildForm($formName, &$form) {
'CRM_Member_Form_Membership'
,
'CRM_Member_Form_MembershipRenewal'
);
if
(
in_array
(
$formName
,
$backendForms
)
&&
!
empty
(
$
form
->
_processors
))
{
if
(
in_array
(
$formName
,
$backendForms
)
&&
!
empty
(
$
stripe_key
))
{
if
(
!
isset
(
$form
->
_elementIndex
[
'stripe_token'
]))
{
if
(
empty
(
$form
->
_attributes
[
'class'
]))
{
$form
->
_attributes
[
'class'
]
=
''
;
}
$form
->
_attributes
[
'class'
]
.
=
' stripe-payment-form'
;
$form
->
addElement
(
'hidden'
,
'stripe_token'
,
NULL
,
array
(
'id'
=>
'stripe-token'
));
stripe_add_stripe_js
(
$form
);
stripe_add_stripe_js
(
$stripe_key
,
$form
);
}
// Add email field as it would usually be found on donation forms.
if
(
!
isset
(
$form
->
_elementIndex
[
'email'
])
&&
!
empty
(
$form
->
userEmail
))
{
...
...
@@ -197,49 +195,58 @@ function stripe_civicrm_buildForm($formName, &$form) {
}
/**
* Add publishable key and event bindings for Stripe.js.
* Return the stripe api public key (aka password)
*
* If this form could conceiveably now or at any time in the future
* contain a Stripe payment processor, return the api public key for
* that processor.
*/
function
stripe_add_stripe_js
(
$form
)
{
if
(
!
empty
(
$form
->
_paymentProcessor
[
'password'
]))
{
$stripe_pub_key
=
$form
->
_paymentProcessor
[
'password'
];
function
stripe_get_key
(
$form
)
{
// Backend contribution pages have the password embedded in them.
if
(
isset
(
$form
->
_paymentProcessor
[
'password'
]))
{
return
$form
->
_paymentProcessor
[
'password'
];
}
$is_test
=
0
;
if
(
isset
(
$form
->
_mode
))
{
$is_test
=
$form
->
_mode
==
'live'
?
0
:
1
;
}
else
{
// Find Stripe's payproc ID in Civi.
$query_params
=
array
(
1
=>
array
(
'Stripe'
,
'String'
),
);
$stripe_pp_id
=
CRM_Core_DAO
::
singleValueQuery
(
"SELECT id
FROM civicrm_payment_processor_type
WHERE name = %1"
,
$query_params
);
// Find out if the form might use Stripe.
if
(
!
empty
(
$stripe_pp_id
))
{
$mode
=
(
$form
->
_mode
==
'live'
?
0
:
1
);
$query_params
=
array
(
1
=>
array
(
$stripe_pp_id
,
'Integer'
),
2
=>
array
(
$mode
,
'Integer'
),
);
$stripe_procs_query
=
CRM_Core_DAO
::
executeQuery
(
"SELECT name, password
FROM civicrm_payment_processor
WHERE payment_processor_type_id = %1 AND is_test = %2"
,
$query_params
);
// Loop through and see if Stripe is on this form.
while
(
$stripe_procs_query
->
fetch
())
{
foreach
(
$form
->
_processors
as
$form_processor
)
{
if
(
$form_processor
==
$stripe_procs_query
->
name
)
{
$stripe_pub_key
=
$stripe_procs_query
->
password
;
break
;
}
if
(
!
empty
(
$stripe_pub_key
))
{
break
;
}
}
// _paymentProcessors array seems to be the most reliable way to find
// at least payment processor on the form that is a stripe pp.
if
(
isset
(
$form
->
_paymentProcessors
))
{
foreach
(
$form
->
_paymentProcessors
as
$k
=>
$pp
)
{
if
(
$pp
[
'payment_processor_type'
]
==
'Stripe'
)
{
// We have a match.
return
stripe_get_key_for_name
(
$pp
[
'name'
],
$is_test
);
}
}
}
}
/**
* Given a payment processor name, return the pub key.
*/
function
stripe_get_key_for_name
(
$name
,
$is_test
)
{
try
{
$params
=
array
(
'name'
=>
$name
,
'is_test'
=>
$is_test
);
$results
=
civicrm_api3
(
'PaymentProcessor'
,
'get'
,
$params
);
if
(
$results
[
'count'
]
==
1
)
{
$result
=
array_pop
(
$results
[
'values'
]);
return
$result
[
'password'
];
}
}
catch
(
CiviCRM_API3_Exception
$e
)
{
return
NULL
;
}
}
$form
->
addElement
(
'hidden'
,
'stripe_pub_key'
,
$stripe_pub_key
,
array
(
'id'
=>
'stripe-pub-key'
));
/**
* Add publishable key and event bindings for Stripe.js.
*/
function
stripe_add_stripe_js
(
$stripe_key
,
$form
)
{
$form
->
addElement
(
'hidden'
,
'stripe_pub_key'
,
$stripe_key
,
array
(
'id'
=>
'stripe-pub-key'
));
CRM_Core_Resources
::
singleton
()
->
addScriptFile
(
'com.drastikbydesign.stripe'
,
'js/civicrm_stripe.js'
,
0
,
'billing-block'
,
FALSE
);
->
addScriptFile
(
'com.drastikbydesign.stripe'
,
'js/civicrm_stripe.js'
,
0
);
}
/**
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment