1.微信的sdk集成步骤很烂,先把他需要的库和文件导入项目(推荐使用pod)
2.导入支付回调的代码类:先把他的示例demo中的#import "WXApiManager.h" 这个类导入到项目,然后修改这个类的.m文件(.h不动)。
#import "WXApiManager.h"
@implementation WXApiManager
#pragma mark - LifeCycle
+(instancetype)sharedManager {
static dispatch_once_t onceToken;
static WXApiManager *instance;
dispatch_once(&onceToken, ^{
instance = [[WXApiManager alloc] init];
});
return instance;
}
- (void)dealloc {
self.delegate = nil;
// [super dealloc];
}
#pragma mark - WXApiDelegate
- (void)onResp:(BaseResp *)resp {//支付结果的回调
if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
if (_delegate
&& [_delegate respondsToSelector:@selector(managerDidRecvMessageResponse:)]) {
SendMessageToWXResp *messageResp = (SendMessageToWXResp *)resp;
[_delegate managerDidRecvMessageResponse:messageResp];
}
} else if ([resp isKindOfClass:[SendAuthResp class]]) {
if (_delegate
&& [_delegate respondsToSelector:@selector(managerDidRecvAuthResponse:)]) {
SendAuthResp *authResp = (SendAuthResp *)resp;
[_delegate managerDidRecvAuthResponse:authResp];
}
} else if ([resp isKindOfClass:[AddCardToWXCardPackageResp class]]) {
if (_delegate
&& [_delegate respondsToSelector:@selector(managerDidRecvAddCardResponse:)]) {
AddCardToWXCardPackageResp *addCardResp = (AddCardToWXCardPackageResp *)resp;
[_delegate managerDidRecvAddCardResponse:addCardResp];
}
}else if([resp isKindOfClass:[PayResp class]]){//返回支付结果,实际支付结果需要去微信服务器端查询
//resp.errCode,resp.errStr
switch (resp.errCode) {
case WXSuccess:{
NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
//发出第三方支付成功的通知(发通知都会吧,下面的发通知是我项目里的,懒得改了,自己发吧,注意后面的参数,YES代表支付成功,NO代表失败;微信比较恶心,只能在这里发个通知给调用支付的界面)
KPostNotification(kNotificationThridPaySuccess, @YES);
}break;
default:{
NSLog(@"支付失败,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
//发出第三方支付失败的通知(发通知都会吧,下面的发通知是我项目里的,懒得改了,自己发吧,注意后面的参数,YES代表支付成功,NO代表失败;微信比较恶心,只能在这里发个通知给调用支付的界面)
KPostNotification(kNotificationThridPaySuccess, @NO);
}break;
}
}
}
- (void)onReq:(BaseReq *)req {//onReq是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面。
if ([req isKindOfClass:[GetMessageFromWXReq class]]) {
if (_delegate
&& [_delegate respondsToSelector:@selector(managerDidRecvGetMessageReq:)]) {
GetMessageFromWXReq *getMessageReq = (GetMessageFromWXReq *)req;
[_delegate managerDidRecvGetMessageReq:getMessageReq];
}
} else if ([req isKindOfClass:[ShowMessageFromWXReq class]]) {
if (_delegate
&& [_delegate respondsToSelector:@selector(managerDidRecvShowMessageReq:)]) {
ShowMessageFromWXReq *showMessageReq = (ShowMessageFromWXReq *)req;
[_delegate managerDidRecvShowMessageReq:showMessageReq];
}
} else if ([req isKindOfClass:[LaunchFromWXReq class]]) {
if (_delegate
&& [_delegate respondsToSelector:@selector(managerDidRecvLaunchFromWXReq:)]) {
LaunchFromWXReq *launchReq = (LaunchFromWXReq *)req;
[_delegate managerDidRecvLaunchFromWXReq:launchReq];
}
}
}
@end
3.在pch文件中导入微信的sdk头文件:#import "WXApi.h"//微信支付
4.在AppDelegate中加入处理回调的头文件:#import "WXApiManager.h"//微信支付,然后在AppDelegate.m中添加以下代码:
//在这个方法中注册微信的key
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//如果有友盟社会化组件(分享之类的),一定要写在微信支付key的前面
//在此先注册友盟的key,不然你会哭的
//微信支付key
[WXApi registerApp:@"你的key"];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {//微信支付的回调
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
//这个方法包括了友盟支付、微信支付、支付宝支付,看你的项目了,不需要的给删了
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{//设置系统回调(支持所有iOS系统)
//6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
if (!result) {
// 其他如支付等SDK的回调
//微信支付回调的结果,在这里可以做你想要的操作,写了一个单例来处理微信支付结果的回调,处理在WXApiManager文件中操作
BOOL WXResult = [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
if (WXResult) {
return WXResult;
}
// return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];//微信支付
//支付宝(iOS9之前的版本回调在此)
BOOL AliResult = [[BYAliPayManager sharedAliPayManager] aliPayResultOpenUrl:url];
if (AliResult) {
return AliResult;
}
}
return result;
}
5.在触发微信支付的类中
//1.注册通知,接收微信支付成功的回调
//接收第三方支付成功的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(thridPaySuccessAndDismiss:)
name:kNotificationThridPaySuccess
object:nil];
- (void)thridPaySuccessAndDismiss:(NSNotification *)notifi{//接到第三方支付结果的通知
BOOL state = [notifi.object boolValue];
if (state) {//支付成功
做前台的页面操作
}else{//支付失败
做前台的页面操作
}
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:kNotificationThridPaySuccess object:nil];
}
//2.调后台的接口,向后台要微信支付的参数字典(示例:paramDic)
if (paramDic != nil) {//调起微信支付
NSMutableString *stamp = [paramDic objectForKey:@"timestamp"];//时间戳
PayReq* req = [[PayReq alloc] init];
req.prepayId = [paramDic objectForKey:@"prepayid"];//预支付订单id
req.partnerId = [paramDic objectForKey:@"partnerid"];//商家向财付通申请的商家id
req.nonceStr = [paramDic objectForKey:@"noncestr"];//随机串,防重发(前端生成)
req.timeStamp = stamp.intValue;//时间戳,防重发
req.package = [paramDic objectForKey:@"package"];//商家根据财付通文档填写的数据和签名
req.sign = [paramDic objectForKey:@"sign"];//商家根据微信开放平台文档对数据做的签名
//调用微信支付
[WXApi sendReq:req];
}