Skip to content

Commit

Permalink
Merge pull request #1483 from angelleye/PFW-668
Browse files Browse the repository at this point in the history
PFW-668 - Google V3 reCAPTCHA on Checkout page with PayFlow, DDP, and Express Checkout
  • Loading branch information
deepakmaurya committed Aug 8, 2020
2 parents 913243c + f4bb688 commit f1bae34
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 1 deletion.
93 changes: 93 additions & 0 deletions classes/wc-gateway-paypal-pro-angelleye.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ function __construct() {
$this->api_password = $this->get_option('sandbox_api_password');
$this->api_signature = $this->get_option('sandbox_api_signature');
}
$this->enable_google_recaptcha = 'yes' === $this->get_option('enable_google_recaptcha', 'no');
$this->recaptcha_site_key = $this->get_option('recaptcha_site_key', '');
$this->recaptcha_secret_key = $this->get_option('recaptcha_secret_key', '');
if($this->enable_google_recaptcha) {
if(empty($this->recaptcha_site_key) || empty($this->recaptcha_secret_key)) {
$this->enable_google_recaptcha = false;
}
}
// Maestro
if (!$this->enable_3dsecure) {
unset($this->available_card_types['GB']['Maestro']);
Expand Down Expand Up @@ -183,6 +191,9 @@ function __construct() {
$this->calculation_angelleye = new WC_Gateway_Calculation_AngellEYE();
}
do_action( 'angelleye_paypal_for_woocommerce_multi_account_api_' . $this->id, $this, null, null );
if( $this->enable_google_recaptcha ) {
add_action('angelleye_pfw_add_google_recaptcha', array($this, 'own_angelleye_pfw_add_google_recaptcha'));
}
}

/**
Expand Down Expand Up @@ -473,6 +484,25 @@ function init_form_fields() {
),
'default' => 'four_digit'
),
'enable_google_recaptcha' => array(
'title' => __('Enable/Disable', 'paypal-for-woocommerce'),
'label' => __('Enable Google reCAPTCHA v3', 'paypal-for-woocommerce'),
'type' => 'checkbox',
'description' => 'Sign up and get your keys : <a target="_blank" href="https://www.google.com/recaptcha/admin/create" target="_blank">https://www.google.com/recaptcha/admin/create</a> (you will get a SITE key and a SECRET key)',
'default' => 'no'
),
'recaptcha_site_key' => array(
'title' => __('reCAPTCHA V3 - Site Key', 'paypal-for-woocommerce'),
'type' => 'text',
'description' => __('Please enter only Google reCAPTCHA V3 Credentials, V2 Credentials are not supported', 'paypal-for-woocommerce'),
'default' => ''
),
'recaptcha_secret_key' => array(
'title' => __('reCAPTCHA V3 - Secret Key', 'paypal-for-woocommerce'),
'type' => 'text',
'description' => __('Please enter only Google reCAPTCHA V3 Credentials, V2 Credentials are not supported', 'paypal-for-woocommerce'),
'default' => ''
),
'debug' => array(
'title' => __('Debug Log', 'paypal-for-woocommerce'),
'type' => 'checkbox',
Expand Down Expand Up @@ -532,6 +562,14 @@ public function admin_options() {
production.show();
}
}).change();
jQuery('#woocommerce_paypal_pro_enable_google_recaptcha').change(function () {
var google_recaptcha_fields = jQuery('#woocommerce_paypal_pro_recaptcha_site_key, #woocommerce_paypal_pro_recaptcha_secret_key').closest('tr');
if (jQuery(this).is(':checked')) {
google_recaptcha_fields.show();
} else {
google_recaptcha_fields.hide();
}
}).change();
jQuery('#woocommerce_paypal_pro_send_items').change(function () {
var paypal_pro_subtotal_mismatch_behavior = jQuery('#woocommerce_paypal_pro_subtotal_mismatch_behavior').closest('tr');
if (jQuery(this).is(':checked')) {
Expand Down Expand Up @@ -586,6 +624,7 @@ public function log($message, $level = 'info') {
*/
public function payment_fields() {
do_action('before_angelleye_pc_payment_fields', $this);

if ($this->description) {
echo '<p>' . wp_kses_post($this->description);
}
Expand Down Expand Up @@ -613,6 +652,7 @@ public function payment_fields() {
$this->form();
}
do_action('payment_fields_saved_payment_methods', $this);
do_action('angelleye_pfw_add_google_recaptcha');
}

public function save_payment_method_checkbox() {
Expand Down Expand Up @@ -717,6 +757,8 @@ public function get_posted_card() {

public function validate_fields() {
try {

$this->angelleye_pfw_validate_google_recaptcha();
if (isset($_POST['wc-paypal_pro-payment-token']) && 'new' !== $_POST['wc-paypal_pro-payment-token']) {
$token_id = wc_clean($_POST['wc-paypal_pro-payment-token']);
$token = WC_Payment_Tokens::get($token_id);
Expand Down Expand Up @@ -2307,4 +2349,55 @@ public function angelleye_successwithwarning_payment_response_handler($order, $P
}
}
}

public function own_angelleye_pfw_add_google_recaptcha() {
if( $this->enable_google_recaptcha ) {
wp_enqueue_script('pfw_recaptcha', 'https://www.google.com/recaptcha/api.js?render='.$this->recaptcha_site_key, array(), '', true);
echo '<input type="hidden" id="pfw_google" name="pfw_google" value="">';
?>
<script>
jQuery(document).ready(function(){
var pfw_grecaptcha = function( ) {
grecaptcha.ready(function() {
grecaptcha.execute('<?php echo $this->recaptcha_site_key; ?>', {action: 'submit'}).then(function(token) {
document.getElementById("pfw_google").value = token;
});
});
};
jQuery(document.body).on('updated_checkout checkout_error', function () {
pfw_grecaptcha();
});
setInterval(function(){
pfw_grecaptcha();
}, 110000);
});
</script>
<?php
}
}

public function angelleye_pfw_validate_google_recaptcha() {
if( $this->enable_google_recaptcha ) {
if(isset($_POST['pfw_google']) && !empty($_POST['pfw_google']) ) {
$response_data = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', array(
'body' => array('secret' => $this->recaptcha_secret_key, 'response' => $_POST['pfw_google'])
) );
if (is_wp_error($response_data)) {
throw new Exception(__('Google recaptcha verification Failed', 'paypal-for-woocommerce'));
}
$body = wp_remote_retrieve_body($response_data);
if( !empty($body)) {
$response = json_decode($body);
if(!$response->success ) {
throw new Exception(__('Google recaptcha verification Failed', 'paypal-for-woocommerce'));
}
if($response->score < 0.2) {
throw new Exception(__('Very likely a bot', 'paypal-for-woocommerce'));
}
}
} else {
throw new Exception(__('Google recaptcha verification Failed', 'paypal-for-woocommerce'));
}
}
}
}
92 changes: 91 additions & 1 deletion classes/wc-gateway-paypal-pro-payflow-angelleye.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,18 @@ function __construct() {
$this->fraud_codes = array('125', '128', '131', '126', '127');
$this->fraud_error_codes = array('125', '128', '131');
$this->fraud_warning_codes = array('126', '127');
$this->enable_google_recaptcha = 'yes' === $this->get_option('enable_google_recaptcha', 'no');
$this->recaptcha_site_key = $this->get_option('recaptcha_site_key', '');
$this->recaptcha_secret_key = $this->get_option('recaptcha_secret_key', '');
if($this->enable_google_recaptcha) {
if(empty($this->recaptcha_site_key) || empty($this->recaptcha_secret_key)) {
$this->enable_google_recaptcha = false;
}
}
do_action('angelleye_paypal_for_woocommerce_multi_account_api_' . $this->id, $this, null, null);
if( $this->enable_google_recaptcha ) {
add_action('angelleye_pfw_payflow_add_google_recaptcha', array($this, 'own_angelleye_pfw_payflow_add_google_recaptcha'));
}
}

public function add_log($message, $level = 'info') {
Expand Down Expand Up @@ -516,6 +527,25 @@ function init_form_fields() {
'description' => __('Display card holder first and last name in credit card form.', 'paypal-for-woocommerce'),
'default' => 'no'
),
'enable_google_recaptcha' => array(
'title' => __('Enable/Disable', 'paypal-for-woocommerce'),
'label' => __('Enable Google reCAPTCHA v3', 'paypal-for-woocommerce'),
'type' => 'checkbox',
'description' => 'Sign up and get your keys : <a target="_blank" href="https://www.google.com/recaptcha/admin/create" target="_blank">https://www.google.com/recaptcha/admin/create</a> (you will get a SITE key and a SECRET key)',
'default' => 'no'
),
'recaptcha_site_key' => array(
'title' => __('reCAPTCHA V3 - Site Key', 'paypal-for-woocommerce'),
'type' => 'text',
'description' => __('Please enter only Google reCAPTCHA V3 Credentials, V2 Credentials are not supported', 'paypal-for-woocommerce'),
'default' => ''
),
'recaptcha_secret_key' => array(
'title' => __('reCAPTCHA V3 - Secret Key', 'paypal-for-woocommerce'),
'type' => 'text',
'description' => __('Please enter only Google reCAPTCHA V3 Credentials, V2 Credentials are not supported', 'paypal-for-woocommerce'),
'default' => ''
),
'debug' => array(
'title' => __('Debug Log', 'paypal-for-woocommerce'),
'type' => 'checkbox',
Expand Down Expand Up @@ -614,6 +644,14 @@ public function admin_options() {
production.show();
}
}).change();
jQuery('#woocommerce_paypal_pro_payflow_enable_google_recaptcha').change(function () {
var payflow_google_recaptcha_fields = jQuery('#woocommerce_paypal_pro_payflow_recaptcha_site_key, #woocommerce_paypal_pro_payflow_recaptcha_secret_key').closest('tr');
if (jQuery(this).is(':checked')) {
payflow_google_recaptcha_fields.show();
} else {
payflow_google_recaptcha_fields.hide();
}
}).change();
jQuery('#woocommerce_paypal_pro_payflow_send_items').change(function () {
var payflow_subtotal_mismatch_behavior = jQuery('#woocommerce_paypal_pro_payflow_subtotal_mismatch_behavior').closest('tr');
if (jQuery(this).is(':checked')) {
Expand Down Expand Up @@ -1521,6 +1559,7 @@ public function payment_fields() {
$this->form();
}
do_action('payment_fields_saved_payment_methods', $this);
do_action('angelleye_pfw_payflow_add_google_recaptcha');
}

public function save_payment_method_checkbox() {
Expand Down Expand Up @@ -1638,7 +1677,7 @@ public function process_refund($order_id, $amount = null, $reason = '') {
* @since 1.1.7.6
*/
public function validate_fields() {

$this->angelleye_pfw_payflow_validate_google_recaptcha();
if (isset($_POST['wc-paypal_pro_payflow-payment-token']) && 'new' !== $_POST['wc-paypal_pro_payflow-payment-token']) {
$token_id = wc_clean($_POST['wc-paypal_pro_payflow-payment-token']);
$token = WC_Payment_Tokens::get($token_id);
Expand Down Expand Up @@ -2455,5 +2494,56 @@ public function get_order_item_names( $order ) {
$orderdesc = apply_filters( 'ae_pppf_paypal_orderdesc', implode( ', ', $item_names ), $order );
return substr($orderdesc, 0, 127);
}

public function own_angelleye_pfw_payflow_add_google_recaptcha() {
if( $this->enable_google_recaptcha ) {
wp_enqueue_script('pfw_payflow_recaptcha', 'https://www.google.com/recaptcha/api.js?render='.$this->recaptcha_site_key, array(), '', true);
echo '<input type="hidden" id="pfw_payflow_google" name="pfw_payflow_google" value="">';
?>
<script>
jQuery(document).ready(function(){
var pfw_payflow_grecaptcha = function( ) {
grecaptcha.ready(function() {
grecaptcha.execute('<?php echo $this->recaptcha_site_key; ?>', {action: 'submit'}).then(function(token) {
document.getElementById("pfw_payflow_google").value = token;
});
});
};
jQuery(document.body).on('updated_checkout checkout_error', function () {
pfw_payflow_grecaptcha();
});
setInterval(function(){
pfw_payflow_grecaptcha();
}, 110000);
});
</script>
<?php
}
}

public function angelleye_pfw_payflow_validate_google_recaptcha() {
if( $this->enable_google_recaptcha ) {
if(isset($_POST['pfw_payflow_google']) && !empty($_POST['pfw_payflow_google']) ) {
$response_data = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', array(
'body' => array('secret' => $this->recaptcha_secret_key, 'response' => $_POST['pfw_payflow_google'])
) );
if (is_wp_error($response_data)) {
throw new Exception(__('Google recaptcha verification Failed', 'paypal-for-woocommerce'));
}
$body = wp_remote_retrieve_body($response_data);
if( !empty($body)) {
$response = json_decode($body);
if(!$response->success ) {
throw new Exception(__('Google recaptcha verification Failed', 'paypal-for-woocommerce'));
}
if($response->score < 0.2) {
throw new Exception(__('Very likely a bot', 'paypal-for-woocommerce'));
}
}
} else {
throw new Exception(__('Google recaptcha verification Failed', 'paypal-for-woocommerce'));
}
}
}

}

0 comments on commit f1bae34

Please sign in to comment.