Permalink
Browse files

Added paypal price formatting and moved checkout event to inside of .…

…checkout()
  • Loading branch information...
1 parent 179e2c0 commit 17333ba3a58bfcc718e7d14dfd7f7b7b836531d4 @brettwejrowski brettwejrowski committed Jun 12, 2012
Showing with 81 additions and 30 deletions.
  1. +3 −4 README.md
  2. +27 −21 simpleCart.js
  3. +51 −5 test/test.core.js
View
@@ -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
View
@@ -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");
}
@@ -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",
@@ -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;
@@ -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
- });
+ };
+
},
@@ -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
- });
+ };
+
},
@@ -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
- });
-
+ };
},
@@ -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
- });
-
+ };
}
View
@@ -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(){
@@ -252,7 +264,6 @@ test("editing items", function(){
item.set("special_value" , "hullo");
-
});
@@ -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" );
@@ -381,7 +390,6 @@ test("editing items", function(){
shippingCustom: null
});
same( simpleCart.shipping() , 7 , "Item shipping prototype function works");
-
});
test("tax works", function(){
@@ -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(){

0 comments on commit 17333ba

Please sign in to comment.