Skip to content

Instantly share code, notes, and snippets.

@seanc
Created November 19, 2015 04:18
Show Gist options
  • Save seanc/c050b44eab8d7dd3890d to your computer and use it in GitHub Desktop.
Save seanc/c050b44eab8d7dd3890d to your computer and use it in GitHub Desktop.
Simple module to programmatically create paypal buynow buttons. ! Warning ! Very ghetto version.
var paypalButton = (function() {
/** Module by Sean Wilson (http://imsean.me) */
var methods = {};
methods.create = function(element, config, btnHtml) {
var
button = config.button,
type = config.type,
name = config.name,
price = config.price,
merchant = config.merchant,
input = $('<input type="hidden">'),
btn = $('<button type="submit"></button>');
/** Set default form attributes */
element
.attr('method', 'post')
.attr('action', 'https://www.paypal.com/cgi-bin/webscr')
.attr('target', '_top');
var default_config = {
'cmd': '_xclick',
'bn': 'JavascriptButton_buynow',
'env': 'www',
'business': element.attr('data-merchant')
};
config = $.extend({}, config, default_config);
/** Set dynamic form input attributes */
$.each(config, function(k, v) {
if(k == 'name') k = 'item_name';
if(k == 'price') k = 'amount';
element.append(input.clone().attr({
'name': k,
'value': v
}));
});
element.removeAttr('data-merchant');
/** Build and append button to form */
btn.addClass('btn btn-primary btn-block').html(btnHtml);
element.append(btn);
};
return methods;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment