PaymentService

The IAPMiniProgram SDK calls the PaymentService SPI to initiate the payment process and handle the payment result. For details on how the SPI functions, refer to the PaymentService workflow.

This SPI provides the pay method. The method details are as follows:

pay

The IAPMiniprogram SDK calls the pay method to initiate the payment process.

Method signature

copy
pay(context: APIContext, request: PaymentRequest): Promise<PaymentResult>;

Request parameters

Field

Data type

Required

Description

request

PaymentRequest

Yes

An object that specifies the payment information.

context

APIContext

Yes

A context object, which carries the mini program runtime metadata.

Response parameters

Field

Data type

Required

Description

result

Promise<PaymentResult>

Yes

The result of the pay request.

Error codes

Error code

Error message

1000

ERROR_CODE_UNKNOWN_ERROR

Unknown error

1001

ERROR_CODE_USER_CANCEL

The user cancels the operation.

1002

ERROR_CODE_APP_SERVICE_ERROR

The app service is wrong.

1003

ERROR_CODE_TIMEOUT

Timeout

1005

ERROR_CODE_SYSTEM_ERROR

System error

2001

ERROR_CODE_AUTH_PENDING_AGREEMENT

Authorization is not finished or is pending.

Samples

The following samples show how to implement the PaymentService SPI to enable payment processing capabilities:

  1. Create a class that implements the PaymentService interface.
  2. Within this class, implement the pay method to initiate the payment process and request for payment with the type of order, and handle the payment result.

The samples differ depending on whether to open a React Native page during the SPI call:

Open a React Native page

copy
import {
  type APIContext,
  type PaymentRequest,
  type PaymentResult,
} from 'iapminiprogram-rn';

import type { PaymentService } from 'iapminiprogram-rn';
class TestPaymentService implements PaymentService {
  async pay(
    context: APIContext,
    request: PaymentRequest
  ): Promise<PaymentResult> {
    return new Promise<PaymentResult>(function (resolve) {
      const result: PaymentResult = {
        showReactNativePage: true,
        bundleURLPath: 'XXX',
        jsMainModulePath: 'XXX',
        moduleName: 'XXX',
        resultCode: '',
        resultMessage: '',
      };
      resolve(result);
    });
  }
}

Remain inside the mini program

copy
import {
  type APIContext,
  type PaymentRequest,
  type PaymentResult,
} from 'iapminiprogram-rn';

import type { PaymentService } from 'iapminiprogram-rn';
class TestPaymentService implements PaymentService {
  async pay(
    context: APIContext,
    request: PaymentRequest
  ): Promise<PaymentResult> {
    return new Promise<PaymentResult>(function (resolve) {
      const result: PaymentResult = {
        resultCode: '',
        resultMessage: '',
      };
      resolve(result);
    });
  }
}