本篇主要介绍接入App原生微信支付的具体步骤(签名在服务端进行生成,客户端主要就是吊起支付)
注册相关信息
1,在微信开放平台上申请app管理,获取AppId,AppServece.
2,认证开发者资质
3,申请微信支付共能
项目中集成微信sdk
-
打开工程直接将下载的SDK拖进去
-
导入SDK所依赖的库
在info,plist 中添加微信白名单和网络协议设置
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
-
将微信支付的APPID设为URL Schemes
实现代码
//1.导入微信支付SDK,注册微信支付
//2.设置微信APPID为URL Schemes
//3.发起支付,调起微信支付
//4.处理支付结果
#import "WXApi.h"
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//向微信注册
[WXApi registerApp:WXKey];
}
在微信支付按钮方法里 首先根据订单编号传给后台,后台调用微信api接口生成预支付订单,然后将预支付订单信息进行第二次签名 将相关信息传给客户端,客户端调用SDK方法完成微信支付。
微信支付点击方法如下:
//以下调用后台的方法为我们自己封装的方法具体可按照你们自己处理的逻辑
WxpayClientEntity*client = [[WxpayClientEntity alloc]init];
client.orderid = self.orderid;
[[NSUserDefaults standardUserDefaults]setObject:client.orderid forKey:@"orderid"];
client.ipaddress =[self getIPAddress];
[[Network new]httpRequestJosn:client WithRespoinseObj:^(id respoinseObj) {
NSLog(@"%@",respoinseObj);
[MBProgressHUD showText:respoinseObj[@"message"]];
if ([[respoinseObj objectForKey:@"status"]isEqualToString:@"2000123"]) { NSDictionary*dic = [respoinseObj objectForKey:@"results"];
WxpayEntity*model = [[WxpayEntity alloc]initWithDictinary:dic];
PayReq *request = [[PayReq alloc] init];
/** 商家向财付通申请的商家id */
request.partnerId =model.partnerid;
/** 预支付订单 */
request.prepayId= model.prepayid;
/** 商家根据财付通文档填写的数据和签名 */
request.package = @"Sign=WXPay";
/** 随机串,防重发 */
request.nonceStr= model.noncestr;
/** 时间戳,防重发 */
// request.timeStamp= [model.timestamp intValue];
// 将当前时间转化成时间戳
NSDate *datenow = [NSDate date];
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
UInt32 timeStamp =[timeSp intValue];
request.timeStamp= timeStamp;
// * 商家根据微信开放平台文档对数据做的签名
// request.sign=[self createMD5SingForPay:WXKey
// partnerid:request.partnerId
// prepayid:request.prepayId
// package:request.package
// noncestr:request.nonceStr
// timestamp:request.timeStamp];
// request.sign = model.sign;
request.sign = model.sign;
NSLog(@"%@",model.timestamp);
[WXApi sendReq: request];
}
} failBlock:^(NSHTTPURLResponse *responseObj) {
}];
在AppDelegate 实现如下回调方法
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [WXApi handleOpenURL:url delegate:self];
}
实现微信支付回调协议
- (void)onResp:(BaseResp *)resp
{
//支付返回结果,实际支付结果需要去微信服务器端查询
NSString *strMsg ,*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;
}
}