index.uts 5.5 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
import { UTSiOSHookProxy } from "DCloudUniappRuntime";
import { UIApplication } from "UIKit";
import { URL, NSUserActivity } from "Foundation";

export const requestPayment : RequestPayment = function (options : RequestPaymentOptions) {
	Wxpay.requestPayment(options)
};

const wxDefaultErrorCode : number = 700000

const wxErrorCodeMap : Map<number, number> = new Map([
	[-1, 701100],
	[-2, 700601]
])

export class WxpayHookProxy implements UTSiOSHookProxy {
	// 应用正常启动时 (不包括已在后台转到前台的情况)的回调函数。
	applicationDidFinishLaunchingWithOptions(application : UIApplication | null, launchOptions : Map<UIApplication.LaunchOptionsKey, any> | null = null) : boolean {
		Wxpay.share.registerApp()
		return false
	}

	// 通过 url scheme 方式唤起 app 时的回调函数。
	applicationOpenURLOptions(app : UIApplication | null, url : URL, options : Map<UIApplication.OpenURLOptionsKey, any> | null = null) : boolean {
		Wxpay.share.handleOpen(url)
		return true
	}

	// 当应用程序接收到与用户活动相关的数据时调用此方法,例如,当用户使用 Universal Link 唤起应用时。
	applicationContinueUserActivityRestorationHandler(application : UIApplication | null, userActivity : NSUserActivity | null, restorationHandler : ((res : [any] | null) => void) | null = null) : boolean {
		Wxpay.share.handleOpenUniversalLink(userActivity)
		return true
	}
}

export class Wxpay implements WXApiDelegate {
	static share = new Wxpay()

	private options ?: RequestPaymentOptions

	@UTSiOS.keyword("fileprivate")
	registerApp() {
		const scheme = Wxpay.share.getApplicationScheme()
		const universalLink = Wxpay.share.getApplicationUniversalLink()
		if (scheme != null && universalLink != null) {
			WXApi.registerApp(scheme!, universalLink = universalLink!) 
		}
	}

	@UTSiOS.keyword("fileprivate")
	handleOpen(url : URL) {
		WXApi.handleOpen(url, delegate = this)
	}

	@UTSiOS.keyword("fileprivate")
	handleOpenUniversalLink(userActivity : NSUserActivity | null) {
		if (userActivity != null) {
			WXApi.handleOpenUniversalLink(userActivity!, delegate = this)
		}
	}

	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 == "wechat") {
						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 wechat = infoDictionary!['WeChat'] as Map<string, any> | null
			if (wechat != null) {
				universalLink = wechat!['universalLink'] as string | null
			}
		}
		return universalLink
	}
	
	//@brief 检查微信是否已被用户安装
	private isWXAppInstalled() : boolean {
		return WXApi.isWXAppInstalled()
	}
	
	//@brief 发送一个sendReq后,收到微信的回应
	onResp(resp : BaseResp) {
		if (resp.errCode == 0) {
			let res : RequestPaymentSuccess = {
				data: resp
			}
			this.options?.success?.(res)
			this.options?.complete?.(res)
		} else {
			const errCode = resp.errCode as number
			let code = wxErrorCodeMap[errCode];
			if (code == null) {
				code = wxDefaultErrorCode
			}
			let err = new RequestPaymentFailImpl(code!);
			this.options?.fail?.(err)
			this.options?.complete?.(err)
		}
	}

	static requestPayment(options : RequestPaymentOptions) {
		Wxpay.share.options = options

		if (Wxpay.share.isWXAppInstalled() == false) {
			let err = new RequestPaymentFailImpl(wxDefaultErrorCode);
			Wxpay.share.options?.fail?.(err)
			Wxpay.share.options?.complete?.(err)
			return
		}

		if (Wxpay.share.getApplicationScheme() == null) {
			let err = new RequestPaymentFailImpl(700800);
			Wxpay.share.options?.fail?.(err)
			Wxpay.share.options?.complete?.(err)
			return
		}

		if (Wxpay.share.getApplicationUniversalLink() == null) {
			let err = new RequestPaymentFailImpl(700801);
			Wxpay.share.options?.fail?.(err)
			Wxpay.share.options?.complete?.(err)
			return
		}

		if (Wxpay.share.options != null) {
			const params = JSON.parse(Wxpay.share.options!.orderInfo) as UTSJSONObject
			const partnerId = params.getString("partnerid")
			const prepayId = params.getString("prepayid")
			const packageV = params.getString("package")
			const nonceStr = params.getString("noncestr")
			const timeStamp = params.getNumber("timestamp")
			const sign = params.getString("sign")

			let request = new PayReq();
			if (partnerId != null) {
				request.partnerId = partnerId!
			}
			if (prepayId != null) {
				request.prepayId = prepayId!
			}
			if (packageV != null) {
				request.package = packageV!
			}
			if (nonceStr != null) {
				request.nonceStr = nonceStr!
			}
			if (timeStamp != null) {
				request.timeStamp = timeStamp!.toUInt32()
			}
			if (sign != null) {
				request.sign = sign!
			}

			//函数调用后,会切换到微信的界面。第三方应用程序等待微信返回onResp。微信在异步处理完成后一定会调用onResp
			WXApi.send(request);
		}
	}
}