import { UTSiOSHookProxy } from "DCloudUniappRuntime"; import { UIApplication } from "UIKit" import { AlipaySDK } from "AlipaySDK" assert { type: "implementationOnly" }; import { URL, NSUserActivity, NSUserActivityTypeBrowsingWeb, Bundle } from "Foundation" const defaultErrorCode : number = 700000; const errorCodeMap : Map = new Map([ ['8000', 700600], ['4000', 701100], ['5000', 701110], ['6001', 700601], ['6002', 700602], ['6004', 700603] ]); export class UniPaymentAlipayProvider implements UniPaymentProvider { id: string override description: string = "Alipay" isAppExist: boolean requestPayment(options : RequestPaymentOptions) { Alipay.requestPayment(options) } constructor() { this.id = "alipay" this.isAppExist = true } } export class AlipayHookProxy implements UTSiOSHookProxy { // 通过 url scheme 方式唤起 app 时的回调函数。 applicationOpenURLOptions(app : UIApplication | null, url : URL, options : Map | null = null) : boolean { // this.processOrder(url) Alipay.share.processOrder(url) return true } // 当应用程序接收到与用户活动相关的数据时调用此方法,例如,当用户使用 Universal Link 唤起应用时。 applicationContinueUserActivityRestorationHandler(application : UIApplication | null, userActivity : NSUserActivity | null, restorationHandler : ((res : [any] | null) => void) | null = null) : boolean { Alipay.share.handleOpenUniversalLink(userActivity) return true } } export class Alipay { static share = new Alipay() private options ?: RequestPaymentOptions static requestPayment(options : RequestPaymentOptions) { Alipay.share.options = options if (Alipay.share.getApplicationScheme() == null) { let err = new RequestPaymentFailImpl(700800); Alipay.share.options?.fail?.(err) Alipay.share.options?.complete?.(err) return } Alipay.share.payOrder() } @UTSiOS.keyword("fileprivate") processOrder(url : URL) { if (url.host == 'safepay') { AlipaySDK.defaultService().processOrder(withPaymentResult = url, standbyCallback = (resultDic ?: Map) : void => { this.handlePaymentResult(resultDic) }) } } @UTSiOS.keyword("fileprivate") handleOpenUniversalLink(userActivity : NSUserActivity | null) { if (userActivity?.activityType == NSUserActivityTypeBrowsingWeb) { AlipaySDK.defaultService().handleOpenUniversalLink(userActivity, standbyCallback = (resultDic ?: Map) : void => { this.handlePaymentResult(resultDic) }) } } private payOrder() { AlipaySDK.defaultService().payOrder(this.options?.orderInfo, fromScheme = this.getApplicationScheme(), fromUniversalLink = this.getApplicationUniversalLink(), callback = (resultDic ?: Map) : void => { this.handlePaymentResult(resultDic) }) } private getApplicationScheme() : string | null { let scheme : string | null = null const infoDictionary = Bundle.main.infoDictionary if (infoDictionary != null) { const bundleURLTypes = infoDictionary!['CFBundleURLTypes'] as Map[] | null if (bundleURLTypes != null) { bundleURLTypes!.forEach((value, key) => { const urlIdentifier = value['CFBundleURLName'] as string | null if (urlIdentifier != null && urlIdentifier!.toLowerCase() == "alipay") { const urlSchemes = value['CFBundleURLSchemes'] as string[] scheme = urlSchemes[0] } }) } } return scheme } private getApplicationUniversalLink() : string | null { let universalLink : string | null = null const infoDictionary = Bundle.main.infoDictionary if (infoDictionary != null) { const alipay = infoDictionary!['Alipay'] as Map | null if (alipay != null) { universalLink = alipay!['universalLink'] as string | null } } return universalLink } private 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 } Alipay.share.options?.success?.(res) Alipay.share.options?.complete?.(res) } else { let code = errorCodeMap[resultStatus]; if (code == null) { code = defaultErrorCode } let err = new RequestPaymentFailImpl(code!); Alipay.share.options?.fail?.(err) Alipay.share.options?.complete?.(err) } } }