index.uts 4.7 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4
import { UTSiOSHookProxy } from "DCloudUniappRuntime";
import { UIApplication } from "UIKit"
import { AlipaySDK } from "AlipaySDK" assert { type: "implementationOnly" };
import { URL, NSUserActivity, NSUserActivityTypeBrowsingWeb, Bundle } from "Foundation"
DCloud-yyl's avatar
DCloud-yyl 已提交
5
import { UniPaymentAlipayProvider } from './interface.uts'
DCloud-yyl's avatar
DCloud-yyl 已提交
6

DCloud-yyl's avatar
DCloud-yyl 已提交
7 8 9 10 11 12 13 14
const defaultErrorCode : number = 700000;

const errorCodeMap : Map<string, number> = new Map([
	['8000', 700600],
	['4000', 701100],
	['5000', 701110],
	['6001', 700601],
	['6002', 700602],
DCloud-yyl's avatar
DCloud-yyl 已提交
15 16 17
	['6004', 700603],
	['6007', 700607],
	['4001', 700608]
DCloud-yyl's avatar
DCloud-yyl 已提交
18 19
]);

DCloud-yyl's avatar
DCloud-yyl 已提交
20
export class UniPaymentAlipayProviderImpl implements UniPaymentAlipayProvider {
DCloud-yyl's avatar
DCloud-yyl 已提交
21 22
	id: string
	
DCloud-yyl's avatar
DCloud-yyl 已提交
23
	override description: string = "支付宝"
DCloud-yyl's avatar
DCloud-yyl 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	
	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<UIApplication.OpenURLOptionsKey, any> | 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<AnyHashable, any>) : void => {
				this.handlePaymentResult(resultDic)
			})
		}
	}
	
	@UTSiOS.keyword("fileprivate")
	handleOpenUniversalLink(userActivity : NSUserActivity | null) {
		if (userActivity?.activityType == NSUserActivityTypeBrowsingWeb) {
			AlipaySDK.defaultService().handleOpenUniversalLink(userActivity, standbyCallback = (resultDic ?: Map<AnyHashable, any>) : void => {
				this.handlePaymentResult(resultDic)
			})
		}
	}
	
	private payOrder() {
		AlipaySDK.defaultService().payOrder(this.options?.orderInfo, fromScheme = this.getApplicationScheme(), fromUniversalLink = this.getApplicationUniversalLink(), callback = (resultDic ?: Map<AnyHashable, any>) : 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<string, any>[] | 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<string, any> | null
			if (alipay != null) {
				universalLink = alipay!['universalLink'] as string | null
			}
		}
		return universalLink
	}

	private handlePaymentResult(resultDic ?: Map<AnyHashable, any>) {
		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)
		}
	}
}