思路:
具体实施步骤:
//向微信注册
- (BOOL) application: (UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
[WXApi registerApp: @"你的appID" universalLink: @"服务器地址"];
}
- (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"];
/** 预支付订单 从服务器获取 */
request.prepayId = [paramDic valueForKey:@"prepayId"];
/** 商家根据财付通文档填写的数据和签名 <暂填写固定值Sign=WXPay>*/
request.package = [paramDic valueForKey:@"package"];
/** 随机串,防重发 */
request.nonceStr = [paramDic valueForKey:@"nonceStr"];
/** 时间戳,防重发 */
request.timeStamp = [paramDic valueForKey:@"timeStamp"];
/** 商家根据微信开放平台文档对数据做的签名, 可从服务器获取,也可本地生成*/
request.sign = [paramDic valueForKey:@"sign"];
[WXApi sendReq: request completion: nil];
}
}
}];
}
- 成功调起接口之后 在AppDelegate.m中进行回调
//支付返回结果
- (void)onResp:(BaseResp *)resp {
if ([resp isKindOfClass:[PayResp class]]) {
PayResp *payresp = (PayResp *)resp;
[[NSNotificationCenter defaultCenter]
postNotificationName:wxpayResultNotification object:@{@"resp":payresp}];
}
}
//进行支付回调
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
return [WXApi handleOpenUrl: url delegate:self];
}