My question is very similar to Looking to Submit Rails Form Only After Successful Stripe Checkout Payment. I tried the solution accepted there but it did not work for me.
I am using Rails 4.2, Stripe integration is already implemented and I added a Survey form to the same page as the Stripe payment page. The Survey form displays correctly and saves if I use the submit button from the partial. But I am trying to submit the form (create and save) if the form and the credit card payment is valid using one submit button on the Stripe payment page.
It looks like the Stripe payment form is being submitted through js.
payments/partials/_ticket_payment_panel.html
<form action='/payments/pay_for/receive_payments' id="ticket-form" method="POST">
// form fields
</form>
<% content_for :scripts do %>
<script type="text/javascript" charset="utf-8">
$(function () {
var $form = $('#ticket-form');
//var $survey = $('#survey-form');
$form.submit(function (event) {
// Disable the submit button to prevent repeated clicks:
$form.find('.submit').prop('disabled', true);
// Request a token from Stripe:
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from being submitted:
return false;
});
});
function stripeResponseHandler(status, response) {
var $form = $('#ticket-form');
if (response.error) {
// Show the errors on the form
$('.submit').removeProp('disabled');
} else {
// response contains id and card, which contains additional card details
var token = response.id;
// Insert the token into the form so it gets submitted to the server
//execute this block only if plan exists -> helpers/payments/application_helper.rb
$form.get(0).submit();
}
}
</script>
<% end %>
The Stripe submit works but it does not submit the Survey form. I tried adding var $survey = $('#survey-form'); and submit in the js. But it either only submits the form and not the Stripe payment or vice versa depending on which comes first.
I also tried to add the save in the controller.
pay_for_controller
def receive_payments
@survey = Survey.new
@survey.userprofile_id = current_user.userprofile.id
skills = params[:skills]
work_type = params[:work_type]
@survey.save
//credit card attempts, delayed jobs code
end
This is my survey form
<%= bootstrap_form_for(@survey, :html => { :id => "survey-form" } ) do |f| %>
//form fields
<%= f.submit "Apply", class: 'action-btn btn-rounded btn-large' %>
<% end %>
Currently, individually they submit/save correctly so I think the link between the model and controller are fine. But I can't seem to alter the js code or controller to get both the Survey form and the Stripe payment to save.
surveyform data via AJAX call and submit stripe form upon response from your AJAX call. - Zicosurveytable and credit card info toTickettable? 2. I will try this suggestion out, does this mean there are 2 buttons? One to submitsurveyform data via AJAX. Then show stripe form upon successfulsurveyform submit and submit stripe form? - teresa