微信支付

2016-09-23  本文已影响78人  小柴2011

1,参考微信支付APP开发者文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5

(1)下载微信支付sdk,拖入工程,添加framework

(2)项目设置APPID

商户在微信开放平台申请开发APP应用后,微信开放平台会生成APP的唯一标识APPID。在Xcode中打开项目,设置项目属性中的URL Schemes为您的APPID。如图8.7标红位置所示。

(3)注册APPID

商户APP工程中引入微信lib库和头文件,调用API前,需要先向微信注册您的APPID,代码如下:

[WXApi registerApp:@"wxd930ea5d5a258f4f" withDescription:@"demo 2.0"];

(4)商户服务器生成支付订单,先调用【统一下单API】生成预付单,获取到prepay_id后将参数再次签名传输给APP发起支付。以下是调起微信支付的关键代码:

PayReq *request = [[[PayReq alloc] init] autorelease];

request.partnerId = @"10000100";

request.prepayId= @"1101000000140415649af9fc314aa427";

request.package = @"Sign=WXPay";

request.nonceStr= @"a462b76e7436e98e0ed6e13c64b4fd1c";

request.timeStamp= @"1397527777";

request.sign= @"582282D72DD2B03AD892830965F428CB16E7A256";

[WXApi sendReq:request];

(5)支付结果回调,在AppDelegate.m

-(void) onResp:(BaseResp*)resp

{

//这里判断回调信息是否为 支付

if([resp isKindOfClass:[PayResp class]]){

switch (resp.errCode) {

case WXSuccess:

//如果支付成功的话,全局发送一个通知,支付成功

[[NSNotificationCenter defaultCenter] postNotificationName:@"weixin_pay_result" object:[NSString stringWithFormat:@"%d" , resp.errCode]];

NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);

break;

default:

//如果支付失败的话,全局发送一个通知,支付失败

[[NSNotificationCenter defaultCenter] postNotificationName:@"weixin_pay_result" object:[NSString stringWithFormat:@"%d" , resp.errCode]];

NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);

break;

}

}

}

(6)发现支付成功但是未收到微信返回的支付结果数据,检查添加如下方法:

-(BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary*)options

{

return  [WXApi handleOpenURL:url delegate:self];

}

成功!!!!

可根据支付结果进行判断,做页面跳转,相应支付结果页面。在AppDelegate.m无法进行控制器跳转,方法:

在调起微信支付的控制器触发方法里添加监听:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weChatPayResultNoti:) name:@"weixin_pay_result" object:nil];

以及监听事件

//监听事件

-(void)weChatPayResultNoti:(NSNotification *)noti{

NSLog(@"%@",noti);

if ([[noti object] isEqualToString:@"0"]) {

NSLog(@"微信支付成功啦啦啦阿拉");

//再此进行页面跳转;

}else{

NSLog(@"微信支付失败啦啦啦阿拉");

}

//上边添加了监听,这里记得移除

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"wxpayresult" object:nil];

}

在方法-(void) onResp:(BaseResp*)resp中发送全局通知。

上一篇下一篇

猜你喜欢

热点阅读