啥也不说了,直接进主题:
首先去这个网页 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319164&lang=zh_CN 下载微信支付需要的SDK,倒入进自己的工程中。
添加依赖库:
修改两配置:
这里需要在info里面配置两个东西,一个白名单一个就是http请求网络允许
OK,开始敲代码:
在AppDelegate.h里面:
在AppDelegate.m中写下面的代码:
#import "AppDelegate.h"
#import "WXApiManager.h"
// 微信开放平台申请得到的 appid, 需要同时添加在 URL schema
NSString * const WXAppId = @"wxd930ea5d5a258f4f";
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[WXApi registerApp:WXAppId];
return YES;
}
#pragma mark - 微信支付回调
- (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
{
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
Demo整体这么多file注意对应:
下面是WXApiManager.h
#import <Foundation/Foundation.h>
#import "WXApi.h"
@interface WXApiManager : NSObject<WXApiDelegate>
+ (instancetype)sharedManager;
@end
WXApiManager.m文件:
#import "WXApiManager.h"
@implementation WXApiManager
#pragma mark - 单例
+(instancetype)sharedManager {
static dispatch_once_t onceToken;
static WXApiManager *instance;
dispatch_once(&onceToken, ^{
instance = [[WXApiManager alloc] init];
});
return instance;
}
#pragma mark - WXApiDelegate
- (void)onResp:(BaseResp *)resp
{
if([resp isKindOfClass:[PayResp class]]){
//支付返回结果,实际支付结果需要去微信服务器端查询
NSString *strMsg;
switch (resp.errCode) {
case WXSuccess:
strMsg = @"支付结果:成功!";
NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
break;
default:
strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
break;
}
}
}
@end
在ViewController.m文件中写上以下的代码就搞定了:
#import "ViewController.h"
#import "WXApi.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self bizPay];
}
//模拟
- (IBAction)wxPayClick:(id)sender {
[self easyPay];
}
//模拟支付订单
- (void)easyPay{
// NSDictionary *dict = @{@"appid":@"wx75b78ead0e64a547",@"attach":@"支付测试",@"body":@"APP支付测试",@"mch_id":@"10000100",@"nonce_str":@"1add1a30ac87aa2db72f57a2375d8fec",@"notify_url":@"http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php",@"out_trade_no":@"1415659990",@"spbill_create_ip":@"14.23.150.211",@"total_fee":@"0.1",@"trade_type":@"APP",@"sign":@"0CB01533B8C1EF103065174F50BCA001"};
// NSMutableString *stamp = [dict objectForKey:@"timestamp"];
//调起微信支付
PayReq* req = [[PayReq alloc] init];
req.partnerId = @"1900000109";
req.prepayId = @"WX1217752501201407033233368018";
req.nonceStr = @"5K8264ILTKCH16CQ2502SI8ZNMTM67VS";
req.timeStamp = 1412000000;
req.package = @"Sign=WXPay";
req.sign = @"9A0A8659F005D6984697E2CA0A9CF3B7";
[WXApi sendReq:req];
}
//真实生成订单走这个方法
- (void)bizPay {
NSString *res = [self jumpToBizPay];
if( ![@"" isEqual:res] ){
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"支付失败" message:res delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alter show];
}
}
-(NSString *)jumpToBizPay {
//============================================================
// V3&V4支付流程实现
// 注意:参数配置请查看服务器端Demo
// 更新时间:2015年11月20日
//============================================================
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(@"url:%@",urlString);
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=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[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对象";
}
}else{
return @"服务器返回错误";
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
基本上到这里,你的应用跳转微信支付是没有问题的了,我们移动端做的事情就这么多的,剩下的抛给后台弄吧,如果他不会,就说他菜,别傻傻的把支付生成订单这么牛逼的事情放在我们移动端,会出问题的。切记切记!
GitHub下载地址