PaymentService

IAPMiniProgram SDK calls the PaymentService SPI to initiate the payment process and handle the payment result.

pay

Method signature

IAPMiniprogram SDK calls the getAuthCode method to initiate the payment process.

copy
void pay(PaymentRequest paymentRequest, APIContext apiContext, Callback<PaymentResult> callback)

Request parameters

Field

Data type

Description

Required

paymentRequest

PaymentRequest

The request object to initiate a payment.

Yes

apiContext

APIContext

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

Yes

callback

Callback (For details, see the following section)

The callback that is executed when the request processing is complete.

Yes

callback

Field

Data type

Description

Required

result

PaymentResult

The result of the pay request.

Yes

Response parameters

N/A

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 call the PaymentService SPI to initiate the payment process and handle the payment result.

  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.

Java

copy
public class PaymentServiceImpl implements PaymentService  {
    @Override
    public void pay(PaymentRequest paymentRequest, APIContext apiContext, Callback<PaymentResult> callback) {
        // to payment code
        callback.result(new PaymentResult(resultCode, resultMessage));
    }
}

Kotlin

copy
class PaymentServiceImpl : PaymentService {
    override fun pay(paymentRequest: PaymentRequest?, apiContext: APIContext?, callback: Callback<PaymentResult>?) {
        // to payment code
        callback?.result(PaymentResult(resultCode,resultMessage))
    }
}