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

Saved Cards Not Displaying on Payment Methods Page - Braintree : PFW-507 #1383

Merged
merged 6 commits into from
Oct 24, 2019
33 changes: 23 additions & 10 deletions classes/wc-gateway-braintree-angelleye.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ public function payment_fields() {
?>
<script type="text/javascript">
var js_variable = <?php echo json_encode($js_variable); ?>;
var is_registration_required = "<?php echo WC()->checkout()->is_registration_required(); ?>";
var is_logged_in = "<?php echo is_user_logged_in(); ?>";
</script>
<?php
if ($this->enable_braintree_drop_in) {
Expand All @@ -521,7 +523,15 @@ public function payment_fields() {
<?php if (is_ajax() || is_checkout_pay_page() || is_add_payment_method_page()) { ?>
<script type="text/javascript">
var angelleye_dropinInstance;

(function ($) {
var hasCreateAccountCheckbox = 0 < $( 'input#createaccount' ).length,
createAccount = hasCreateAccountCheckbox && $( 'input#createaccount' ).is( ':checked' );
if ( createAccount || is_logged_in || is_registration_required ) {
$( '.payment_method_braintree .woocommerce-SavedPaymentMethods-saveNew' ).show();
} else {
$( '.payment_method_braintree .woocommerce-SavedPaymentMethods-saveNew' ).hide();
}
$('form.checkout').on('checkout_place_order_braintree', function () {
return braintreeFormHandler();
});
Expand Down Expand Up @@ -552,10 +562,12 @@ function is_angelleye_braintree_selected() {
}
}
$(document.body).on('checkout_error', function () {
$('.braintree-token').remove();
$('.braintree-device-data').remove();
$('.is_submit').remove();
$form.unblock();
if(is_angelleye_braintree_selected()) {
$('.braintree-token').remove();
$('.braintree-device-data').remove();
$('.is_submit').remove();
$form.unblock();
}
});
var button = document.querySelector('#place_order');
var $form = $( 'form.checkout, form#order_review, form#add_payment_method' );
Expand Down Expand Up @@ -622,8 +634,8 @@ function is_angelleye_braintree_selected() {
$form.unblock();
});
}
checkout_form.addEventListener('submit', function (event) {
if(is_angelleye_braintree_selected()) {
checkout_form.addEventListener('submit', function (event) {
dropinInstance.requestPaymentMethod(function (err, payload) {
if(err) {
$('.woocommerce-error').remove();
Expand Down Expand Up @@ -661,10 +673,11 @@ function is_angelleye_braintree_selected() {
ccForm.append('<input type="hidden" class="braintree-token" name="braintree_token" value="' + payload.nonce + '"/>');
ccForm.append("<input type='hidden' class='braintree-device-data' id='device_data' name='device_data' value=" + payload.deviceData + ">");
$form.submit();
$('form.checkout').triggerHandler("checkout_place_order");
}
});
});
}
});
});
}(jQuery));
</script>
Expand Down Expand Up @@ -1253,7 +1266,7 @@ public function angelleye_do_payment($order) {
} else {
$customer_id = get_current_user_id();
}
$token->set_token($paymentMethod->billingAgreementId);
$token->set_token($payment_method_token);
$token->set_gateway_id($this->id);
$token->set_card_type('PayPal Billing Agreement');
$token->set_last4(substr($paymentMethod->billingAgreementId, -4));
Expand Down Expand Up @@ -1648,7 +1661,7 @@ public function payment_scripts() {
return;
}
if ($this->enable_braintree_drop_in) {
wp_enqueue_script('braintree-gateway', 'https://js.braintreegateway.com/web/dropin/1.16.0/js/dropin.min.js', array('jquery'), null, false);
wp_enqueue_script('braintree-gateway', 'https://js.braintreegateway.com/web/dropin/1.20.4/js/dropin.min.js', array('jquery'), null, false);
} else {
wp_enqueue_style('braintree_checkout', PAYPAL_FOR_WOOCOMMERCE_ASSET_URL . 'assets/css/braintree-checkout.css', array(), VERSION_PFW);
wp_enqueue_script('braintree-gateway-client', 'https://js.braintreegateway.com/web/3.35.0/js/client.min.js', array('jquery'), null, true);
Expand Down Expand Up @@ -1877,7 +1890,7 @@ public function braintree_save_payment_method($customer_id, $result, $zero_amoun
$token->set_user_id($customer_id);
} elseif (!empty($braintree_method->billingAgreementId)) {
$customer_id = get_current_user_id();
$token->set_token($braintree_method->billingAgreementId);
$token->set_token($payment_method_token);
$token->set_gateway_id($this->id);
$token->set_card_type('PayPal Billing Agreement');
$token->set_last4(substr($braintree_method->billingAgreementId, -4));
Expand Down Expand Up @@ -2657,7 +2670,7 @@ public function braintree_save_payment_method_auth($customer_id, $result, $order
} else {
$customer_id = get_current_user_id();
}
$token->set_token($braintree_method->billingAgreementId);
$token->set_token($payment_method_token);
$token->set_gateway_id($this->id);
$token->set_card_type('PayPal Billing Agreement');
$token->set_last4(substr($braintree_method->billingAgreementId, -4));
Expand Down
74 changes: 74 additions & 0 deletions paypal-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public function __construct()
add_action( 'wp_head', array( $this, 'paypal_for_woo_head_mark' ), 1 );
add_action( 'admin_footer', array($this, 'angelleye_add_deactivation_form'));
add_action( 'wp_ajax_angelleye_send_deactivation', array($this, 'angelleye_handle_plugin_deactivation_request'));
add_action( 'wp', array( __CLASS__, 'angelleye_delete_payment_method_action' ), 10 );
add_filter( 'woocommerce_saved_payment_methods_list', array($this, 'angelleye_synce_braintree_save_payment_methods'), 5, 2 );
$this->customer_id;
}

Expand Down Expand Up @@ -1287,6 +1289,78 @@ public function load_cartflow_pro_plugin() {
include_once plugin_dir_path(__FILE__) . 'angelleye-includes/cartflows-pro/class-angelleye-cartflow-pro-helper.php';
}
}

/**
* Process the delete payment method form.
*/
public static function angelleye_delete_payment_method_action() {
global $wp, $woocommerce;
if ( isset( $wp->query_vars['delete-payment-method'] ) ) {
wc_nocache_headers();
$token_id = absint( $wp->query_vars['delete-payment-method'] );
$token = WC_Payment_Tokens::get( $token_id );
if ( !is_null( $token ) && $token->get_gateway_id() === 'braintree' && get_current_user_id() == $token->get_user_id() && isset( $_REQUEST['_wpnonce'] ) || true === wp_verify_nonce( wp_unslash( $_REQUEST['_wpnonce'] ), 'delete-payment-method-' . $token_id ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
try {
$gateways = $woocommerce->payment_gateways->payment_gateways();
$gateways['braintree']->angelleye_braintree_lib();
$token_value = $token->get_token();
Braintree_PaymentMethod::delete($token_value);
} catch (Braintree_Exception_NotFound $e) {
$gateways['braintree']->add_log("Braintree_PaymentMethod::delete Braintree_Exception_NotFound: " . $e->getMessage());
} catch (Braintree_Exception_Authentication $e) {
$gateways['braintree']->add_log("Braintree_ClientToken::generate Exception: API keys are incorrect, Please double-check that you haven't accidentally tried to use your sandbox keys in production or vice-versa.");
} catch (Braintree_Exception_Authorization $e) {
$gateways['braintree']->add_log("Braintree_ClientToken::generate Exception: The API key that you're using is not authorized to perform the attempted action according to the role assigned to the user who owns the API key.");
} catch (Braintree_Exception_DownForMaintenance $e) {
$gateways['braintree']->add_log("Braintree_Exception_DownForMaintenance: Request times out.");
} catch (Braintree_Exception_ServerError $e) {
$gateways['braintree']->add_log("Braintree_Exception_ServerError" . $e->getMessage());
} catch (Braintree_Exception_SSLCertificate $e) {
$gateways['braintree']->add_log("Braintree_Exception_SSLCertificate" . $e->getMessage());
} catch (Exception $ex) {
$gateways['braintree']->add_log("Exception" . $ex->getMessage());
}
}
}
}

public function angelleye_synce_braintree_save_payment_methods($list, $customer_id) {
global $wp, $woocommerce;
try {
$gateways = $woocommerce->payment_gateways->payment_gateways();
$gateways['braintree']->angelleye_braintree_lib();
if( !empty($gateways['braintree'])) {
if ($gateways['braintree']->enable_tokenized_payments == 'yes') {
$payment_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, 'braintree' );
foreach ( $payment_tokens as $payment_token ) {
$token_value = $payment_token->get_token();
try {
Braintree_PaymentMethod::find($token_value);
} catch (Braintree_Exception_NotFound $e) {
$gateways['braintree']->add_log("Braintree_PaymentMethod::delete Braintree_Exception_NotFound: " . $e->getMessage());
WC_Payment_Tokens::delete( $payment_token->get_id() );
} catch (Braintree_Exception_Authentication $e) {
$gateways['braintree']->add_log("Braintree_ClientToken::generate Exception: API keys are incorrect, Please double-check that you haven't accidentally tried to use your sandbox keys in production or vice-versa.");
} catch (Braintree_Exception_Authorization $e) {
$gateways['braintree']->add_log("Braintree_ClientToken::generate Exception: The API key that you're using is not authorized to perform the attempted action according to the role assigned to the user who owns the API key.");
} catch (Braintree_Exception_DownForMaintenance $e) {
$gateways['braintree']->add_log("Braintree_Exception_DownForMaintenance: Request times out.");
} catch (Braintree_Exception_ServerError $e) {
$gateways['braintree']->add_log("Braintree_Exception_ServerError" . $e->getMessage());
} catch (Braintree_Exception_SSLCertificate $e) {
$gateways['braintree']->add_log("Braintree_Exception_SSLCertificate" . $e->getMessage());
} catch (Exception $ex) {
$gateways['braintree']->add_log("Exception" . $ex->getMessage());
}
}
}

}
} catch (Exception $ex) {

}
return $list;
}
}
}
new AngellEYE_Gateway_Paypal();