Skip to content

Instantly share code, notes, and snippets.

@mmichael0413
Last active January 4, 2016 09:09
Show Gist options
  • Save mmichael0413/8599791 to your computer and use it in GitHub Desktop.
Save mmichael0413/8599791 to your computer and use it in GitHub Desktop.
Stripe custom Checkout form and corresponding controller action and Payment model method
<!-- STRIPE CUSTOM CHECKOUT FORM -->
<div>
<script src="https://checkout.stripe.com/checkout.js"></script>
<a class="cta-red-copy" id="customButton">Pay with Card</a>
<script>
var amountValue;
var handler = StripeCheckout.configure({
key: '<%= Rails.configuration.stripe[:publishable_key] %>',
image: '/assets/fp_square_logo.png',
token: function(token, args) {
// Use the token to create the charge with a server-side script.
$.ajax({
type: 'POST',
data: { 'token' : token, 'amount' : amountValue },
url: '/application/<%= @submission.id %>/payments/credit_card_charge',
success: function(data) {
location.reload();
$('body').scrollTop(0);
$('#stripeSuccess').show();
},
error: function(data) {
$('#stripeError').show();
}
});
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
amountValue = $('#payment_amount').val();
var pmtInputBox = document.getElementById('payment_amount');
var errorText = document.getElementById('stripeErrorText');
if (isNaN(amountValue)) {
pmtInputBox.style.border='1px solid red';
errorText.style.display='';
} else if (amountValue < 500) {
pmtInputBox.style.border='1px solid red';
errorText.style.display='';
}
else {
var dim = document.getElementById('stripeProcessing');
amountValue = amountValue * 100;
// Open Checkout with further options
handler.open({
name: 'Fullbridge, Inc.',
description: 'The Fullbridge Program Tuition',
amount: amountValue,
closed: dim.style.display='none'
});
}
e.preventDefault();
});
</script>
</div>
def self.new_stripe_charge(submission, charge)
payment = submission.payments.new(
name: 'Stripe Credit Card Charge',
amount: charge.amount.to_f / 100, # in dollars
is_successful: true,
_type: 'Credit Card',
stripe_charge_id: charge.id,
stripe_is_paid: charge.paid,
stripe_is_live: charge.livemode,
stripe_is_refunded: charge.refunded,
stripe_card_id: charge.card.id,
stripe_card_last4: charge.card.last4,
stripe_card_type: charge.card.type,
stripe_card_exp_month: charge.card.exp_month,
stripe_card_exp_year: charge.card.exp_year,
stripe_customer_id: charge.customer
)
payment.save!
payment
end
class PaymentsController < ApplicationController
helper_method :submission
# POST /application/:submission_id/payments/credit_card_charge
def credit_card_charge
amount = params[:amount].to_i
profile = submission.profile
if profile.stripe_customer_id
customer = Stripe::Customer.retrieve(profile.stripe_customer_id)
else
customer = Stripe::Customer.create(email: profile.email, card: params[:token])
profile.stripe_customer_id = customer.id
profile.save
end
charge = Stripe::Charge.create(customer: customer.id, amount: amount, description: 'The Fullbridge Program Tuition', currency: 'usd')
if charge.paid
payment = Payment.new_stripe_charge(submission, charge)
end
redirect_to submission_tuition_url(submission), notice: "Thank you! Your payment for $#{payment.amount} was processed successfully."
rescue Stripe::CardError => e
redirect_to request.referer, notice: e.message
end
# HELPER METHOD
def submission
@submission ||= Submission.find(params[:submission_id])
end
end
@mmichael0413
Copy link
Author

I was thinking of moving the if-else for the profile.stripe_customer_id piece into a separate method and wrapping that as well as the Payment.new_stripe_charge method in a delayed_job (background job).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment