4. Applying payment using jsapi:tradePay
About the AlipayJSAPI
AlipayJSAPI is a set of APIs for external developers based on the AlipayJSBridge package. The goal is to unify the AlipayJSBridge interface naming, parameterization, processing of AlipayJSBridge faculties, reducing container development costs and customer service costs. Only compatible with Alipay APIs above 10.0 version10.0 (For APIs below version 10.0, you have to design ad hoc methods to use). AlipayJSAPI provides a wealth of Alipay native APIs, you can easily use Alipay's ability to realize the experience of native applications, such as page jump, payment function.
Installation
On your H5 page, add the following code:
copy
<script src="https://a.alipayobjects.com/g/h5-lib/alipayjsapi/3.0.3/alipayjsapi.min.js"></script>
API
copy
AlipayJSBridge.call('tradePay', {
tradeNO
}, fn);
Request parameters:
Name | Type | Description | Required | Version |
tradeNO | string | the Alipay Trade number | Y | 8.0 |
fn | function | The result callback after the cashier is closed. | N |
Response Parameters:
Callback function parameters: {resultCode, callbackUrl, memo, result}
Name | Type | Description | Version |
resultCode | string | Payment result. ‘9000’: The payment succeeds; ‘8000’:The payment is in process; ‘4000’: The payment fails; ‘6001’: Canceled by the uer; ‘6002’: Network connection error; ‘99’: The user clicks "Foeget the password" and exits (only apply to iOS above version 9.5) | 8.0 |
callbackUrl | bool | The URL to be redirected to after the trade succeeds. Null in most cases. | 8.0 |
memo | bool | The memo string returned by the cashier server | iOS 8.0, android 8.4 |
result | bool | The result string returned by the cashier server | iOS 8.0, android 8.4 |
DEMO Code
copy
<h1>点击以下按钮唤起收银台支付</h1>
<a href="javascript:void(0)" class="tradeno">交易号唤起</a>
<script>
function ready(callback) {
// 如果jsbridge已经注入则直接调用
if (window.AlipayJSBridge) {
callback && callback();
} else {
// 如果没有注入则监听注入的事件
document.addEventListener('AlipayJSBridgeReady', callback, false);
}
}
ready(function(){
document.querySelector('.tradeno').addEventListener('click', function(){
AlipayJSBridge.call("tradePay",{
tradeNO: "201209071234123221"
}, function(result){
alert(JSON.stringify(result));
});
});
});
</script>