iOS第三方支付-微信支付

原创 2015年11月20日 20:35:57

目前项目里有微信支付的需求,调研过一段时间后,发现其实并没有想象中的那么困难。如果你只是想实现该功能,一个方法足以,但是若你想深入了解实现原理。就需要花费更多的功夫了。目前我只清楚微信支付需要做签名,一种是在后台签名,一种是自己在前端签名。其实这对前端来说代码量并没有多大的改变,最大的区别是在后台签名逻辑更容易理解,而在前端签名逻辑有些混乱。

     现在,小编也只是知道怎么使用,而不得其精髓,现在我附上我的学习链接,为了避免以后链接出现不可用情况,请各位理解小编的粘贴复制。

    

   博客园(幻想无极) http://www.cnblogs.com/hxwj/p/4563351.html

  微信支付开发文档     http://mch.weixin.qq.com/wiki/doc/api/app.php?chapter=8_1   

   

iOS第三方支付-微信支付

 

 

 

微信支付用到的文件

1.首先支持非arc 

 

 2.设置URL types

 

3.AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [WXApi registerApp:@"wxd930ea5d5a258f4f" withDescription:@"demo 2.0"];
    return YES;
}

 

 4.微信回调

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

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    
    return [WXApi handleOpenURL:url delegate:self];
}

//微信回调
- (void)onResp:(BaseResp *)resp {
    //errCode
    switch (resp.errCode) {
        case WXSuccess:
            //成功回调
            break;
        default:
            break;
    }
}

 

 5.调用微信支付(主要代码,后台签名)

 
 //调起微信支付
            PayReq* req = [[PayReq alloc] init];
            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];

 

 6.如果要在程序签名

 

​
//
//  ViewController.m
//  WxPay
//
//  Created by apple on 15/6/9.
//  Copyright (c) 2015年 tqh. All rights reserved.
//

#import "ViewController.h"
#import "payRequsestHandler.h"
#import "WXApi.h"

@interface ViewController () {
    enum WXScene _scene;
    NSString *Token;
    long token_time;
}

@end

@implementation ViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        _scene = WXSceneSession;
    }
    token_time = 0;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //向微信注册
    
    
    //微信支付
    UIButton *but2 = [[UIButton alloc]initWithFrame:CGRectMake(200, 100, 100, 40)];
    [but2 setTitle:@"微信支付" forState:UIControlStateNormal];
    but2.backgroundColor = [UIColor lightGrayColor];
    [but2 addTarget:self action:@selector(wxpay) forControlEvents:UIControlEventTouchUpInside];
    [but2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:but2];
    
}

//微信支付
- (void)wxpay
{
    //商户号
    NSString *PARTNER_ID    = @"1900000109";
    //商户密钥
    NSString *PARTNER_KEY   = @"8934e7d15453e97507ef794cf7b0519d";
    //APPID
    NSString *APPI_ID       = @"wxd930ea5d5a258f4f";
    //appsecret
    NSString *APP_SECRET    = @"db426a9829e4b49a0dcac7b4162da6b6";
    //支付密钥
    NSString *APP_KEY       = @"L8LrMqqeGRxST5reouB0K66CaYAWpqhAVsq7ggKkxHCOastWksvuX1uvmvQclxaHoYd3ElNBrNO2DHnnzgfVG9Qs473M3DTOZug5er46FhuGofumV8H2FVR9qkjSlC5K";
    
    //支付结果回调页面
    NSString *NOTIFY_URL    = @"http://localhost/pay/wx/notify_url.asp";
    //订单标题
    NSString *ORDER_NAME    = @"Ios客户端签名支付 测试";
    //订单金额,单位(分)
    NSString *ORDER_PRICE   = @"1";
    
    //创建支付签名对象
    payRequsestHandler *req = [payRequsestHandler alloc];
    //初始化支付签名对象
    [req init:APPI_ID app_secret:APP_SECRET partner_key:PARTNER_KEY app_key:APP_KEY];
    
    //判断Token过期时间,10分钟内不重复获取,测试帐号多个使用,可能造成其他地方获取后不能用,需要即时获取
    time_t  now;
    time(&now);
    //if ( (now - token_time) > 0 )//非测试帐号调试请启用该条件判断
    {
        //获取Token
        Token                   = [req GetToken];
        //设置Token有效期为10分钟
        token_time              = now + 600;
        //日志输出
        NSLog(@"获取Token: %@\n",[req getDebugifo]);
    }
    if ( Token != nil){
        //================================
        //预付单参数订单设置
        //================================
        NSMutableDictionary *packageParams = [NSMutableDictionary dictionary];
        [packageParams setObject: @"WX"                                             forKey:@"bank_type"];
        [packageParams setObject: ORDER_NAME                                        forKey:@"body"];
        [packageParams setObject: @"1"                                              forKey:@"fee_type"];
        [packageParams setObject: @"UTF-8"                                          forKey:@"input_charset"];
        [packageParams setObject: NOTIFY_URL                                        forKey:@"notify_url"];
        [packageParams setObject: [NSString stringWithFormat:@"%ld",time(0)]        forKey:@"out_trade_no"];
        [packageParams setObject: PARTNER_ID                                        forKey:@"partner"];
        [packageParams setObject: @"196.168.1.1"                                    forKey:@"spbill_create_ip"];
        [packageParams setObject: ORDER_PRICE                                       forKey:@"total_fee"];
        
        NSString    *package, *time_stamp, *nonce_str, *traceid;
        //获取package包
        package        = [req genPackage:packageParams];
        
        //输出debug info
        NSString *debug     = [req getDebugifo];
        NSLog(@"gen package: %@\n",package);
        NSLog(@"生成package: %@\n",debug);
        
        //设置支付参数
        time_stamp  = [NSString stringWithFormat:@"%ld", now];
        nonce_str    = [TenpayUtil md5:time_stamp];
        traceid        = @"mytestid_001";
        NSMutableDictionary *prePayParams = [NSMutableDictionary dictionary];
        [prePayParams setObject: APPI_ID                                            forKey:@"appid"];
        [prePayParams setObject: APP_KEY                                            forKey:@"appkey"];
        [prePayParams setObject: nonce_str                                          forKey:@"noncestr"];
        [prePayParams setObject: package                                            forKey:@"package"];
        [prePayParams setObject: time_stamp                                         forKey:@"timestamp"];
        [prePayParams setObject: traceid                                            forKey:@"traceid"];
        
        //生成支付签名
        NSString    *sign;
        sign        = [req createSHA1Sign:prePayParams];
        //增加非参与签名的额外参数
        [prePayParams setObject: @"sha1"                                            forKey:@"sign_method"];
        [prePayParams setObject: sign                                               forKey:@"app_signature"];
        
        //获取prepayId
        NSString *prePayid;
        prePayid            = [req sendPrepay:prePayParams];
        //输出debug info
        debug               = [req getDebugifo];
        NSLog(@"提交预付单: %@\n",debug);
        
        if ( prePayid != nil) {
            //重新按提交格式组包,微信客户端5.0.3以前版本只支持package=Sign=***格式,须考虑升级后支持携带package具体参数的情况
            //package       = [NSString stringWithFormat:@"Sign=%@",package];
            package         = @"Sign=WXPay";
            //签名参数列表
            NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
            [signParams setObject: APPI_ID                                          forKey:@"appid"];
            [signParams setObject: APP_KEY                                          forKey:@"appkey"];
            [signParams setObject: nonce_str                                        forKey:@"noncestr"];
            [signParams setObject: package                                          forKey:@"package"];
            [signParams setObject: PARTNER_ID                                       forKey:@"partnerid"];
            [signParams setObject: time_stamp                                       forKey:@"timestamp"];
            [signParams setObject: prePayid                                         forKey:@"prepayid"];
            
            //生成签名
            sign        = [req createSHA1Sign:signParams];
            
            //输出debug info
            debug     = [req getDebugifo];
            NSLog(@"调起支付签名: %@\n",debug);
            
            //调起微信支付
            PayReq* req = [[PayReq alloc] init];
            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];
        }else{
            /*long errcode = [req getLasterrCode];
             if ( errcode == 40001 )
             {//Token实效,重新获取
             Token                   = [req GetToken];
             token_time              = now + 600;
             NSLog(@"获取Token: %@\n",[req getDebugifo]);
             };*/
            NSLog(@"获取prepayid失败\n");
            [self alert:@"提示信息" msg:debug];
        }
    }else{
        NSLog(@"获取Token失败\n");
        [self alert:@"提示信息" msg:@"获取Token失败"];
    }
    
}

//客户端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    
    [alter show];
}

//- (void)onReq:(BaseReq *)req {
//    NSLog(@"qwe123");
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

 DEMO下载地址:http://pan.baidu.com/s/1jGjCaJc

 补:有些童鞋出现跳到微信支付又跳回来的情况一般是你集成了第三方分享功能需要进行一下判断

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

 

 上面方法里面进行是不是微信支付的判断

 if ([url.host isEqualToString:@"pay"]) {
        [WXApi handleOpenURL:url delegate:self];
    }
    
    return YES;
版权声明:VIC

Android项目中接入微信第三方支付及一些问题

一。接入微信SDK,添加依赖 compile files('libs/wechat-sdk-android-with-mta-1.0.2.jar') 二。新建类,必须放在文件夹weapi下,这...

第三方支付之微信支付

前言上一篇写了支付宝支付,这一篇说下微信支付,废话不多说直接开门见山接入流程1.申请开发者资质地址:https://open.weixin.qq.com/使用公司管理者/高层帐号登录微信开放平台,进入...

第三方支付平台:微信支付接口

第三方支付平台:微信支付接口如果想要实在公众号支付方式,你需要准备以下几项内容: 首先,你要有一个服务号 服务号要开通微信认证...

Android第三方支付--微信支付

转载请标明出处: http://blog.csdn.net/android_it/article/details/50628486; 本文出自:【冯帅的CSDN博客】 做了微信支付,下载...

支付宝微信个人收款接口不让申请,谁来提供收款第三方支付接口

支付宝微信个人收款接口不能申请,聚合支付个人也不能申请。因为骗子太多,支付宝原本支持的个人收款api也被停止服务了。 难道真的没有解决办法了吗?那规规矩矩做网站的个人站长想实现一些收入怎么办呢?个人...

Android第三方支付——微信支付

Android第三方支付——微信支付目录 Android第三方支付微信支付 微信平台注册应用 支付流程图 注册appId 调起支付 支付结果回调 注意事项 之前一直说要坚持写一个技术博客,但是人都...

电商平台接入第三方支付接口之微信支付接入订单系统

边做边更新……………… 先接入微信接口: 支付方式:用户扫描二维码支付 接口类型:扫码支付之模式二 先copy一份模式二的业务流程时序图 业务流程说明: (1)商户后台系统根据用户选购的商品生成...

Http协议与请求, Post请求与Get请求的区别,Base64和URL编码,md5,sha-1加密,对称和非对称加密,支付宝微信第三方支付

Http协议与请求 Post请求 Post请求与Get请求的区别 Get请求的参数是直接放在url后面的,而Post请求是放在请求体中的 Get请求参数的长度...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS第三方支付-微信支付
举报原因:
原因补充:

(最多只允许输入30个字)