使用pod
下载微信支付SDK
platform:ios, ‘7.0‘
use_frameworks!
target ‘你的工程名’ do
pod 'WeChat_SDK', '~> 1.7.5.1'
end
在AppDelegate.m
中注册你申请的微信app_id
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1 微信pay
[WXApi registerApp:@"你的微信app_id" withDescription:@"你的项目名称"];
}
重写AppDelegate
的openURL
方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary*)options
{
//设置微信WXApi的代理是当前控制器
return [WXApi handleOpenURL:url delegate:self];
}
AppDelegate
继承代理WXApiDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>
在AppDelegate.m
中重写WXApiDelegate
的onResp
方法
- (void)onResp:(BaseResp *)resp{
NSString * strMsg = [NSString stringWithFormat:@"errorCode: %d",resp.errCode];
NSLog(@"strMsg: %@",strMsg);
NSString * errStr = [NSString stringWithFormat:@"errStr: %@",resp.errStr];
NSLog(@"errStr: %@",errStr);
NSString * strTitle;
//判断是微信消息的回调 --> 是支付回调回来的还是消息回调回来的.
if ([resp isKindOfClass:[SendMessageToWXResp class]])
{
strTitle = [NSString stringWithFormat:@"发送媒体消息的结果"];
}
NSString * wxPayResult;
//判断是否是微信支付回调
if ([resp isKindOfClass:[PayResp class]])
{
//支付返回的结果, 实际支付结果需要去微信服务器端查询
strTitle = [NSString stringWithFormat:@"支付结果"];
switch (resp.errCode)
{
case WXSuccess:
{
strMsg = @"支付结果:";
NSLog(@"支付成功: %d",resp.errCode);
wxPayResult = @"success";
break;
}
case WXErrCodeUserCancel:
{
strMsg = @"用户取消了支付";
NSLog(@"用户取消支付: %d",resp.errCode);
wxPayResult = @"cancel";
break;
}
default:
{
strMsg = [NSString stringWithFormat:@"支付失败! code: %d errorStr: %@",resp.errCode,resp.errStr];
NSLog(@":支付失败: code: %d str: %@",resp.errCode,resp.errStr);
wxPayResult = @"faile";
break;
}
}
//全局广播
NSNotification * notification = [NSNotification notificationWithName:@"WXPay" object:wxPayResult];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
}
在调起支付的控制器XXX.m
中接受广播
- (void)viewDidLoad {
[super viewDidLoad];
//是否安装微信
if ([WXApi isWXAppInstalled])
{
//监听通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXPay" object:nil];
}
}
处理广播内容
- (void)getOrderPayResult:(NSNotification *)notification
{
if ([notification.object isEqualToString:@"success"])
{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
[self paySuccessHandler];
}
else{
[self alert:@"提示" msg:@"支付失败"];
[self payFailHandler];
}
}
//客户端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alter show];
}
请求服务器对微信支付签名后的数据
-(void)wechat{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"nos"] = self.payNos; //订单号
dict[@"token"] = [self token];
NSString* urlString = @"http://api.xxxxx.com/order/pay_wx";
[manager GET:urlString parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
WXDataModel *dataModel = [WXDataModel mj_objectWithKeyValues:responseObject[@"data"]];
WXSelectedModel *selectModel = [WXSelectedModel mj_objectWithKeyValues:dataModel.selected_pay_data];
WechatModel *weChatModel = [WechatModel mj_objectWithKeyValues:selectModel.native];
//调起支付
BOOL result = [self wechatPay:weChatModel];
NSLog(@"result = %d",result?YES:NO);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
调起支付,WechatModel
是数据模型,封装了服务器sign签名后返回的数据
// 微信支付
-(BOOL)wechatPay:(WechatModel *)dict{
PayReq *req = [[PayReq alloc]init];
req.partnerId = dict.partnerid;
req.prepayId = dict.prepayid;
req.nonceStr = dict.noncestr;
req.timeStamp = dict.timestamp.intValue;
req.package = dict.package;
req.sign = dict.sign;
return [WXApi sendReq:req];
}
销毁广播
/**
销毁广播
*/
-(void)dealloc {
[[NSNotificationCenter defaultCenter]removeObserver:self];
}