思路:
具體實施步驟:
//向微信注冊
- (BOOL) application: (UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
[WXApi registerApp: @"你的appID" universalLink: @"服務(wù)器地址"];
}
- (void) weChatPay {
[NSDictionary micDic = [NSDictionary alloc] init];
[micDic setValue:useId forKey: @"userId"];
[self.myViewModel appweChatpay: micDic requestResult:^(NSString * _Nonnull string, BOOL success) { //appweChatpay 支付接口
if (success) {
if (string != nil || string.length != 0) {
NSRange range = [string rangeOfString: @"?"];
NSString *rightStr = [string substringFromIndex: range.location+1];
NSArray *objArray = [rightStr componentsSeparatedByString: @"&"];
[NSDictionary *paramDic = [NSDictionary alloc] init];
for (int i=0; i<objArray.count; i++) {
NSString *li = objArray[i];
NSArray *key = [li componentsSeparatedByString: @"="];
[paramDic setValue:key[1] forKey: key[0]];
}
PayReq *request = [[PayReq alloc] init]; //request的屬性在PayReq中定義 key的值要和接口返回的值一致
request.partnerId = [paramDic valueForKey:@"partnerId"];
/** 預(yù)支付訂單 從服務(wù)器獲取 */
request.prepayId = [paramDic valueForKey:@"prepayId"];
/** 商家根據(jù)財付通文檔填寫的數(shù)據(jù)和簽名 <暫填寫固定值Sign=WXPay>*/
request.package = [paramDic valueForKey:@"package"];
/** 隨機(jī)串,防重發(fā) */
request.nonceStr = [paramDic valueForKey:@"nonceStr"];
/** 時間戳,防重發(fā) */
request.timeStamp = [paramDic valueForKey:@"timeStamp"];
/** 商家根據(jù)微信開放平臺文檔對數(shù)據(jù)做的簽名, 可從服務(wù)器獲取霞溪,也可本地生成*/
request.sign = [paramDic valueForKey:@"sign"];
[WXApi sendReq: request completion: nil];
}
}
}];
}
- 成功調(diào)起接口之后 在AppDelegate.m中進(jìn)行回調(diào)
//支付返回結(jié)果
- (void)onResp:(BaseResp *)resp {
if ([resp isKindOfClass:[PayResp class]]) {
PayResp *payresp = (PayResp *)resp;
[[NSNotificationCenter defaultCenter]
postNotificationName:wxpayResultNotification object:@{@"resp":payresp}];
}
}
//進(jìn)行支付回調(diào)
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
return [WXApi handleOpenUrl: url delegate:self];
}