在我们的App中是很经常的会用到微信的两个功能,1就是分享,2就是支付(通常会和支付宝、银联支付放在一起使用),通常接入这种三方的东西,对于移动端来说,过程都比较简单,但是对于后台来说,一般也不是说难,就是步骤多,配置的东西比移动端多(像比如支付过程), 废话不多说,由于我这里的分享只需要做微信的分享,所以过程比较简单,配置什么的就不多说了,自己去看微信支付的UML流程图和微信SDK的配置流程,对于iOS端的代码如下:
首先在工程里面配置白名单:
AppDelegate里面初始化微信SDK:
1
| [WXApi registerApp:kWeChatID];
|
支付or分享代码:
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
| [ShareView shareViewWithCompleteHandel:^(NSInteger index) { WXMediaMessage * message = [WXMediaMessage message]; message.title = [NSString stringWithFormat:@"%@[%@k-%@k]",rel[@"job_name"],rel[@"salary_min"],rel[@"salary_max"]]; message.description = [NSString stringWithFormat:@"入职快,加薪快,还能赚外快\n%@",rel[@"com_name"]]; [message setThumbImage:image]; WXWebpageObject * webpageObject = [WXWebpageObject object]; webpageObject.webpageUrl = urlString; message.mediaObject = webpageObject; SendMessageToWXReq * req = [[SendMessageToWXReq alloc] init]; req.bText = NO; req.message = message; req.scene = index == 0 ? WXSceneSession :index == 1 ? WXSceneTimeline:WXSceneFavorite; [WXApi sendReq:req]; }];
// 支付,note:这里的repsonse是后台生成的支付参数,下订单是请求自己的服务端拿到这些参数: if ([responese isKindOfClass:[NSDictionary class]]) { NSDictionary *payParams = (NSDictionary *)responese; PayReq * req = [[PayReq alloc] init]; req.partnerId = payParams[@"partnerid"]; req.prepayId = payParams[@"prepayid"]; req.nonceStr = payParams[@"noncestr"]; req.timeStamp = [payParams[@"timestamp"] intValue]; req.package = payParams[@"package"]; req.sign = payParams[@"sign"]; [WXApi sendReq:req]; }
|
支付or分享结果的回调:
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
| // MARK: - 处理三方回调 - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options { //判断是微信还是支付宝操作 if ([url.absoluteString rangeOfString:kWeChatID].location != NSNotFound){ return [WXApi handleOpenURL:url delegate:self]; } else { if ([url.host isEqualToString:@"safepay"]) { //支付宝支付回调自己App的结果 [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) { NSLog(@"result = %@",resultDic); }]; } return YES; } }
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { if ([url.absoluteString rangeOfString:kWeChatID].location != NSNotFound) { return [WXApi handleOpenURL:url delegate:self]; } else if ([url.host isEqualToString:@"safepay"]) { [[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) { NSLog(@"result = %@",resultDic); }]; } return YES; }
// MARK: - WXApiDelegate - (void)onResp:(BaseResp *)resp { if ([resp isKindOfClass:[PayResp class]]){ //支付 PayResp *response = (PayResp*)resp; switch(response.errCode){ case 0: //服务器端查询支付通知或查询API返回的结果再提示成功 [[NSNotificationCenter defaultCenter] postNotificationName:kWeChatPaySucessNotificationName object:self userInfo:@{@"PayResp":resp}]; [_window.rootViewController alertWithTitle:@"支付成功!" complete:nil]; break; case -1: case -2: { [_window.rootViewController alertWithTitle:@"支付失败!" complete:nil]; } break; default: NSLog(@"支付失败,retcode=%d",resp.errCode); break; } } else if ([resp isKindOfClass:[SendMessageToWXResp class]]) { //分享 SendMessageToWXResp *sendMessageRes = (SendMessageToWXResp *)resp; if (sendMessageRes.errCode == 0 ) { [_window.rootViewController alertWithTitle:@"分享成功!" complete:nil]; } else if (sendMessageRes.errCode == WXErrCodeUserCancel) { [_window.rootViewController alertWithTitle:@"您取消了分享!" complete:nil]; } else { [_window.rootViewController alertWithTitle:@"分享失败!" complete:nil]; } } }
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { if ([url.host isEqualToString:@"safepay"]) { //跳转支付宝钱包进行支付,处理支付结果 [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) { NSLog(@"result = %@",resultDic); }]; } return YES; }
|
集成起来比较简单,由于这里的支付请求加密参数是放在后台进行的(也理论上是后台做比较安全,为了防止反编译,一般是后台做)App端做的事情相对来来说还是比较少的。