Skip to content

Instantly share code, notes, and snippets.

@grantalltodavid
Created October 3, 2017 06:09
Show Gist options
  • Save grantalltodavid/96f2e2b3575309408ad8a8b9dea9cab4 to your computer and use it in GitHub Desktop.
Save grantalltodavid/96f2e2b3575309408ad8a8b9dea9cab4 to your computer and use it in GitHub Desktop.
Sliced Invoices Stripe Gateway: move Apple Pay button right to the top of invoice page
add_action( 'sliced_invoice_top_bar_left', 'sliced_custom_apple_pay_button', 999 );
add_action( 'sliced_invoice_head', 'sliced_custom_apple_pay_button_scripts' );
function sliced_custom_apple_pay_button() {
$payments = get_option( 'sliced_payments' );
if ( ! $payments['stripe_apple_pay'] ) {
return;
}
if( has_term( array( 'paid', 'cancelled' ), 'invoice_status' ) ) {
return;
}
$id = sliced_get_invoice_id();
$currency = sliced_get_invoice_currency( $id );
$currency_code = $currency && $currency !== 'default' ? $currency : $payments['stripe_currency'];
$payments_url = esc_url( get_permalink( (int)$payments['payment_page'] ) );
?>
<div class="sliced_gateway_button">
<button id="sliced-stripe-apple-pay-button" type="button"></button>
</div>
<script type="text/javascript">
Stripe.setPublishableKey( '<?php echo $payments['stripe_publishable'] ?>' );
Stripe.applePay.checkAvailability(function(available) {
if (available) {
document.getElementById('sliced-stripe-apple-pay-button').style.display = 'block';
}
});
document.getElementById('sliced-stripe-apple-pay-button').addEventListener('click', beginApplePay);
function beginApplePay() {
var paymentRequest = {
countryCode: '<?php echo ( $payments['stripe_apple_pay_country'] );?>',
currencyCode: '<?php echo $currency_code; ?>',
total: {
label: '<?php echo __( 'Invoice', 'sliced-invoices' ) . ' ' . esc_html( sliced_get_invoice_prefix() ) . esc_html( sliced_get_invoice_number() ); ?>',
amount: '<?php echo sliced_get_invoice_total_raw( $id ); ?>'
}
};
var session = Stripe.applePay.buildSession(paymentRequest,
function(result, completion) {
jQuery.post('<?php echo $payments_url; ?>', {
sliced_payment_invoice_id: "<?php echo $id; ?>",
stripeToken: result.token.id,
sliced_stripe_applepay: "auth"
}).done(function() {
completion(ApplePaySession.STATUS_SUCCESS);
window.location.href = '<?php echo add_query_arg( array(
'sliced_stripe_applepay' => 'success',
'sliced_payment_invoice_id' => $id,
), $payments_url ); ?>';
}).fail(function() {
completion(ApplePaySession.STATUS_FAILURE);
});
}, function(error) {
console.log(error.message);
});
session.oncancel = function() {
console.log("User hit the cancel button in the payment window");
};
session.begin();
}
</script>
<?php
}
function sliced_custom_apple_pay_button_scripts() {
wp_register_script( 'stripe', 'https://js.stripe.com/v2/', array( 'jquery' ) );
wp_print_scripts( 'stripe' );
?>
<style type="text/css">
#sliced-stripe-apple-pay-button {
display: none;
background-color: black;
background-image: -webkit-named-image(apple-pay-logo-white);
background-size: 100% 100%;
background-origin: content-box;
background-repeat: no-repeat;
width: 100px;
height: 30px;
margin-top: -1px;
padding: 10px 0;
border: none;
border-radius: 3px;
}
</style>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment