Created
July 28, 2016 19:48
-
-
Save zkoch/c8b75b0d649e9cd1a6ab15512cb996a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
PaymentRequestBehavior = { | |
listeners: { | |
'show-payment-request': '_requestPayment', | |
}, | |
_requestPayment: function(e, total) { | |
var supportedInstruments = [{ | |
supportedMethods: [ | |
'amex', 'diners', 'discover', 'mastercard', 'visa' | |
] | |
}]; | |
var formattedTotal = '' + total.toFixed(2); | |
var details = { | |
'total': { | |
label: "Total due", | |
amount: {'currency': 'USD', value: formattedTotal} | |
}, | |
'items': [{ | |
'id': 'shipping', | |
'label': 'Free standard shipping', | |
'amount': {'currency': 'USD', 'value': '0.00'} | |
}], | |
'shippingOptions': [{ | |
'id': 'standardShipping', | |
'label': 'Free standard shipping', | |
'amount': {'currency': 'USD', 'value': '0.00'}, | |
selected: true | |
}] | |
}; | |
var options = { | |
'requestShipping': true | |
}; | |
var self = this; | |
try { | |
var request = new PaymentRequest( | |
supportedInstruments, details, options); | |
request.show().then(function(instrumentResponse) { | |
fetch('/data/sample_success_response.json', { | |
method: 'POST', | |
headers: new Headers({'Content-Type': 'application/json'}), | |
body: JSON.stringify({ | |
cart: details, | |
shippingAddress: request.shippingAddress, | |
shippingOption: request.shippingOption, | |
paymentMethodName: instrumentResponse.methodName, | |
paymentDetails: instrumentResponse.details}) | |
}).then(function(buy) { | |
if (buy.success) { | |
buy.json().then(function(buyJson) { | |
self._complete(instrumentResponse, buyJson.success, buyJson.msg); | |
}).catch(function(err) {self._complete(instrumentResponse, false, err.message);}); | |
} else { | |
self._complete(instrumentResponse, false, 'Could not post to /buy'); | |
} | |
}).catch(function(err) {self._complete(instrumentResponse, false, err.message);}); | |
}).catch(function(err) { | |
//alert(err); | |
//self._pushState('error'); | |
}); | |
} catch(e) { | |
//alert(e); | |
//self._pushState('error'); | |
} | |
}, | |
_complete: function(instrument, result, msg) { | |
var self = this; | |
instrument.complete("success").then(function() { | |
if (true) { | |
window.location = "/checkout/success" | |
//self._pushState('success'); | |
//self._reset(); | |
self.fire('clear-cart'); | |
} else { | |
window.location = "checkout/error"; | |
//self._pushState('error'); | |
} | |
}).catch(function(error) { | |
self._pushState('error'); | |
}); | |
} | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment