1.微信的sdk集成步驟很爛纵朋,先把他需要的庫(kù)和文件導(dǎo)入項(xiàng)目(推薦使用pod)
2.導(dǎo)入支付回調(diào)的代碼類:先把他的示例demo中的#import "WXApiManager.h" 這個(gè)類導(dǎo)入到項(xiàng)目艾扮,然后修改這個(gè)類的.m文件(.h不動(dòng))。
#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 {//支付結(jié)果的回調(diào)
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]]){//返回支付結(jié)果,實(shí)際支付結(jié)果需要去微信服務(wù)器端查詢
//resp.errCode,resp.errStr
switch (resp.errCode) {
case WXSuccess:{
NSLog(@"支付成功-PaySuccess凡怎,retcode = %d", resp.errCode);
//發(fā)出第三方支付成功的通知(發(fā)通知都會(huì)吧,下面的發(fā)通知是我項(xiàng)目里的托酸,懶得改了演闭,自己發(fā)吧不跟,注意后面的參數(shù),YES代表支付成功米碰,NO代表失斘迅铩;微信比較惡心吕座,只能在這里發(fā)個(gè)通知給調(diào)用支付的界面)
KPostNotification(kNotificationThridPaySuccess, @YES);
}break;
default:{
NSLog(@"支付失敗虐译,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
//發(fā)出第三方支付失敗的通知(發(fā)通知都會(huì)吧,下面的發(fā)通知是我項(xiàng)目里的吴趴,懶得改了漆诽,自己發(fā)吧,注意后面的參數(shù)锣枝,YES代表支付成功厢拭,NO代表失敗惊橱;微信比較惡心蚪腐,只能在這里發(fā)個(gè)通知給調(diào)用支付的界面)
KPostNotification(kNotificationThridPaySuccess, @NO);
}break;
}
}
}
- (void)onReq:(BaseReq *)req {//onReq是微信終端向第三方程序發(fā)起請(qǐng)求,要求第三方程序響應(yīng)税朴。第三方程序響應(yīng)完后必須調(diào)用sendRsp返回回季。在調(diào)用sendRsp返回時(shí)家制,會(huì)切回到微信終端程序界面。
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文件中導(dǎo)入微信的sdk頭文件:#import "WXApi.h"http://微信支付
4.在AppDelegate中加入處理回調(diào)的頭文件:#import "WXApiManager.h"http://微信支付泡一,然后在AppDelegate.m中添加以下代碼:
//在這個(gè)方法中注冊(cè)微信的key
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//如果有友盟社會(huì)化組件(分享之類的)颤殴,一定要寫在微信支付key的前面
//在此先注冊(cè)友盟的key,不然你會(huì)哭的
//微信支付key
[WXApi registerApp:@"你的key"];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {//微信支付的回調(diào)
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
//這個(gè)方法包括了友盟支付、微信支付鼻忠、支付寶支付涵但,看你的項(xiàng)目了,不需要的給刪了
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{//設(shè)置系統(tǒng)回調(diào)(支持所有iOS系統(tǒng))
//6.3的新的API調(diào)用帖蔓,是為了兼容國(guó)外平臺(tái)(例如:新版facebookSDK,VK等)的調(diào)用[如果用6.2的api調(diào)用會(huì)沒(méi)有回調(diào)],對(duì)國(guó)內(nèi)平臺(tái)沒(méi)有影響
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
if (!result) {
// 其他如支付等SDK的回調(diào)
//微信支付回調(diào)的結(jié)果,在這里可以做你想要的操作,寫了一個(gè)單例來(lái)處理微信支付結(jié)果的回調(diào),處理在WXApiManager文件中操作
BOOL WXResult = [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
if (WXResult) {
return WXResult;
}
// return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];//微信支付
//支付寶(iOS9之前的版本回調(diào)在此)
BOOL AliResult = [[BYAliPayManager sharedAliPayManager] aliPayResultOpenUrl:url];
if (AliResult) {
return AliResult;
}
}
return result;
}
5.在觸發(fā)微信支付的類中
//1.注冊(cè)通知矮瘟,接收微信支付成功的回調(diào)
//接收第三方支付成功的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(thridPaySuccessAndDismiss:)
name:kNotificationThridPaySuccess
object:nil];
- (void)thridPaySuccessAndDismiss:(NSNotification *)notifi{//接到第三方支付結(jié)果的通知
BOOL state = [notifi.object boolValue];
if (state) {//支付成功
做前臺(tái)的頁(yè)面操作
}else{//支付失敗
做前臺(tái)的頁(yè)面操作
}
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:kNotificationThridPaySuccess object:nil];
}
//2.調(diào)后臺(tái)的接口,向后臺(tái)要微信支付的參數(shù)字典(示例:paramDic)
if (paramDic != nil) {//調(diào)起微信支付
NSMutableString *stamp = [paramDic objectForKey:@"timestamp"];//時(shí)間戳
PayReq* req = [[PayReq alloc] init];
req.prepayId = [paramDic objectForKey:@"prepayid"];//預(yù)支付訂單id
req.partnerId = [paramDic objectForKey:@"partnerid"];//商家向財(cái)付通申請(qǐng)的商家id
req.nonceStr = [paramDic objectForKey:@"noncestr"];//隨機(jī)串塑娇,防重發(fā)(前端生成)
req.timeStamp = stamp.intValue;//時(shí)間戳澈侠,防重發(fā)
req.package = [paramDic objectForKey:@"package"];//商家根據(jù)財(cái)付通文檔填寫的數(shù)據(jù)和簽名
req.sign = [paramDic objectForKey:@"sign"];//商家根據(jù)微信開(kāi)放平臺(tái)文檔對(duì)數(shù)據(jù)做的簽名
//調(diào)用微信支付
[WXApi sendReq:req];
}