IOS-微信支付

今天博主有一个微信支付的需求,遇到了一些困难点,在此和大家分享,希望能够共同进步.

1.申请AppID,导入依赖库,配置URL Scheme这里就不深究了,各位看官请自行百度.

2.在AppDelegate中导入头文件,遵守WXApiDelegate,然后在launch里注册AppID.

3.在支付界面导入头文件,创建支付对象,调起微信

//调起微信支付,传入参数

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

req.openID = APPI_ID;

req.partnerId = PARTNER_ID;

req.prepayId = prePayid;

req.nonceStr = nonce_str;

req.timeStamp = now;

req.package = package;

req.sign = sign;

[WXApi safeSendReq:req];

4.微信支付的回调需要添加在AppDelegate中

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

 

     //返回应用后的回调,通过handleOpenURL进入微信支付的回调onResp:(BaseResp*)resp

         return [WXApi handleOpenURL:url delegate:self];

}

-(void) onResp:(BaseResp*)resp

{

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

{

PayResp *response = (PayResp *)resp;

NSString *strTitle = [NSString stringWithFormat:@"支付结果"];

NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", response.errCode];

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitlemessage:strMsg  delegate:self cancelButtonTitle:@"OK"  otherButtonTitles:nil, nil];

[alert show];

switch (response.errCode) {

case WXSuccess: {

NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"success"];

[[NSNotificationCenter defaultCenter] postNotification:notification];

break;

}

default: {

NSNotification *notification = [NSNotification notificationWithName:ORDER_PAY_NOTIFICATION object:@"fail"];

[[NSNotificationCenter defaultCenter] postNotification:notification];

break;

}

}

}

}

但是将微信支付相关的代码全部写到AppDelegate中又会显得代码冗余,这时我们需要创建通知,在成功回调后发出通知,在支付界面监听回调,完成支付.

5.在监听回调中,上传我们从微信支付中获得的参数,完成支付.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值