zoukankan      html  css  js  c++  java
  • iOS-不用微信SDK唤起微信支付

    作者:TianBai

    原文链接:http://www.jianshu.com/p/8930b4496023

    要想知道微信SDK是如何调起微信客户端,那么咱们先看看微信SDK到底做了什么

    前期准备

    1. 接入前期准备工作包括商户签约和密钥配置,已完成商户可略过。

    2. 下载微信SDK  微信SDK下载地址

    SDK集成流程

    将解压的微信OpenSDK文件夹拷贝到项目文件夹下,并导入开发环境中。

    libWeChatSDK.a

    WechatAuthSDK.h

    WXApi.h

    WXApiObject.h

    导入系统依赖库

     

    在link Binary With Libraries 里面添加

    libc++.tbd

    CoreTelephony.framework

    libsqlite3.0.tbd

    libz.tbd

    SystemConfiguration.framework

    还有之前导入的  libWeChatSDK.a

    到这一步,SDK集成已经完毕

    配置代码

    • 添加URL Schemes

    点击项目名称,点击“Info”选项卡,在“URL Types”选项中,点击“+”, 在“URL Schemes”中输入微信的注册码

    • 在支付的类里引用头文件:

    #import "WXApi.h"

    • 配置wxpay支付

    #pragma mark   ==============配置wxpay支付==============

    - (void)payAction{

    //发起网络请求,去你们服务器请求1.订单id,2. 钱(单位:分),3. 内容描述

    //订单最好服务器生成,本文为了各位看的明白,所以在本地生成!

    [NetWorkTool dingiD:[self generateTradeNO] andDetail:@"描述" success:^(NSDictionary *responseObject) {

    //服务器返回数据

    //调起微信支付

    PayReq* wxreq             = [[PayReq alloc] init];

    /** appid */

    wxreq.openID              = responseObject[@"result"][@"appid"];

    /** 商家向财付通申请的商家id */

    wxreq.partnerId           = responseObject[@"result"][@"partnerid"];

    /** 预支付订单 */

    wxreq.prepayId            = responseObject[@"result"][@"prepayid"];

    /** 随机串,防重发 */

    wxreq.nonceStr            = responseObject[@"result"][@"noncestr"];

    /** 时间戳,防重发 */

    wxreq.timeStamp           = [responseObject[@"result"][@"timestamp"] intValue];

    /** 商家根据财付通文档填写的数据和签名 */

    wxreq.package             = responseObject[@"result"][@"package"];

    /** 商家根据微信开放平台文档对数据做的签名 */

    wxreq.sign                = responseObject[@"result"][@"sign"];

    [WXApi sendReq:wxreq];

    } failure:^(NSError *error) {

    NSLog(@"%@",error);

    }];

    }

    #pragma mark   ==============产生随机订单号==============

    - (NSString *)generateTradeNO

    {

    static int kNumber = 15;

    NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    NSMutableString *resultStr = [[NSMutableString alloc] init];

    srand((unsigned)time(0));

    for (int i = 0; i < kNumber; i++)

    {

    unsigned index = rand() % [sourceStr length];

    NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];

    [resultStr appendString:oneStr];

    }

    return resultStr;

    }

    配置返回处理代码

    在 AppDelegate.h 文件中,增加微信协议:

    #import "WXApi.h"

    @interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>

    在 AppDelegate.m 文件中,增加引用代码:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 初始化微信sdk

    [WXApi registerApp:@"wx*********"];

    return YES;

    }

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

    /*! @brief 处理微信通过URL启动App时传递的数据

    *

    * 需要在 application:openURL:sourceApplication:annotation:或者application:handleOpenURL中调用。

    * @param url 微信启动第三方应用时传递过来的URL

    * @param delegate  WXApiDelegate对象,用来接收微信触发的消息。

    * @return 成功返回YES,失败返回NO。

    */

    return [WXApi handleOpenURL:url delegate:self];

    }

    - (void)onResp:(BaseResp *)resp

    {

    //支付返回结果,实际支付结果需要去微信服务器端查询

    NSString *strMsg = [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;

    }

    }

    不用微信SDK 唤起微信支付

    1. 众所周知,iOS是一个封闭的系统,应用之间是不可以互相读取文件的,苹果就使用了URL Scheme来实现了这个功能。通过各个APP设计的符合苹果的统一规范的URL Scheme,Url Scheme 是可以用来传递信息的

    URL Scheme是为方便app之间互相调用而设计的。你可以通过一个类似URL的链接,通过系统的OpenURl来打开该app,并可以传递一些参数。每个URL必须能唯一标识一个APP,如果你设置的URL与别的APP的URL冲突,此时,你的APP不一定会被调用起来,原因是当APP在安装的时候就已经在系统里面注册了此APP的URL Scheme,如果你的一致但是是后安装的,那么系统不会调用你的APP,因为你的APP设置的URL scheme被覆盖了。

    2. 分析得出,微信,支付宝等sdk 的分享,支付功能,都是通过URL scheme 进行传递内容的

    3. 那么我们可以查看微信SDK唤起微信客服端支付的时候,传递的URL Scheme 是什么内容,如果可以找到其编码规律,那么即可以不用sdk进行支付

    那么,发起支付的过程中,系统会唤起微信客户端,我们思路是查看微信sdk发送给微信客户端的URL Scheme内容,就要伪造一个微信,也就是向系统声明一个和微信URL Scheme相同的地址

    查看微信URL Scheme地址

    经测试: 微信的URL Scheme是:weixin://

    那么,我们新建个工程,起名为:GetPayURLScheme

    接着注册自定义 URL Scheme

    点击 项目里info.plist (非test里面的info.plist)并选择 右键 Open As – Source Code,加入:

    <key>CFBundleURLTypes</key>

    <array>

    <dict>

    <key>CFBundleURLSchemes</key>

    <array>

    <string>weixin</string>

    </array>

    <key>CFBundleURLName</key>

    <string>1111</string>

    </dict>

    </array>

    这时候,点击 项目里info.plist (非test里面的info.plist)并选择 右键 Open As – list:

    这样就生成了如下图的URLscheme:

    在AppDelegate.m里面添加

    //应用app接收urlScheme传值时会响应此方法

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url

    sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{

    //显示截取的urlscheme

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"接收到的urlScheme" message:url.absoluteString delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

    [alert show];

    复制到剪贴板

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

    pasteboard.string = url.absoluteString;

    return YES;

    }

    运行到手机里面,这时候,你无论那个app要微信付款的时候,唤起的是刚才新建的名叫GetPayURLScheme工程 ,如下图:

    可以看到:那个urlScheme:

    weixin://app/wxdf261c3b90ffbc25/pay/?nonceStr=Ho7nAFOALQpVqSM7&package=Sign%3DWXPay&partnerId=1236537302&prepayId=wx201606052201506009de63980169148758&timeStamp=1465135310&sign=5A3EF234382FD61D36CEC104723387ED&signType=SHA1

    可以看出:它的拼接方法是:

    NSString *str = [NSString stringWithFormat:@"weixin://app/%@/pay/?nonceStr=%@&package=Sign%%3DWXPay&partnerId=%@&prepayId=%@&timeStamp=%@&sign=%@&signType=SHA1",appid,noncestr,partnerid,prepayid,[NSString stringWithFormat:@"%d",[timestamp intValue]],sign];

    不用SDK,只需要配置这么一段代码,微信支付即可完成!!!!!!!!

    - (void)payAction{

    //发起网络请求,去你们服务器请求1.订单id,2. 钱(单位:分),3. 内容描述

    //订单最好服务器生成,本文为了各位看的明白,所以在本地生成!

    [NetWorkTool dingiD:[self generateTradeNO] andDetail:@"描述" success:^(NSDictionary *responseObject) {

    /** appid */

    NSString *appid           = responseObject[@"result"][@"appid"];

    /** 商家向财付通申请的商家id */

    NSString *partnerId       = responseObject[@"result"][@"partnerid"];

    /** 预支付订单 */

    NSString *prepayId        = responseObject[@"result"][@"prepayid"];

    /** 随机串,防重发 */

    NSString *nonceStr        = responseObject[@"result"][@"noncestr"];

    /** 时间戳,防重发 */

    NSString *timeStamp       = responseObject[@"result"][@"timestamp"];

    /** 商家根据财付通文档填写的数据和签名 */

    NSString *package         = responseObject[@"result"][@"package"];

    /** 商家根据微信开放平台文档对数据做的签名 */

    NSString *sign            = responseObject[@"result"][@"sign"];

    //生成URLscheme

    NSString *str = [NSString stringWithFormat:@"weixin://app/%@/pay/?nonceStr=%@&package=Sign%%3DWXPay&partnerId=%@&prepayId=%@&timeStamp=%@&sign=%@&signType=SHA1",appid,nonceStr,partnerId,prepayId,[NSString stringWithFormat:@"%d",[timeStamp intValue] ],sign];

    //通过openURL的方法唤起支付界面

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

    } failure:^(NSError *error) {

    NSLog(@"%@",error);

    }];

    }

    注意:

    1. 订单最好服务器生成

    2. 之后必须去服务器验证支付状态!!!

    大功告成!

    Demo 下载

    • GetPayURLScheme

    下载地址:点此下载

    • WXPay

    下载地址:点此下载

     

  • 相关阅读:
    eclipse 10个常用 快捷键
    struts2 s:set标签
    web项目的路径问题
    linux系统中用户切换
    java的多线程(一)
    Mongodb 与 Mongoose 的使用
    escape()、encodeURI()、encodeURIComponent()区别详解
    JSON WEB TOKEN
    关于使用REST API
    mac下设置mongodb开机启动方法
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5851908.html
Copyright © 2011-2022 走看看