iOS开发---微信支付
2015-10-21 本文已影响3839人
PPAbner
在最前面,还是先感谢开发过程中一下两个帖子对自己的帮助,分享!!!
为了公司的安全,这个版本的微信信息我没配置,不过,刚在网上找到个可以使用的,还是分享下,如下:
1.向微信注册你的应用程序信息
我们的App由我们的项目经理搞定,具体我也不清楚
官方接入指南如下:
请到 开发者应用登记页面 进行登记,登记并选择移动应用进行设置后,将获得AppID,可立即用于开发。但应用登记完成后还需要提交审核,只有审核通过的应用才能正式发布使用。
2.下载微信终端SDK文件,项目中导入lib
,SDKExport
两个文件
下载iOS第二个
2.1导进去SDK后需要完善一些配置,要不会报错
-
导入必要的库
导入必要类库 -
ARC和MRC配置,在后面添加-fno-objc-arc
ARC和MRC配置 -
bitCode和HTTPS配置,iOS9后必须配置的
bitCode和HTTPS配置指导
3.上代码,胜过千言万语
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#import "WXApi.h"
@interface AppDelegate ()<WXApiDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
ViewController * vc = [[ViewController alloc]init];
UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
//向微信注册
[WXApi registerApp:@"。。。。。。"];
return YES;
}
#pragma mark 微信支付相关方法
-(void)onResp:(BaseResp *)resp{
//如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调。sendReq请求调用后,会切到微信终端程序界面
NSString *strMsg=[NSString stringWithFormat:@"errcode:%d",resp.errCode];
NSString *strTitle;
if ([resp isKindOfClass:[PayResp class]]) {
//支付返回结果,实际支付结果需要去微信服务器端查询
strTitle=[NSString stringWithFormat:@"支付结果"];
switch (resp.errCode) {
case WXSuccess:
strMsg=@"支付结果:成功!";
break;
default:
strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
break;
}
}
//支付成功后回调
// http://210.22.129.138:8080/link/public/notifywechatcallback
//下边先注释掉,以后会用得上
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
// [alert show];
}
-(void)onReq:(BaseReq *)req{
//onReq是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面
}
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic,assign) int totalPrice;
@property (nonatomic,strong) NSString * orderName;
@end
ViewController.m
#import "ViewController.h"
#import "payRequsestHandler.h"
#import "WXApi.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton * bt = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
bt.backgroundColor = [UIColor redColor];
[bt addTarget:self action:@selector(ButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bt];
self.totalPrice = 1;
self.orderName = @"请叫我雷锋,哈哈";
}
-(void)ButtonClicked{
[self pay];
}
- (void)pay
{
payRequsestHandler *req = [payRequsestHandler alloc];
//初始化支付签名对象
[req init:APP_ID mch_id:MCH_ID];
//设置密钥
[req setKey:PARTNER_ID];
//获取到实际调起微信支付的参数后,在app端调起支付
int lastPrice=self.totalPrice*100;
NSMutableDictionary *dict = [req sendPay_demo:self.orderName price:[NSString stringWithFormat:@"%d",lastPrice]];
if(dict != nil){
NSMutableString *retcode = [dict objectForKey:@"retcode"];
if (retcode.intValue == 0){
NSMutableString *stamp = [dict objectForKey:@"timestamp"];
//调起微信支付
PayReq* req = [[PayReq alloc] init];
req.openID = [dict objectForKey:@"appid"];
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=%@",req.openID,req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
}else{
NSLog(@"提示信息:=%@",[dict objectForKey:@"retmsg"]);
// [self alert:@"提示信息" msg:[dict objectForKey:@"retmsg"]];
}
}else{
NSLog(@"提示信息:-%@",@"服务器返回错误,未获取到json对象");
// [self alert:@"提示信息" msg:@"服务器返回错误,未获取到json对象"];
// }
// }else{
// [self alert:@"提示信息" msg:@"服务器返回错误"];
}
}
payRequsestHandler.h
配置相关信息
// 账号帐户资料
//更改商户把相关参数后可测试
#define APP_ID @"。。。。。。" //APPID
#define APP_SECRET @"。。。。。。" //appsecret
//商户号,填写商户对应参数
#define MCH_ID @"。。。。。。"
//商户API密钥,填写相应参数
#define PARTNER_ID @"。。。。。。"
//支付结果回调页面
#define NOTIFY_URL @"http://wxpay.weixin.qq.com/pub_v2/pay/notify.v2.php"
//获取服务器端支付数据地址(商户自定义)
#define SP_URL @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php"
修改下面的方法,后面有参数的一种是项目需要的
//签名实例测试(demo中的)
- ( NSMutableDictionary *)sendPay_demo;
//签名实例(实际需要的)
- ( NSMutableDictionary *)sendPay_demo:(NSString *)orderName price:(NSString *)price;
payRequsestHandler.m
- ( NSMutableDictionary *)sendPay_demo:(NSString *)orderName price:(NSString *)price{
//订单标题,展示给用户
NSString *order_name = orderName;
//订单金额,单位(分)
NSString *order_price = price;//1分钱测试
//================================
//预付单参数订单设置
//================================
srand( (unsigned)time(0) );
NSString *noncestr = [NSString stringWithFormat:@"%d", rand()];
NSString *orderno = [NSString stringWithFormat:@"%ld",time(0)];
NSLog(@"%@",orderno);
NSMutableDictionary *packageParams = [NSMutableDictionary dictionary];
[packageParams setObject: appid forKey:@"appid"]; //开放平台appid
[packageParams setObject: mchid forKey:@"mch_id"]; //商户号
[packageParams setObject: @"APP-001" forKey:@"device_info"]; //支付设备号或门店号
[packageParams setObject: noncestr forKey:@"nonce_str"]; //随机串
[packageParams setObject: @"APP" forKey:@"trade_type"]; //支付类型,固定为APP
[packageParams setObject: order_name forKey:@"body"]; //订单描述,展示给用户
[packageParams setObject: NOTIFY_URL forKey:@"notify_url"]; //支付结果异步通知
[packageParams setObject: orderno forKey:@"out_trade_no"];//商户订单号
[packageParams setObject: @"196.168.1.1" forKey:@"spbill_create_ip"];//发器支付的机器ip
[packageParams setObject: order_price forKey:@"total_fee"]; //订单金额,单位为分
//获取prepayId(预支付交易会话标识)
NSString *prePayid;
prePayid = [self sendPrepay:packageParams];
if ( prePayid != nil) {
//获取到prepayid后进行第二次签名
NSString *package, *time_stamp, *nonce_str;
//设置支付参数
time_t now;
time(&now);
time_stamp = [NSString stringWithFormat:@"%ld", now];
nonce_str = [WXUtil md5:time_stamp];
//重新按提交格式组包,微信客户端暂只支持package=Sign=WXPay格式,须考虑升级后支持携带package具体参数的情况
//package = [NSString stringWithFormat:@"Sign=%@",package];
package = @"Sign=WXPay";
//第二次签名参数列表
NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
[signParams setObject: appid forKey:@"appid"];
[signParams setObject: nonce_str forKey:@"noncestr"];
[signParams setObject: package forKey:@"package"];
[signParams setObject: mchid forKey:@"partnerid"];
[signParams setObject: time_stamp forKey:@"timestamp"];
[signParams setObject: prePayid forKey:@"prepayid"];
//[signParams setObject: @"MD5" forKey:@"signType"];
//生成签名
NSString *sign = [self createMd5Sign:signParams];
//添加签名
[signParams setObject: sign forKey:@"sign"];
[debugInfo appendFormat:@"第二步签名成功,sign=%@\n",sign];
//返回参数列表
return signParams;
}else{
[debugInfo appendFormat:@"获取prepayid失败!\n"];
}
return nil;
}