Skip to content

Commit

Permalink
Added paypal price formatting and moved checkout event to inside of .…
Browse files Browse the repository at this point in the history
…checkout()
  • Loading branch information
brettwejrowski committed Jun 12, 2012
1 parent 179e2c0 commit 17333ba
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 30 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ Dual licensed under the MIT or GPL licenses.
If you would like to use an older version, you can use a different branch or see them in the
downloads area

v3.0.4 changelog
- added .on alias for .bind
- allowing for multiple event bindings at once with space separated list
- fixed check to bug with switched currency and shipping
v3.0.5 changelog
- moved beforeCheckout event and form sending inside of .checkout() to keep dry
- added price, shipping, tax formatting for paypal checkout


## Quick Start
Expand Down
48 changes: 27 additions & 21 deletions simpleCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,16 @@
if (settings.checkout.type.toLowerCase() === 'custom' && isFunction(settings.checkout.fn)) {
settings.checkout.fn.call(simpleCart,settings.checkout);
} else if (isFunction(simpleCart.checkout[settings.checkout.type])) {
simpleCart.checkout[settings.checkout.type].call(simpleCart,settings.checkout);
var checkoutData = simpleCart.checkout[settings.checkout.type].call(simpleCart,settings.checkout);

// if the checkout method returns data, try to send the form
if( checkoutData.data && checkoutData.action && checkoutData.method ){
// if no one has any objections, send the checkout form
if( false !== simpleCart.trigger('beforeCheckout', [checkoutData.data]) ){
simpleCart.generateAndSendForm( checkoutData );
}
}

} else {
simpleCart.error("No Valid Checkout Method Specified");
}
Expand Down Expand Up @@ -921,8 +930,8 @@
, currency_code : simpleCart.currency().code
, business : opts.email
, rm : opts.method === "GET" ? "0" : "2"
, tax_cart : simpleCart.tax()
, handling_cart : simpleCart.shipping()
, tax_cart : (simpleCart.tax()*1).toFixed(2)
, handling_cart : (simpleCart.shipping()*1).toFixed(2)
, charset : "utf-8"
},
action = opts.sandbox ? "https://www.sandbox.paypal.com/cgi-bin/webscr" : "https://www.paypal.com/cgi-bin/webscr",
Expand All @@ -948,7 +957,7 @@
// basic item data
data["item_name_" + counter] = item.get("name");
data["quantity_" + counter] = item.quantity();
data["amount_" + counter] = item.price();
data["amount_" + counter] = (item.price()*1).toFixed(2);
data["item_number_" + counter] = item.get("item_number") || counter;


Expand All @@ -975,13 +984,14 @@
data["option_index_"+ x] = Math.min(10, optionCount);
});

simpleCart.trigger('beforeCheckout', [data]);

simpleCart.generateAndSendForm({
// return the data for the checkout form
return {
action : action
, method : method
, data : data
});
};

},


Expand Down Expand Up @@ -1035,13 +1045,13 @@
data['item_description_' + counter] = options_list.join(", ");
});

simpleCart.trigger('beforeCheckout', [data]);

simpleCart.generateAndSendForm({
// return the data for the checkout form
return {
action : action
, method : method
, data : data
});
};


},

Expand Down Expand Up @@ -1104,14 +1114,12 @@
data['item_description_' + counter] = options_list.join(", ");
});

simpleCart.trigger('beforeCheckout', [data]);

simpleCart.generateAndSendForm({
// return the data for the checkout form
return {
action : action
, method : method
, data : data
});

};

},

Expand Down Expand Up @@ -1172,14 +1180,12 @@
data = simpleCart.extend(data,opts.extra_data);
}

simpleCart.trigger('beforeCheckout', [data]);

simpleCart.generateAndSendForm({
// return the data for the checkout form
return {
action : action
, method : method
, data : data
});

};
}


Expand Down
56 changes: 51 additions & 5 deletions test/test.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ test("adding and removing items", function(){
});

same( item3.price() , 36 , "Price with dollar sign in front is parsed correctly");


simpleCart.empty();

var item4 = simpleCart.add({
name: "RaceCar",
quantity: 1.4342
});

same( item4.quantity() , 1 , "Item quantity parsed as INT and not decimal");
same( simpleCart.quantity(), 1 , "SimpleCart quantity parsed as INT and not decimal");

});

test("editing items", function(){
Expand Down Expand Up @@ -252,7 +264,6 @@ test("editing items", function(){

item.set("special_value" , "hullo");


});


Expand All @@ -272,9 +283,7 @@ test("editing items", function(){
simpleCart.bind( 'afterAdd' , function( item ){
afteradd_not_called = false;
});




simpleCart.load();

ok( beforeadd_not_called , "beforeAdd event is not called on load" );
Expand Down Expand Up @@ -381,7 +390,6 @@ test("editing items", function(){
shippingCustom: null
});
same( simpleCart.shipping() , 7 , "Item shipping prototype function works");

});
test("tax works", function(){

Expand Down Expand Up @@ -435,6 +443,44 @@ test("editing items", function(){
});


test("tax and shipping send to paypal", function(){

simpleCart({
taxRate: 0.5
});

simpleCart.shipping(function(){
return 5.55555;
});

simpleCart.empty();
simpleCart.add({
name: "cool thing with weird price",
price: 111.1111111111
});

simpleCart({
checkout: {
type: "PayPal",
email: "you@yours.com"
}
});

simpleCart.bind( "beforeCheckout" , function(data){

same( data.amount_1 , ( data.amount_1*1).toFixed(2) , "Item price is correctly formatted before going to paypal");
same( data.tax_cart , ( data.tax_cart*1 ).toFixed(2) , "Tax is correctly formated before going to paypal");
same( data.handling_cart , ( data.handling_cart*1 ).toFixed(2) ,"Shipping is correctly formated before going to paypal" );

//return false;
});


simpleCart.checkout();

});


module('simpleCart.find');
test("simpleCart.find() function works", function(){

Expand Down

0 comments on commit 17333ba

Please sign in to comment.