From b2ab0d0f548a773a85fdb663c88fed650a0d7947 Mon Sep 17 00:00:00 2001 From: strawp Date: Fri, 27 Oct 2017 15:30:18 +0100 Subject: [PATCH] Made confirmation message work on image load --- paymentrequest.php | 119 ++++++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 50 deletions(-) diff --git a/paymentrequest.php b/paymentrequest.php index 25f5463..12a4c62 100644 --- a/paymentrequest.php +++ b/paymentrequest.php @@ -7,39 +7,45 @@ * - value - cash value of purchase (default: "10") * - confirmation - URL of the payment confirmation page to forward to after receiving details */ - header( "Content-type: text/javascript" ); - - $url = 'http'; - if( !empty( $_SERVER['HTTPS'] ) ){ - $url .= 's'; - } - $url .= "://"; - if( empty( $_SERVER['HTTP_HOST'] ) ){ - $url .= $_SERVER["SERVER_ADDR"]; - }else{ - $url .= $_SERVER['HTTP_HOST']; - } - $url .= $_SERVER["PHP_SELF"]; - - if( !empty( $_GET["label"] ) ){ - $label = $_GET["label"]; - }else{ - $label = "Total"; - } - - if( !empty( $_GET["currency"] ) ){ - $currency = $_GET["currency"]; - }else{ - $currency = "GBP"; - } - - if( !empty( $_GET["value"] ) ){ - $value = $_GET["value"]; - }else{ - $value = "10"; - } - if(empty($_GET["data"])){ + header( "Content-type: text/javascript" ); + + $url = 'http'; + if( !empty( $_SERVER['HTTPS'] ) ){ + $url .= 's'; + } + $url .= "://"; + if( empty( $_SERVER['HTTP_HOST'] ) ){ + $url .= $_SERVER["SERVER_ADDR"]; + }else{ + $url .= $_SERVER['HTTP_HOST']; + } + $url .= $_SERVER["PHP_SELF"]; + + if( !empty( $_GET["label"] ) ){ + $label = $_GET["label"]; + }else{ + $label = "Total"; + } + + if( !empty( $_GET["currency"] ) ){ + $currency = $_GET["currency"]; + }else{ + $currency = "GBP"; + } + + if( !empty( $_GET["value"] ) ){ + $value = $_GET["value"]; + }else{ + $value = "10"; + } + + if( !empty( $_GET["confirmation"] ) ){ + $confirmation = $_GET["confirmation"]; + }else{ + $confirmation = null; + } + ?> if(window.PaymentRequest) { @@ -64,27 +70,40 @@ if(window.PaymentRequest) { const request = new PaymentRequest( supportedPaymentMethods, paymentDetails, options ); - request.show() - .then((paymentResponse) => { - - // Send payment response back to this URL - console.log(paymentResponse); - url = '?data=' + btoa(JSON.stringify(paymentResponse)); - console.log(url); - new Image().src = url; - return paymentResponse.complete(); - }).catch((err) => { - console.log("Payment request failed"); - }); - - window.location = ""; - -} else { + promise = request.show() + .then((paymentResponse) => { + return paymentResponse.complete() + .then(() => {; + // Send payment response back to this URL + url = '?data=' + btoa(JSON.stringify(paymentResponse)); + i= new Image(); + i.addEventListener('load', function(){ + + window.location = ''; + + alert("Payment received, thank you") + + },false); + i.src = url; + }); + + }).catch((err) => { + console.log("Payment request failed"); + }); +} else { // Fallback to traditional checkout console.log("PaymentRequest API not supported in this browser"); }