import { UTSiOSHookProxy } from "DCloudUniappRuntime"; import { UIApplication } from "UIKit" import { AlipaySDK } from "AlipaySDK" import { URL, NSUserActivity, NSUserActivityTypeBrowsingWeb } from "Foundation" const defaultErrorCode : number = 700716; const errorCodeMap : Map = new Map([ ['8000', 700710], ['4000', 700711], ['5000', 700712], ['6001', 700713], ['6002', 700714], ['6004', 700715] ]); export class Alipay implements UTSiOSHookProxy { options ?: RequestPaymentOptions // 通过 url scheme 方式唤起 app 时的回调函数。 applicationOpenURLOptions(app : UIApplication | null, url : URL, options : Map | null = null) : boolean { if (url.host == 'safepay') { AlipaySDK.defaultService().processOrder(withPaymentResult = url, standbyCallback = (resultDic ?: Map) : void => { this.handlePaymentResult(resultDic) }) } return true } // 当应用程序接收到与用户活动相关的数据时调用此方法,例如,当用户使用 Universal Link 唤起应用时。 applicationContinueUserActivityRestorationHandler(application : UIApplication | null, userActivity : NSUserActivity | null, restorationHandler : ((res : [any] | null) => void) | null = null) : boolean { if (userActivity?.activityType == NSUserActivityTypeBrowsingWeb) { AlipaySDK.defaultService().handleOpenUniversalLink(userActivity, standbyCallback = (resultDic ?: Map) : void => { this.handlePaymentResult(resultDic) }) } return true } requestPayment(options : RequestPaymentOptions) { this.options = options AlipaySDK.defaultService().payOrder(options.orderInfo, fromScheme = "uniAlipay", fromUniversalLink = "", callback = (resultDic ?: Map) : void => { this.handlePaymentResult(resultDic) }) } handlePaymentResult(resultDic ?: Map) { let resultStatus : string = '' if (resultDic == null) { resultStatus = defaultErrorCode.toString() } else { resultStatus = resultDic!.get("resultStatus") as string if (resultStatus == null) { resultStatus = defaultErrorCode.toString() } } if (resultStatus == "9000") { let res : RequestPaymentSuccess = { data: resultDic } this.options?.success?.(res) this.options?.complete?.(res) } else { let code = errorCodeMap[resultStatus]; if (code == null) { code = defaultErrorCode } let err = new RequestPaymentFailImpl(code!); this.options?.fail?.(err) this.options?.complete?.(err) } } }