zoukankan      html  css  js  c++  java
  • ios开发笔记-3-微信支付

    转载博客

    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.在监听回调中,上传我们从微信支付中获得的参数,完成支付.

    http://www.jianshu.com/p/97d38b00e53d?plg_nld=1&plg_auth=1&plg_nld=1&plg_dev=1&plg_uin=1&plg_usr=1&plg_vkey=1&plg_nld=1&plg_uin=1&plg_nld=1&plg_auth=1&plg_usr=1&plg_vkey=1&plg_dev=1

    http://www.tuicool.com/articles/32UFBzQ?plg_nld=1&plg_uin=1&plg_auth=1&plg_nld=1&plg_usr=1&plg_vkey=1&plg_dev=1

    http://www.jianshu.com/p/6e75a450dcc4?plg_nld=1&plg_uin=1&plg_auth=1&plg_nld=1&plg_usr=1&plg_vkey=1&plg_dev=1

  • 相关阅读:
    python网络爬虫——scrapy核心组件介绍、请求传参、下载中间件
    python网络爬虫——Scrapy全站数据爬取【手动请求发送】及post请求的发送
    python网络爬虫——scrapy框架持久化存储
    php常见排序
    php实现快速排序
    mysql读写分离 主从同步
    php预定义字符
    本地Navicat连不上Linux虚拟机MySQL数据库问题
    php yii 命令
    yii 定义场景
  • 原文地址:https://www.cnblogs.com/feng520/p/4962304.html
Copyright © 2011-2022 走看看