按照文档demo以及很多网上教程,基本上实现了测试iD成功调起微信支付,但是支付成功以及与自己的后台交互就出现问题了,第一个问题是两个appID都需要该成自己的别忘了,并且别用控制器器管理我就莫名的出现了一个白板在支付回调的时候,太坑!
下方是与后台的交互,我就是把这个变成了控制器白板就出现了
#import "WXpay.h"
@implementation WXpay
-(void)wxpay{
NSString *weixinbackstr = [self jumpToBizPay];
if (![weixinbackstr isEqualToString:@""]) {
NSLog(@"weixinbackstr:%@",weixinbackstr);
}
WXApiManager *mager = [WXApiManager new];
mager.delegate = self;
}
-(NSString *)jumpToBizPay {
//在调起微信之前需要加上两个判断
if (![WXApi isWXAppInstalled]) {
[[NSNotificationCenter defaultCenter]postNotificationName:@"isWXAppInstalled" object:nil];
return nil;
}else
if (![WXApi isWXAppSupportApi]){
NSLog(@"微信版本太低不支持Api");
[[NSNotificationCenter defaultCenter]postNotificationName:@"isWXAppSupportApi" object:nil];
}
NSLog(@"已安装微信且支持Api");
//============================================================
// V3&V4支付流程实现
// 注意:参数配置请查看服务器端Demo
// 更新时间:2015年11月20日
//============================================================
NSMutableDictionary *dict = [[NSUserDefaults standardUserDefaults]objectForKey:@"Wxpaydict"];
NSLog(@"%@@@@@@@@@",dict);
// NSString *urlString = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
// //解析服务端返回json数据
// NSError *error;
// //加载一个NSURL对象
// NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// //将请求的url数据放到NSData对象中
// NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//// if ( response != nil) {
// NSMutableDictionary *dict = NULL;
// IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
// dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"dict:%@",dict);
if(dict != nil){
NSMutableString *retcode = [dict objectForKey:@"retcode"];
if (retcode.intValue == 0){
NSMutableString *stamp = [dict objectForKey:@"timestamp"];
//调起微信支付
PayReq* req = [[PayReq alloc] init];
req.partnerId = [dict objectForKey:@"partnerid"];
req.prepayId = [dict objectForKey:@"prepayid"];
req.nonceStr = [dict objectForKey:@"noncestr"];
req.timeStamp = stamp.intValue;
req.package = [dict objectForKey:@"package"];
req.sign = [dict objectForKey:@"sign"];
[WXApi sendReq:req];
//日志输出
NSLog(@"appid=%@ partid=%@ prepayid=%@ noncestr=%@ timestamp=%ld package=%@ sign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
return @"";
}else{
return [dict objectForKey:@"retmsg"];
}
}else{
return @"服务器返回错误,未获取到json对象";
}
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"isWXAppInstalled" object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"isWXAppSupportApi" object:nil];
}
@end
下面是支付的回调////////////////////////////////////////////////下面是支付的回调//////////////////////////////////////////////////////////////////下面是支付的回调
#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;
}
#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:[WXChooseCardResp class]]) {
if (_delegate
&& [_delegate respondsToSelector:@selector(managerDidRecvChooseCardResponse:)]) {
WXChooseCardResp *chooseCardResp = (WXChooseCardResp *)resp;
[_delegate managerDidRecvChooseCardResponse:chooseCardResp];
}
}
if([resp isKindOfClass:[PayResp class]]){
switch (resp.errCode) {
case WXSuccess:
[[NSNotificationCenter defaultCenter]postNotificationName:@"WXSuccess" object:nil];
break;
case WXErrCodeUserCancel:
[[NSNotificationCenter defaultCenter]postNotificationName:@"WXfail" object:nil];
break;
case WXErrCodeUnsupport:
[[NSNotificationCenter defaultCenter]postNotificationName:@"WXfail" object:nil];
break;
case WXErrCodeAuthDeny:
[[NSNotificationCenter defaultCenter]postNotificationName:@"WXfail" object:nil];
break;
case WXErrCodeSentFail:
[[NSNotificationCenter defaultCenter]postNotificationName:@"WXfail" object:nil];
break;
case WXErrCodeCommon:
[[NSNotificationCenter defaultCenter]postNotificationName:@"WXfail" object:nil];
break;
}
}
}
- (void)onReq:(BaseReq *)req {
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