This module provides APIs for directly starting the third-party payment cashier through dependency packages.
Model restriction: This API can be used only in the stage model.
Atomic service API: This API can be used in atomic services since version 6.0.0(20).
System capability: SystemCapability.Payment.ThirdPaymentService
Since: 6.0.0(20)
Enumerates the third-party payment methods.
Model restriction: This API can be used only in the stage model.
Atomic service API: This API can be used in atomic services since version 6.0.0(20).
System capability: SystemCapability.Payment.ThirdPaymentService
Since: 6.0.0(20)
| Name | Value | Description |
|---|---|---|
| WECHAT_PAY | 'wechat_pay' | WeChat Pay. |
| ALI_PAY | 'ali_pay' | Alipay. |
| WECHAT_MINI_PROGRAM | 'wechat_mini_program' | WeChat Mini Program. |
Describes the payment request client.
Model restriction: This API can be used only in the stage model.
Atomic service API: This API can be used in atomic services since version 6.0.0(20).
System capability: SystemCapability.Payment.ThirdPaymentService
Since: 6.0.0(20)
constructor(context: common.UIAbilityContext, payMethod: PayMethod, thirdAppId: string);
Describes the constructor used to construct a ThirdPayClient instance.
Model restriction: This API can be used only in the stage model.
Atomic service API: This API can be used in atomic services since version 6.0.0(20).
System capability: SystemCapability.Payment.ThirdPaymentService
Since: 6.0.0(20)
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| context | common.UIAbilityContext | Yes | UIAbility context. |
| payMethod | PayMethod | Yes | Payment method. |
| thirdAppId | string | Yes | ID of the third-party payment app. |
Example
import { thirdPaymentService } from '@kit.PaymentKit';
import { common } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
@State private thirdPayClient: thirdPaymentService.ThirdPayClient | null = null;
aboutToAppear() {
try {
const thirdAppid ='third_appid_123456'
// Initialize the third-party payment client.
this.thirdPayClient = new thirdPaymentService.ThirdPayClient(
this.getUIContext().getHostContext() as common.UIAbilityContext,
thirdPaymentService.PayMethod.WECHAT_PAY,
thirdAppid
);
} catch (error) {
console.error ('Initialization of the payment client fails:', error);
// Prompt the user or jump to the error page.
}
}
payButtonClicked() {
if (!this.thirdPayClient) {
console.error ('The payment client is not initialized.');
return;
}
// Call the payment API to pass the payment information.
this.thirdPayClient.pay('{"xxx1":"***", "xxx2":"***", "token":"***"}');
}
build() {
Column() {
Button ('Pay Now')
.onClick(() => {
this.payButtonClicked();
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
} pay(payInfo: string): Promise<void>;
Starts the third-party payment cashier. Before calling this API, ensure that the network is connected. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
Atomic service API: This API can be used in atomic services since version 6.0.0(20).
System capability: SystemCapability.Payment.ThirdPaymentService
Since: 6.0.0(20)
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| payInfo | string | Yes | Payment information passed to the cashier, which is in JSON string format. The parameters is passed according to the requirements of the third-party payment cashier. For details, see payInfo. For example, {"xxx1":"", "xxx2":"", "token":"*******"}**. |
Returns
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 801 | Capability not supported. Failed to call the API due to limited device capabilities. |
| 1022830000 | The operation was canceled by the user. |
| 1022830001 | Pay failed. |
| 1022830002 | The payInfo invalid. Possible causes: 1.Data format is not json string; 2.Mandatory parameters are left unspecified. |
Example
For details about how to obtain the context in the example, see Obtaining the Context of UIAbility.
import { thirdPaymentService } from '@kit.PaymentKit';
import { common } from '@kit.AbilityKit';
export let thirdPayClient: thirdPaymentService.ThirdPayClient | undefined = undefined;
@Entry
@Component
struct Index {
context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
thirdPaymentServicePayPromise() {
thirdPayClient = new thirdPaymentService.ThirdPayClient(this.context, thirdPaymentService.PayMethod.WECHAT_PAY, 'appid_123456');
// The following is an example of constructing parameters for different payment methods:
// PayMethod.WECHAT_PAY: '{"appId":"***","partnerId":"***","prepayId":"***","packageValue":"***","nonceStr":"***","timeStamp":"***","sign":"***","extData":"***","token":"***"}'
// PayMethod.ALI_PAY: '{"orderInfo":"***", "token":"***"}'
// PayMethod.WECHAT_MINI_PROGRAM: '{"userName":"original ID", "path":"mini program start path", "miniProgramType":"mini program type, 0: official version; 1: development version; 2: trial version. Default value : 0", "extData":"***", "token":"***"}'
const payInfo = '{"xxx1":"***", "xxx2":"***", "token":"***"}';
thirdPayClient.pay(payInfo).then(() => {
// The payment is successful.
console.info('succeeded in paying.');
});
}
build() {
Column() {
Button('thirdPaymentServicePayPromise')
.type(ButtonType.Capsule)
.width('50%')
.margin(20)
.onClick(() => {
this.thirdPaymentServicePayPromise();
})
}
.width('100%')
.height('100%')
}
} handlePayCallback(want: Want): boolean;
Provides a callback for handling the payment result. Before calling this API, ensure that the network is connected. This API returns a value of the Boolean type.
Model restriction: This API can be used only in the stage model.
Atomic service API: This API can be used in atomic services since version 6.0.0(20).
System capability: SystemCapability.Payment.ThirdPaymentService
Since: 6.0.0(20)
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| want | Want | Yes | Carrier for information transfer between app components. |
Returns
| Type | Description |
|---|---|
| boolean | Result returned by the callback, which is the payment handling result, not the actual payment result. The actual payment result is subject to the third-party payment result. - true: The payment is successful. - false: The payment fails. |
Error codes
For details about the error codes, please refer to ArkTS API Error Codes.
| ID | Error Message |
|---|---|
| 801 | Capability not supported. Failed to call the API due to limited device capabilities. |
Example
import { UIAbility, Want } from '@kit.AbilityKit';
// Import the third-party payment client object from the thirdPayClient object definition file. The following is an example. The actual path depends on the app.
import { thirdPayClient } from '../pages/thirdPaymentServicetest';
// If a class ability has been implemented, you can directly add onNewWant.
export default class EntryAbility extends UIAbility {
onNewWant(want: Want): void {
// The third-party payment client object must be the same as that starting the cashier.
if (thirdPayClient) {
console.info('clientForThirdPayment handlePayCallback');
let handlePayCallback = thirdPayClient.handlePayCallback(want);
console.info(`clientForThirdPayment handlePayCallback result: ${handlePayCallback}`);
}
}
}