Skip to content

Instantly share code, notes, and snippets.

@islandskater43
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save islandskater43/e8836567012a3c7fa766 to your computer and use it in GitHub Desktop.
Save islandskater43/e8836567012a3c7fa766 to your computer and use it in GitHub Desktop.
Add 'Change Payment Method' button to Onepage Checkout
<?php
/**
* Amazon Payments
*
* @category Amazon
* @package Amazon_Payments
* @copyright Copyright (c) 2014 Amazon.com
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
*/
?>
<form id="co-widget-form" action="">
<div class="amazon-widget" id="checkout-widget">
<input type="checkbox" style="display:none;"> <!-- MageMonkey Mailchimp compatibility (their JS requires a form element to dynamically append input element) -->
<input type="hidden" name="amazon_order_reference_id" value="" />
<input type="hidden" name="payment[method]" value="amazon_payments" />
<?php if ($this->isShowShip()) : ?>
<div id="amazon-widget-address-wrapper" class="amazon-widget-wrapper">
<h3>Shipping Information</h3>
<div id="amazon-widget-address"></div>
</div>
<?php endif; ?>
<div id="amazon-widget-wallet-wrapper" class="amazon-widget-wrapper">
<h3>Payment Information</h3>
<div id="amazon-widget-wallet"></div>
</div>
<div class="buttons-set" id="widget-buttons-container">
<button type="button" class="button" onclick="amazonCheckoutCancel();"><span><span><?php echo $this->__('Change Payment Method'); ?></span></span></button>
<button type="button" title="<?php echo $this->__('Continue') ?>" class="button" onclick="amazonWidgetStep.save()"><span><span><?php echo $this->__('Continue') ?></span></span></button>
<span class="please-wait" id="widget-please-wait" style="display:none;">
<img src="<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif') ?>" alt="<?php echo $this->__('Loading next step...') ?>" title="<?php echo $this->__('Loading next step...') ?>" class="v-middle" /> <?php echo $this->__('Loading next step...') ?>
</span>
</div>
</div>
</form>
<script type="text/javascript">
//<![CDATA[
function amazonCheckoutCancel() {
amazon.Login.logout();
window.top.location.href = "<?php echo $this->getUrl('amazon_payments/checkout/clear'); ?>";
}
var AmazonWidgetStep = Class.create();
AmazonWidgetStep.prototype = {
hasAddress: false,
hasPayment: false,
showShipping: true,
initialize: function(form, saveUrl){
this.form = form;
if ($(this.form)) {
$(this.form).observe('submit', function(event){this.save();Event.stop(event);}.bind(this));
}
this.saveUrl = saveUrl;
//this.validator = new Validation(this.form);
this.onSave = this.nextStep.bindAsEventListener(this);
this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
},
setAmazonOrderReferenceId: function(id) {
$(this.form)["amazon_order_reference_id"].value = id;
},
validate: function() {
if (!$(this.form)["amazon_order_reference_id"].value) {
alert("Amazon Order Reference ID required. Please try again.");
return false;
}
if (this.showShipping && !this.hasAddress) {
alert(Translator.translate('Please select a shipping address.').stripTags());
return false;
}
if (!this.hasPayment) {
alert(Translator.translate('Please select a payment method.').stripTags());
return false;
}
return true;
},
save: function(){
if (checkout.loadWaiting!=false) return;
if (this.validate()) {
checkout.setLoadWaiting('widget');
var request = new Ajax.Request(
this.saveUrl,
{
method:'post',
onComplete: this.onComplete,
onSuccess: this.onSave,
onFailure: checkout.ajaxFailure.bind(checkout),
parameters: Form.serialize(this.form)
}
);
}
},
resetLoadWaiting: function(transport){
checkout.setLoadWaiting(false);
},
nextStep: function(transport){
if (transport && transport.responseText){
try{
response = eval('(' + transport.responseText + ')');
}
catch (e) {
response = {};
}
}
if (response.error) {
alert(response.message);
return false;
}
if (response.update_section) {
$('checkout-'+response.update_section.name+'-load').update(response.update_section.html);
}
if (response.goto_section) {
checkout.gotoSection(response.goto_section, true);
return;
}
checkout.setShippingMethod();
}
};
var amazonWidgetStep = new AmazonWidgetStep('co-widget-form', "<?php echo $this->getUrl('amazon_payments/onepage/saveWidget', array('_secure' => true)) ?>");
// Variables for Mage Onepage
var quoteBaseGrandTotal = <?php echo (float)$this->getQuoteBaseGrandTotal(); ?>;
var checkQuoteBaseGrandTotal = quoteBaseGrandTotal;
var lastPrice;
// Empty class for shippingMethod.nextStep
var payment = Class.create();
payment.initWhatIsCvvListeners = function() { };
payment.form = 'co-widget-form';
payment.currentMethod = 'amazon_payments';
// Overrides
document.observe("dom:loaded", function() {
checkout.gotoSection('widget', false);
if (typeof shippingMethod != "undefined") {
shippingMethod.saveUrl = '<?php echo $this->getUrl('amazon_payments/onepage/saveShippingMethod', array('_secure' => true)) ?>';
}
// Add error checking for payment declines
Review.prototype.nextStep = function(transport){
if (transport && transport.responseText) {
try{
response = eval('(' + transport.responseText + ')');
}
catch (e) {
response = {};
}
if (response.redirect) {
this.isSuccess = true;
location.href = response.redirect;
return;
}
if (response.success) {
this.isSuccess = true;
window.location=this.successUrl;
}
else{
var msg = response.error_messages;
if (typeof(msg)=='object') {
msg = msg.join("\n");
}
if (msg) {
alert(msg);
}
// Refresh Wallet
if (msg.indexOf("terms and conditions") == -1) {
checkout.gotoSection("widget", true);
setTimeout(loadAmazonWallet, 2000);
}
}
if (response.update_section) {
$('checkout-'+response.update_section.name+'-load').update(response.update_section.html);
}
if (response.goto_section) {
checkout.gotoSection(response.goto_section, true);
}
}
}
});
// 1.9
$$('body')[0].addClassName('opc-has-progressed-from-login');
//]]>
</script>
<?php if ($this->isShowShip()) : ?>
<!-- Address Book Widget JS -->
<script>
new OffAmazonPayments.Widgets.AddressBook({
sellerId: '<?php echo $this->getSellerId(); ?>',
onOrderReferenceCreate: function(orderReference) {
amazonWidgetStep.setAmazonOrderReferenceId(orderReference.getAmazonOrderReferenceId());
},
onAddressSelect: function(orderReference) {
amazonWidgetStep.hasAddress = true;
},
design: {
designMode: 'responsive'
},
onError: function(error) {
if (error.getErrorCode() == "BuyerSessionExpired") {
document.location.href = '<?php echo $this->getUrl('checkout/amazon_payments/clear'); ?>';
}
console.log(error.getErrorMessage());
}
}).bind("amazon-widget-address");
</script>
<?php endif; ?>
<!-- Wallet Widget JS -->
<script>
function loadAmazonWallet() {
new OffAmazonPayments.Widgets.Wallet({
<?php if (!$this->isShowShip()) : ?>
onOrderReferenceCreate: function(orderReference) {
amazonWidgetStep.showShipping = false;
amazonWidgetStep.setAmazonOrderReferenceId(orderReference.getAmazonOrderReferenceId());
},
<?php endif; ?>
sellerId: '<?php echo $this->getSellerId(); ?>',
design: {
designMode: 'responsive',
},
onPaymentSelect: function(orderReference) {
amazonWidgetStep.hasPayment = true;
},
onError: function(error) {
console.log(error.getErrorMessage());
}
}).bind("amazon-widget-wallet");
}
loadAmazonWallet();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment