Skip to content

Commit

Permalink
Merge pull request #11 from Kalpa-Services/10-token-not-generating-ac…
Browse files Browse the repository at this point in the history
…cording-to-api-spec

10 token not generating according to api spec
  • Loading branch information
carlHandy authored Jul 18, 2024
2 parents 49e0dcc + 07615aa commit bd84741
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
35 changes: 27 additions & 8 deletions includes/class-mmg-checkout-payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct() {
}

public function enqueue_scripts() {
wp_enqueue_script('mmg-checkout', plugin_dir_url(__FILE__) . 'js/mmg-checkout.js', array('jquery'), '1.0', true);
wp_enqueue_script('mmg-checkout', plugin_dir_url(dirname(__FILE__)) . 'js/mmg-checkout.js', array('jquery'), '3.0', true);
wp_localize_script('mmg-checkout', 'mmg_checkout_params', array(
'ajax_url' => admin_url('admin-ajax.php'),
));
Expand Down Expand Up @@ -71,9 +71,10 @@ public function generate_checkout_url() {
'merchantName' => get_option('mmg_merchant_name', get_bloginfo('name')),
);

$token = $this->encrypt_and_encode($token_data);
$encrypted = $this->encrypt($token_data);
$encoded = $this->url_safe_base64_encode($encrypted);
$checkout_url = add_query_arg(array(
'token' => $token,
'token' => $encoded,
'merchantId' => get_option('mmg_merchant_id'),
'X-Client-ID' => get_option('mmg_client_id'),
), $this->get_checkout_url());
Expand All @@ -89,15 +90,33 @@ private function get_checkout_url() {
return $this->mode === 'live' ? $this->live_checkout_url : $this->demo_checkout_url;
}

private function encrypt_and_encode($data) {
private function encrypt($data) {
$json = json_encode($data);
$public_key = openssl_pkey_get_public(get_option('mmg_rsa_public_key'));

// Use OAEP padding with SHA-256
openssl_public_encrypt($json, $encrypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING);
if (!$public_key) {
throw new Exception('Invalid public key');
}

// Convert JSON to bytes using ISO-8859-1 encoding
$json_bytes = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $json);

if ($json_bytes === false) {
throw new Exception('Encoding conversion failed');
}

// Use URL-safe Base64 encoding
return rtrim(strtr(base64_encode($encrypted), '+/', '-_'), '=');
// Encrypt using OpenSSL's public encrypt function with OAEP padding
openssl_public_encrypt($json_bytes, $encrypted, $public_key, OPENSSL_PKCS1_OAEP_PADDING);

if ($encrypted === false) {
throw new Exception('Encryption failed');
}

return $encrypted;
}

private function url_safe_base64_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

private function validate_public_key() {
Expand Down
25 changes: 22 additions & 3 deletions js/mmg-checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,31 @@ jQuery(document).ready(function($) {
order_id: orderId
},
success: function(response) {
if (response.success) {
window.location.href = response.data.checkout_url;
if (response.success && response.data.checkout_url) {
if (isValidUrl(response.data.checkout_url)) {
window.location.href = response.data.checkout_url;
} else {
console.error('Invalid checkout URL:', response.data.checkout_url);
alert('Error: Invalid checkout URL generated');
}
} else {
alert('Error generating checkout URL');
console.error('Error generating checkout URL:', response.data.error);
alert('Error generating checkout URL: ' + (response.data.error || 'Unknown error'));
}
},
error: function(xhr, status, error) {
console.error('AJAX error:', status, error);
alert('Error communicating with the server. Please try again.');
}
});
});

function isValidUrl(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}
});

0 comments on commit bd84741

Please sign in to comment.