This topic describes how to integrate Captcha into an Alipay mini program and explains the related interfaces.
Preparations
Environment requirements
Developer tool |
Alipay Developer Tool |
Base library v1.x for debugging |
1.25.1 or later |
Base library v2.x for debugging |
2.7.2 or later |
Download the SDK
Log on to the Phone Number Verification Service console. On the Overview page, find the API&SDK area on the right and click Download Now. On the API&SDK page, download and decompress the SDK as instructed.
Sample demo
A sample demo is available for integrating the Captcha feature into an Alipay mini program. Refer to the sample code in the demo to quickly understand the integration steps. To download the demo, see Alipay mini program integration demo.
Create a verification plan
Log on to the Phone Number Verification Service console.
In the left navigation pane, choose Integrated Verification (Based on Atomic Capabilities) > Captcha Service > Captcha Plan Management.
On the Captcha Plan Management page, click Add Captcha Plan.
Enter a Plan Name, set Client Type to H5, and create the Captcha plan. This provides the key parameters, such as
appIdandappKey.NoteThe Client Type setting is for tagging purposes only and does not affect the use of the plan ID.
Integration steps
Import the component
Add the domain name
https://captcha.alicaptcha.comto the whitelist in the backend of the Alipay mini program.In the Alipay Developer Tool, go to Details > Mini Program Configuration to enable component2.
In the
xxxx.jsonfile of the page that requires verification, import the component. The following code provides an example. For more information, see theindex.jsonfile in the demo.{ "usingComponents": { "captcha4": "/component/captcha4/captcha4" } }
Initialization
In the xxxx.axml file of the page that requires verification, import the component. The parameters in the following example are required.
<captcha4 ref="captcha" my:if="{{loadCaptcha}}" captchaId="{{captchaId}}"/>
Component parameters:
Parameter |
Required |
Type |
Description |
captchaId |
Yes |
string |
The Captcha ID. This is the |
language |
No |
string |
The language. Default value: zh. |
useNativeButton |
No |
boolean |
Specifies whether to use a button to trigger the verification. Valid values:
|
riskType |
No |
string |
If integrated risk control is configured on the server-side, this field can specify the verification type.
Important
This parameter is not yet effective because the integrated risk control mode is not available. |
hideBar |
No |
array |
Hides the Close and Refresh buttons on the verification interface. Valid values: close, refresh. |
scale |
No |
number |
The scaling ratio. Default value: 1. |
mask |
No |
object |
Configures whether to close the verification pop-up when a user clicks outside the Captcha area, and sets the background color of the pop-up. Default value: |
hideSuccess |
No |
boolean |
Specifies whether to hide the success pop-up in bind mode. Default value: false. |
The following code shows how to bind an event listener. For complete event descriptions and code examples, see Event descriptions later in this topic.
<captcha4
id="captcha"
my:if="{{loadCaptcha}}"
captchaId="{{captchaId}}"
onSuccess="captchaSuccess"
onReady="captchaReady"
onClose="captchaClose"
onError="captchaError"
onFail="captchaFail"
/>
In buttonless mode, you must manually call the showCaptcha method to trigger the Captcha verification. The following code provides an example. For more information, see the index.js file in the demo.
captcha(ref){ // Get the ref
this.captcha = ref
}
verify: function () { // Open the Captcha
this.captcha.showCaptcha()
},
Obtain the success credential
After the Captcha is successfully completed, the user-defined captchaSuccess function is triggered. Store the verification result in the result variable. You can use a custom storage method. The result is then fetched and uploaded for secondary authentication. In buttonless mode, you can perform secondary authentication directly at this step.
captchaSuccess: function(result) {
console.log("captcha-Success!");
this.setData({
result: result
})
}
Submit for secondary validation
When the user clicks the submit button, secondary validation is triggered.
captchaValidate: function() {
var self = this;
var data = self.data.result; // Get the verification result stored when the Captcha was completed
if (typeof data !== 'object') {
console.log("Please complete the verification first!");
return;
}
// Submit the result to your server for secondary validation
my.request({
url: "your_business_interface", // Replace with the actual URL
method: 'POST',
dataType: 'json',
data: Object.assign({}, data, {
captcha_id: self.data.captchaId
}),
success: function(res) {
my.showToast({
title: res.data.result
});
},
fail: function() {
console.log('error');
}
});
}
Event descriptions
onReady
Listens for the event that indicates the DOM for the verification button is generated.
// axml
<captcha4 onReady="captchaReady"/>
// js
captchaReady: function () {
console.log('captcha-Ready!')
}
onError
Listens for verification errors. The Error callback is triggered for errors that Captcha can catch, such as too many refreshes, static resource loading failures, or poor network connectivity. The following code provides an example. The error returns an object `e`, where `e.detail` contains two properties: `code` (error code) and `tips` (error message).
// axml
<captcha4 onError="captchaError"/>
// js
captchaError: function (e) {
console.log('captcha-Error!', e.detail)
}
onSuccess
Listens for the successful verification event. This event returns a result object. The `detail` property of the result object contains four properties: `lot_number`, `pass_token`, `captcha_output`, and `gen_time`. These parameters are the credentials for the successful verification and must be passed for secondary validation.
// axml
<captcha4 onSuccess="captchaSuccess"/>
// js
captchaSuccess: function (result) {
console.log('captcha-Success!')
// Store the parameters from the result to be passed for secondary validation
this.setData({
result: result.detail
})
}
onClose
This callback is triggered when the user closes the verification pop-up.
// axml
<captcha4 onClose="captchaClose"/>
// js
captchaClose: function () {
console.log('captcha-Close!')
}
onFail
Listens for the verification failure event.
// axml
<captcha4 onFail="captchaFail"/>
// js
captchaFail: function () {
console.log('captcha-Fail!')
}
Methods
Method Name |
Description |
showCaptcha |
Call this method only when `useNativeButton` is `false`. It displays the Captcha pop-up. |
reset |
Resets the Captcha. |
Abnormal conditions (breakdown)
If the server cannot be accessed during Captcha initialization because of client network issues or other problems, the breakdown process is triggered. During a breakdown, the Captcha verification passes immediately, and the Success event is triggered instantly. Using the onSuccess event as an example, the e.detail object contains lot_number, pass_token, captcha_output, gen_time, and is_offline. In this case, the value of is_offline is fixed to true.