1、去微信开发平台 下载 sdk :https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_5
2、将SDK文件中包含的 libWeChatSDK.a,WXApi.h,WXApiObject.h 三个文件添加到你所建的工程中
3、导入对应的库文件:
SystemConfiguration.framework,libz.dylib,libsqlite3.0.dylib,libc++.dylib。
4、appdelegate里面:
@interface AppDelegate : UIResponder <WXApiDelegate>
{
enum WXScene _scene;
}//微信支付
#import "WXApi.h"
#import "WXApiObject.h"
注册APPID
商户APP工程中引入微信lib库和头文件,调用API前,需要先向微信注册您的APPID,代码如下:
[WXApi registerApp:@"wxd930ea5d5a258f4f" withDescription:@"demo 2.0"];
#pragma mark - //向微信注册
[WXApi registerApp:APP_ID withDescription:@"demo 2.0"];
#pragma mark - 微信支付 - ------
-(void) changeScene:(NSInteger )scene
{
_scene = (enum WXScene)scene;
}
-(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:@"支付结果:失败!"];
// 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];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [WXApi handleOpenURL:url delegate:self];
}
5、
调起支付接口:
if (![WXApi isWXAppInstalled]) {
[PTHudView showOnlyText:@"请先安装微信!" dismiss:YES];
return;
}
NSMutableDictionary * params = [NSMutableDictionary dictionary];
params[@"SIMILAR_ID"] = @"15c0992d0db44bdd9170e02a98aaf9c5";
[HWHttpTool post:[NSString stringWithFormat:@"http://12xxxxxxxapp/pay/wxOrder"] params:params success:^(id json) {
NSString * stamp = [json objectForKey:@"timestamp"];
//调起微信支付
PayReq* req = [[PayReq alloc] init];
req.openID = [json objectForKey:@"appid"];
req.partnerId = [json objectForKey:@"partnerid"];
req.prepayId = [json objectForKey:@"prepayid"];
req.nonceStr = [json objectForKey:@"noncestr"];
req.timeStamp = stamp.intValue;
req.package = [json objectForKey:@"package"];
req.sign = [json objectForKey:@"sign"];
[WXApi sendReq:req];
} failure:^(NSError *error) {
}];
if(self.weixindic ==nil || ![[self.weixindicobjectForKey:@"status"]isEqualToString:@"success"]){
//错误提示
//NSString *debug = [req getDebugifo];
NSString *debug =[self.weixindicobjectForKey:@"msg"];
// [self alert:@"提示信息" msg:debug];
NSLog(@" debug=== %@\n\n",debug);
}else{
//NSLog(@"%@\n\n",[req getDebugifo]);
//[self alert:@"确认" msg:@"下单成功,点击OK后调起支付!"];
NSMutableString *stamp = [self.weixindicobjectForKey:@"timestamp"];
//调起微信支付
PayReq* req = [[PayReqalloc]init];
req.openID = [self.weixindicobjectForKey:@"appid"];
req.partnerId = [self.weixindicobjectForKey:@"partnerid"];
req.prepayId = [self.weixindicobjectForKey:@"prepayid"];
req.nonceStr = [self.weixindicobjectForKey:@"noncestr"];
req.timeStamp = stamp.intValue;
req.package = [self.weixindicobjectForKey:@"package"];
req.sign = [self.weixindicobjectForKey:@"sign"];
[WXApisendReq:req];
}
//客户端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
UIAlertView *alter = [[UIAlertViewalloc]initWithTitle:titlemessage:msgdelegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil];
[alter show];
}
可参考:http://blog.csdn.net/joonchen111/article/details/48287877
服务端参考:http://www.cnblogs.com/xyt-0412/p/4953748.html