From afb9586017d256199d8268e674d2760ecc4c7fe9 Mon Sep 17 00:00:00 2001 From: yixian Date: Wed, 31 Jan 2018 18:08:17 +0800 Subject: [PATCH] asynchronize redirect after payment --- .../templates/payment/v1/gateway_jsapi.js | 34 ++++++++++++++---- .../ui/static/templates/payment/v1/payment.js | 35 ++++++++++++++++--- .../templates/payment/v1/retail_jsapi.js | 20 ++++++++++- 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/src/main/ui/static/templates/payment/v1/gateway_jsapi.js b/src/main/ui/static/templates/payment/v1/gateway_jsapi.js index 42b571ea1..fc837d49c 100644 --- a/src/main/ui/static/templates/payment/v1/gateway_jsapi.js +++ b/src/main/ui/static/templates/payment/v1/gateway_jsapi.js @@ -4,16 +4,18 @@ $(document).ready(function () { 'use strict'; decode(); + function decode() { var redirect = window.redirect; - while(redirect.indexOf('://')<0){ + while (redirect.indexOf('://') < 0) { redirect = decodeURIComponent(redirect); - if(redirect==window.redirect){ + if (redirect == window.redirect) { break; } window.redirect = redirect; } } + var dataCache = {paying: false}; $('#key_P').bind('touchstart', startPay); if (window.paydata) { @@ -21,6 +23,7 @@ $(document).ready(function () { invokePay(window.paydata); }) } + function startPay() { $('#wdiv').show(); if (dataCache.paying) { @@ -33,13 +36,13 @@ $(document).ready(function () { method: 'get', dataType: 'json', success: function (pay) { - if('SUCCESS'!=pay.return_code){ + if ('SUCCESS' != pay.return_code) { alert(pay.return_msg); $('#wdiv').hide(); dataCache.paying = false; return; } - if(pay.direct_paid){ + if (pay.direct_paid) { dataCache.paying = false; location.href = window.redirect + (window.redirect.indexOf('?') < 0 ? '?' : '&') + 'success=true'; } @@ -67,7 +70,7 @@ $(document).ready(function () { }, function (res) { dataCache.paying = false; if (res.err_msg == 'get_brand_wcpay_request:ok') { - location.href = window.redirect + (window.redirect.indexOf('?') < 0 ? '?' : '&') + 'success=true'; + startCheckOrder(window.client_moniker, window.merchant_orderid); } else { if (res.err_msg != 'get_brand_wcpay_request:cancel' && res.err_msg != 'get_brand_wcpay_request:fail') { alert('WeChat Error:' + res.err_msg); @@ -81,6 +84,25 @@ $(document).ready(function () { }) } + function startCheckOrder(clientMoniker, merchantOrderId) { + function checkOrderStd() { + $.ajax({ + url: '/api/v1.0/payment/clients/' + clientMoniker + '/orders/' + merchantOrderId + '/status', + method: 'GET', + dataType: 'json', + success: function (res) { + if (res.paid) { + location.href = window.redirect + (window.redirect.indexOf('?') < 0 ? '?' : '&') + 'success=true'; + } else { + setTimeout(checkOrderStd, 500); + } + } + }) + } + + checkOrderStd(); + } + function waitForPaymentSuccess(orderId) { var sock = new SockJS('/register'); var client = Stomp.over(sock); @@ -91,7 +113,7 @@ $(document).ready(function () { if (window.redirect.indexOf('?') > 0) { concat = '&'; } - location.href = window.redirect + concat + 'time=' + json.time + '&nonce_str=' + json.nonce_str + '&sign=' + json.sign+'&success=true'; + location.href = window.redirect + concat + 'time=' + json.time + '&nonce_str=' + json.nonce_str + '&sign=' + json.sign + '&success=true'; }) }) } diff --git a/src/main/ui/static/templates/payment/v1/payment.js b/src/main/ui/static/templates/payment/v1/payment.js index b017686be..1e8259bc4 100644 --- a/src/main/ui/static/templates/payment/v1/payment.js +++ b/src/main/ui/static/templates/payment/v1/payment.js @@ -256,7 +256,7 @@ $(function () { return; } if (pay.direct_paid) { - window.location = '/api/payment/v1.0/wechat_jsapi_payment/partners/' + window.client_moniker + '/orders/' + pay.order_id; + location.href = '/api/payment/v1.0/wechat_jsapi_payment/partners/' + window.client_moniker + '/orders/' + pay.order_id; return; } var paydata = pay.jsapi; @@ -270,7 +270,8 @@ $(function () { }, function (res) { dataCache.paying = false; if (res.err_msg == 'get_brand_wcpay_request:ok') { - window.location = '/api/payment/v1.0/wechat_jsapi_payment/partners/' + window.client_moniker + '/orders/' + pay.order_id; + startCheckOrder(pay.order_id); + } else { if (res.err_msg != 'get_brand_wcpay_request:cancel' && res.err_msg != 'get_brand_wcpay_request:fail') { weuiAlert('WeChat Error:' + res.err_msg); @@ -289,6 +290,24 @@ $(function () { }) }); + function startCheckOrder(orderId) { + function checkOrderStd() { + $.ajax({ + url: '/api/v1.0/payment/orders/' + orderId + '/status', + method: 'GET', + dataType: 'json', + success: function (res) { + if (res.paid) { + location.href = '/api/payment/v1.0/wechat_jsapi_payment/partners/' + window.client_moniker + '/orders/' + orderId; + } else { + setTimeout(checkOrderStd, 500); + } + } + }) + } + checkOrderStd(); + } + function weuiAlert(msg) { var config = { template: msg @@ -343,7 +362,11 @@ $(function () { } var ft = $('
').appendTo(dialogBox); if ($.isFunction(config.confirm)) { - var yes = $('', {class: 'weui_btn_dialog primary', text: 'OK',style:'background: #0bb20c;color: #fff;'}).appendTo(ft); + var yes = $('', { + class: 'weui_btn_dialog primary', + text: 'OK', + style: 'background: #0bb20c;color: #fff;' + }).appendTo(ft); yes.click(function () { config.confirm(dialog, true); dialog.remove(); @@ -354,7 +377,11 @@ $(function () { dialog.remove(); }) } else { - var ok = $('', {class: 'weui_btn_dialog primary', text: 'OK',style:'background: #0bb20c;color: #fff;'}).appendTo(ft); + var ok = $('', { + class: 'weui_btn_dialog primary', + text: 'OK', + style: 'background: #0bb20c;color: #fff;' + }).appendTo(ft); ok.click(function () { dialog.remove(); }) diff --git a/src/main/ui/static/templates/payment/v1/retail_jsapi.js b/src/main/ui/static/templates/payment/v1/retail_jsapi.js index cae38c388..6c42a8d43 100644 --- a/src/main/ui/static/templates/payment/v1/retail_jsapi.js +++ b/src/main/ui/static/templates/payment/v1/retail_jsapi.js @@ -56,7 +56,7 @@ $(document).ready(function () { }, function (res) { dataCache.paying = false; if (res.err_msg == 'get_brand_wcpay_request:ok') { - location.href = window.redirect + (window.redirect.indexOf('?') < 0 ? '?' : '&') + 'success=true'; + startCheckOrder(window.order_id) } else { if (res.err_msg != 'get_brand_wcpay_request:cancel' && res.err_msg != 'get_brand_wcpay_request:fail') { alert('WeChat Error:' + res.err_msg); @@ -69,4 +69,22 @@ $(document).ready(function () { $('#wdiv').hide(); }) } + + function startCheckOrder(orderId) { + function checkOrderStd() { + $.ajax({ + url: '/api/v1.0/payment/orders/' + orderId + '/status', + method: 'GET', + dataType: 'json', + success: function (res) { + if (res.paid) { + location.href = window.redirect + (window.redirect.indexOf('?') < 0 ? '?' : '&') + 'success=true'; + } else { + setTimeout(checkOrderStd, 500); + } + } + }) + } + checkOrderStd(); + } }); \ No newline at end of file