Skip to content

Instantly share code, notes, and snippets.

@iwarner
Last active October 16, 2023 06:51
Show Gist options
  • Save iwarner/8866483 to your computer and use it in GitHub Desktop.
Save iwarner/8866483 to your computer and use it in GitHub Desktop.
Simple Cart Stripe Installation
/**
* Stripe
*
* @param object opts Options Object
* @see https://support.stripe.com/questions/which-currencies-does-stripe-support
* @todo Make functions variables in this
*/
Stripe : function ( opts )
{
// Debug
console.log( simpleCart.currency() );
console.log( simpleCart.quantity() );
console.log( simpleCart.toCurrency( simpleCart.grandTotal() ) );
// Check that the total in the checkout is greater than the limit
if ( settings.limit >= simpleCart.grandTotal() ) {
alert( "Sorry the minimum order is " + simpleCart.currency().symbol + settings.limit + ". \r\nPlease order some more." );
return false;
}
// Create the Total
total = $('<div />').html( simpleCart.toCurrency( simpleCart.grandTotal() ) ).text();
// Stripe Key is required
if ( ! opts.key || ! opts.image || ! opts.name || ! opts.url ) {
return simpleCart.error( "No Stripe Key, image, url or name is provided" );
}
// Stripe Handler
var handler = StripeCheckout.configure(
{
billingAddress : true,
closed : function( ) {
console.log( "Closed" );
},
currency : simpleCart.currency().code,
image : opts.image,
key : opts.key,
panelLabel : "Buy {{amount}}",
opened : function( ) {
console.log( "Opened" );
},
shippingAddress : true,
token : function( token, args ) {
// Use the token to create the charge with a server-side script.
// Ajax POST
$.ajax( {
cache : false,
contentType : "application/json; charset=utf-8",
crossDomain : true,
data : JSON.stringify( { "details" : { "amount" : simpleCart.grandTotal() * 100, "currency" : simpleCart.currency().code } , "token" : token, "args" : args } ),
dataType : "json",
type : "POST",
url : opts.url
// Done - check for errors - empty cart if success
} ).done( function( data, status, xhr ) {
// Debug
console.log( "DONE : ", data, status, xhr );
// Empty the Cart
simpleCart.empty();
// Fail - add message
} ).fail( function( xhr, status, error ) {
// Debug
console.log( "FAIL : ", xhr, status, error );
// Porbably nothing required here
} ).always( function( data, status, error ) {
// Debug
console.log( "ALWAYS : ", data, status, error );
} );
// Debug
console.log( token, args );
}
});
// Open the Stripe Checkout Form
handler.open( {
name : opts.name,
description : simpleCart.quantity() + " items @ " + total,
amount : simpleCart.grandTotal() * 100
} );
// Return the data for the checkout form
// Stripe does not require this - return null
return {
action : null
, method : null
, data : false
};
},
# Stripe Checkout
checkout :
type : "Stripe"
image : "http://placehold.it/128.gif/aabbdd/000&text=LOGO"
key : "pk_test_"
name : "DryKISS Ltd"
url : "http://localhost:8080/charge.php"
# Setting the Cart Columns for the sidebar cart display.
cartColumns : [
# A custom cart column for putting the quantity and increment
# and decrement items in one div for easier styling.
{ view : ( item, column ) ->
"<span>" + item.get( "quantity" ) + "</span>
<div>
<a href='javascript:;' class='simpleCart_increment'>
<img src='/assets/images/site/increment.png' title='+1' alt='arrow up'>
</a>
<a href='javascript:;' class='simpleCart_decrement'>
<img src='/assets/images/site/decrement.png' title='-1' alt='arrow down'>
</a>
</div>"
attr : "custom" },
# Name of the item
{ attr : "name", label : false }
# Subtotal of that row (quantity of that item * the price)
{ view : "currency", attr : "total", label : false }
]
cartStyle : "div"
@iwarner
Copy link
Author

iwarner commented Feb 15, 2014

Updated with latest options - this is inline with the widget in the SimpleCart Example. Added the start of a limit function so the checkout does not fire unless the total is over a certain amount

@zanematthew
Copy link

Why not make this a repo?

@iwarner
Copy link
Author

iwarner commented Mar 6, 2014

I think I may form Simple Cart then and stick this into the development - seeing as SC has not been worked on in anger for a while.

@zanematthew
Copy link

Maybe I'm missing something, but how does your gist handle the Stripe cc form? https://stripe.com/checkout shouldn't all that be handled by Stripe? If so at what point does simpleCart pass on the cart contents to Stripe?

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