1.需要提供公司资质或者营业执照,个人无法申请。
2.向微信注册你的应用程序id
开发者应用登记页面 进行登记,登记并选择移动应用进行设置后,将获得AppID,可立即用于开发。但应用登记完成后还需要提交审核,只有审核通过的应用才能正式发布使用。
3.微信APP支付接入商户服务中心,下载微信SDK文件(具体参考微信官方文档)
4.添加依赖库
SystemConfiguration.framework
libz.dylib
libsqlite3.dylib
libc++.dylib
CoreTelephony.framework
CoreGraphics.framework
5.在需要调用WeChatSDK的文件中,增加头文件引用。
#import "WXApi.h"
#import "payRequsestHandler.h"
6.生成订单信息及签名
- (void)wechatPayAction:(UIButton *) sender
{
// 判断用户是否安装微信
if (![WXApi isWXAppInstalled]) {
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请安装微信客户端" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertView show];
return;
}
// 实现支付
[self sendPay_demo];
}
- (void)sendPay_demo
{
//{{{
//本实例只是演示签名过程, 请将该过程在商户服务器上实现
// 配置微信支付的参数
//创建支付签名对象
payRequsestHandler *req = [[payRequsestHandler alloc] init];
//初始化支付签名对象
[req init:__WXappID mch_id:__WXmchID];
//设置密钥
[req setKey:__WXpaySignKey];
//}}}
//获取到实际调起微信支付的参数后,在app端调起支付
NSMutableDictionary *dict = [req sendPay_demo];
if(dict == nil){
//错误提示
NSString *debug = [req getDebugifo];
[self alert:@"提示信息" msg:debug];
NSLog(@"%@\n\n",debug);
}else{
NSLog(@"%@\n\n",[req getDebugifo]);
//[self alert:@"确认" msg:@"下单成功,点击OK后调起支付!"];
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];
}
}
//客户端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alter show];
}
7.Xcode设置URL scheme
在Xcode中,选择你的工程设置项,选中“TARGETS”一栏,在“info”标签栏的“URL type“添加“URL scheme”为你所注册的应用程序id(如下图所示)。
8.在你需要使用微信终端API的文件中import WXApi.h 头文件,并增加 WXApiDelegate 协议。
// 微信所有的API接口
#import "WXApi.h"
// APP端签名相关头文件
#import "payRequsestHandler.h"
@interface AppDelegate ()<WXApiDelegate>
@end
9.要使你的程序启动后微信终端能响应你的程序,必须在代码中向微信终端注册你的id。(如下图所示,在 AppDelegate 的 didFinishLaunchingWithOptions 函数中向微信注册id)。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//向微信注册
[WXApi registerApp:APP_ID withDescription:@"demo 2.0"];
return YES;
}
重写AppDelegate的handleOpenURL和openURL方法:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [WXApi handleOpenURL:url delegate:self];
}
10.现在,你的程序要实现和微信终端交互的具体请求与回应,因此需要实现WXApiDelegate协议的两个方法:
-(void) onReq:(BaseReq*)req
{
if([req isKindOfClass:[GetMessageFromWXReq class]])
{
// 微信请求App提供内容, 需要app提供内容后使用sendRsp返回
NSString * strTitle = [NSString stringWithFormat:@"微信请求App提供内容"];
NSString * strMsg = @"微信请求App提供内容,App要调用sendResp:GetMessageFromWXResp返回给微信";
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
alert.tag = 1000;
[alert show];
}
else if([req isKindOfClass:[ShowMessageFromWXReq class]])
{
ShowMessageFromWXReq * temp = (ShowMessageFromWXReq*)req;
WXMediaMessage * msg = temp.message;
//显示微信传过来的内容
WXAppExtendObject * obj = msg.mediaObject;
NSString * strTitle = [NSString stringWithFormat:@"微信请求App显示内容"];
NSString * strMsg = [NSString stringWithFormat:@"标题:%@ \n内容:%@ \n附带信息:%@ \n缩略图:%lu bytes\n\n", msg.title, msg.description, obj.extInfo, msg.thumbData.length];
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
else if([req isKindOfClass:[LaunchFromWXReq class]])
{
//从微信启动App
NSString * strTitle = [NSString stringWithFormat:@"从微信启动"];
NSString * strMsg = @"这是从微信启动的消息";
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
onReq是微信终端向第三方程序发起请求,要求第三方程序响应。第三方程序响应完后必须调用sendRsp返回。在调用sendRsp返回时,会切回到微信终端程序界面。
-(void) onResp:(BaseResp*)resp
{
NSString * strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
NSString * strTitle;
if([resp isKindOfClass:[SendMessageToWXResp class]])
{
strTitle = [NSString stringWithFormat:@"发送媒体消息结果"];
}
if([resp isKindOfClass:[PayResp class]]){
//支付返回结果,实际支付结果需要去微信服务器端查询
strTitle = [NSString stringWithFormat:@"支付结果"];
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;
}
}
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
如果第三方程序向微信发送了sendReq的请求,那么onResp会被回调。sendReq请求调用后,会切到微信终端程序界面。