Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves javascript structure #39

Merged
merged 3 commits into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 24 additions & 60 deletions razorpay-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ function woocommerce_razorpay_init()

class WC_Razorpay extends WC_Payment_Gateway
{
const BASE_URL = 'https://api.razorpay.com/';

const API_VERSION = 'v1';

// This one stores the WooCommerce Order Id
const SESSION_KEY = 'razorpay_wc_order_id';
const RAZORPAY_PAYMENT_ID = 'razorpay_payment_id';
Expand Down Expand Up @@ -225,9 +221,7 @@ public function generate_razorpay_form($orderId)

$checkoutArgs = $this->getCheckoutArguments($order, $razorpayOrderId);

$json = json_encode($checkoutArgs);

$html = $this->generateOrderForm($redirectUrl, $json, $orderId);
$html = $this->generateOrderForm($redirectUrl, $checkoutArgs);

return $html;
}
Expand Down Expand Up @@ -358,19 +352,36 @@ function getOrderCreationData($orderId)
return $data;
}

private function enqueueCheckoutScripts($data)
{
wp_register_script('razorpay_checkout',
'https://checkout.razorpay.com/v1/checkout.js',
null, null);

wp_register_script('razorpay_wc_script', plugin_dir_url(__FILE__) . 'script.js',
array('razorpay_checkout'));

wp_localize_script('razorpay_wc_script',
'razorpay_wc_checkout_vars',
$data
);

wp_enqueue_script('razorpay_wc_script');
}

/**
* Generates the order form
**/
function generateOrderForm($redirectUrl, $json)
function generateOrderForm($redirectUrl, $data)
{
$html = <<<EOT
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var data = $json;
</script>
$this->enqueueCheckoutScripts($data);

return <<<EOT
<form name='razorpayform' action="$redirectUrl" method="POST">
<input type="hidden" name="razorpay_payment_id" id="razorpay_payment_id">
<input type="hidden" name="razorpay_signature" id="razorpay_signature" >
<!-- This distinguishes all our various wordpress plugins -->
<input type="hidden" name="razorpay_wc_form_submit" value="1">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

</form>
<p id="msg-razorpay-success" class="woocommerce-info woocommerce-message" style="display:none">
Please wait while we are processing your payment.
Expand All @@ -379,54 +390,7 @@ function generateOrderForm($redirectUrl, $json)
<button id="btn-razorpay">Pay Now</button>
<button id="btn-razorpay-cancel" onclick="document.razorpayform.submit()">Cancel</button>
</p>
<script>
(function(){
var setDisabled = function(id, state) {
if (typeof state === 'undefined') {
state = true;
}
var elem = document.getElementById(id);
if (state === false) {
elem.removeAttribute('disabled');
}
else {
elem.setAttribute('disabled', state);
}
};
// Payment was closed without handler getting called
data.modal = {
ondismiss: function() {
setDisabled('btn-razorpay', false);
}
};
data.handler = function(payment){
setDisabled('btn-razorpay-cancel');
var successMsg = document.getElementById('msg-razorpay-success');
successMsg.style.display = "block";
document.getElementById('razorpay_payment_id').value = payment.razorpay_payment_id;
document.getElementById('razorpay_signature').value = payment.razorpay_signature;
document.razorpayform.submit();
};
var razorpayCheckout = new Razorpay(data);
// global method
function openCheckout() {
// Disable the pay button
setDisabled('btn-razorpay');
razorpayCheckout.open();
}
function addEvent(element, evnt, funct){
if (element.attachEvent)
return element.attachEvent('on'+evnt, funct);
else
return element.addEventListener(evnt, funct, false);
}
// Attach event listener
addEvent(document.getElementById('btn-razorpay'), 'click', openCheckout);
openCheckout();
})();
</script>
EOT;
return $html;
}

/**
Expand Down
51 changes: 51 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(function() {
var data = razorpay_wc_checkout_vars;
var setDisabled = function(id, state) {
if (typeof state === 'undefined') {
state = true;
}
var elem = document.getElementById(id);
if (state === false) {
elem.removeAttribute('disabled');
} else {
elem.setAttribute('disabled', state);
}
};

// Payment was closed without handler getting called
data.modal = {
ondismiss: function() {
setDisabled('btn-razorpay', false);
},
};

data.handler = function(payment) {
setDisabled('btn-razorpay-cancel');
var successMsg = document.getElementById('msg-razorpay-success');
successMsg.style.display = 'block';
document.getElementById('razorpay_payment_id').value =
payment.razorpay_payment_id;
document.getElementById('razorpay_signature').value =
payment.razorpay_signature;
document.razorpayform.submit();
};

var razorpayCheckout = new Razorpay(data);

// global method
function openCheckout() {
// Disable the pay button
setDisabled('btn-razorpay');
razorpayCheckout.open();
}

function addEvent(element, evnt, funct) {
if (element.attachEvent) return element.attachEvent('on' + evnt, funct);
else return element.addEventListener(evnt, funct, false);
}
// Attach event listener
window.onload = function() {
addEvent(document.getElementById('btn-razorpay'), 'click', openCheckout);
openCheckout();
};
})();