1.微信后台文档创建AppKey
创建单例类WechatManager
#import <Foundation/Foundation.h>
#import "WXApi.h"
@interface WechatManager : NSObject
+ (id)shareInstance;
+ (BOOL)handleOpenUrl:(NSURL *)url;
+ (void)hangleWechatPayWith:(PayReq *)req;
@end
#import "WechatManager.h"
@interface WechatManager()<WXApiDelegate>
@end
@implementation WechatManager
+ (id)shareInstance {
static WechatManager *weChatPayInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
weChatPayInstance = [[WechatManager alloc] init];
});
return weChatPayInstance;
}
+ (BOOL)handleOpenUrl:(NSURL *)url {
return [WXApi handleOpenURL:url delegate:[WechatManager shareInstance]];
}
+ (void)hangleWechatPayWith:(PayReq *)req {
[WXApi sendReq:req completion:^(BOOL success) {
if (success) {
NSLog(@"微信支付成功");
} else {
NSLog(@"微信支付异常");
}
}];
}
#pragma mark - 微信支付回调
- (void)onResp:(BaseResp *)resp {
if ([resp isKindOfClass:[PayResp class]]) {
/*
enum WXErrCode {
WXSuccess = 0, < 成功
WXErrCodeCommon = -1, < 普通错误类型
WXErrCodeUserCancel = -2, < 用户点击取消并返回
WXErrCodeSentFail = -3, < 发送失败
WXErrCodeAuthDeny = -4, < 授权失败
WXErrCodeUnsupport = -5, < 微信不支持
};
*/
PayResp *response = (PayResp*)resp;
switch (response.errCode) {
case WXSuccess: {
NSLog(@"微信回调支付成功");
// [[NSNotificationCenter defaultCenter] postNotificationName:UserNotificationAlipayOrWechatSuccess
// object:nil
// userInfo:nil];
break;
}
case WXErrCodeCommon: {
NSLog(@"微信回调支付异常");
break;
}
case WXErrCodeUserCancel: {
NSLog(@"微信回调用户取消支付");
break;
}
case WXErrCodeSentFail: {
NSLog(@"微信回调发送支付信息失败");
break;
}
case WXErrCodeAuthDeny: {
NSLog(@"微信回调授权失败");
break;
}
case WXErrCodeUnsupport: {
NSLog(@"微信回调微信版本暂不支持");
break;
}
default: {
break;
}
}
}
}
@end
if ([_getPayID isEqualToString:@"21"]) {
NSString * orderString = responseObject[@"data"];
[[AlipaySDK defaultService] payOrder:orderString fromScheme:@"ADSDK" callback:^(NSDictionary *resultDic) {
NSLog(@"reslut ======= %@",resultDic);
}];
}
if ([_getPayID isEqualToString:@"20"]) {
//调起微信支付
PayReq *req = [[PayReq alloc] init];
req.nonceStr = [responseObject[@"data"] objectForKey:@"noncestr"];
req.timeStamp = [[responseObject[@"data"] objectForKey:@"timestamp"] intValue];
req.package = [responseObject[@"data"] objectForKey:@"package"];
req.partnerId = [responseObject[@"data"] objectForKey:@"partnerid"];
req.prepayId = [responseObject[@"data"] objectForKey:@"prepayid"];
req.sign = [responseObject[@"data"] objectForKey:@"sign"];
[WechatManager hangleWechatPayWith:req];
}
网友评论