美文网首页
iOS微信电子发票获取

iOS微信电子发票获取

作者: PenguinDM | 来源:发表于2017-10-25 10:32 被阅读0次

    开发目标:实现从微信获取发票列表并多选选择发票在系统中

    使用要求:微信开放平台iOS sdk版本1.7.7以上版本(增加发票)

    开发步骤(比较简单,均通过微信官文get请求完成,我用的AF请求的,这个随意)

    1、请求AccessToken,只需你的appid 和 appsecret就行了;不过这个一般都是由服务器来控制,便于管理,时限是7200s。

    - (void)getWxAccessToken

    2、获取api_ticket,需要用到第一步的AccessToken;临时票据,后面签名使用

    (void)getWxApi_ticket

    {

    //https请求方式: GET

    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];

    NSMutableDictionary *mudic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"wx_card",@"type", nil];

    //上文提供的

    [mudic setObject:access_token forKey:@"access_token"];

    [managerGET:@"https://api.weixin.qq.com/cgi-bin/ticket/getticket" parameters:mudicprogress:nil success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {

    NSString *ticket = [responseObject objectForKey:@“ticket”];

    }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){}];}

    3、获取签名方法,需要用到的参数,看下图方法:

    - (NSString *)getCardsign

    {

    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];

    NSString *timestamp = [NSStringstringWithFormat:@"%ld",(long)[NSDate date].timeIntervalSince1970];

    [parDic setObject:timestamp forKey:@"timestamp"];

    NSString *nonceStr= [NSStringstringWithFormat:@"%d", rand()];

    [parDic setObject:nonceStr forKey:@"nonceStr"];

    [parDic setObject:[[NSUserDefaults standardUserDefaults]objectForKey:@"wx_ticket"] forKey:@"api_ticket"];

    [parDic setObject:WXAPPKEY forKey:@"appid"];

    [parDic setObject:@"INVOICE" forKey:@"cardType"];

    //先把这个存起来,因为后面进入微信用到的时间戳,要和签名这一致,不然拿不到发票列表

    [[NSUserDefaults standardUserDefaults] setObject:parDic forKey:@“cardSignDic"];

    [[NSUserDefaults standardUserDefaults] synchronize];

    NSMutableString *contentString=[NSMutableString string];

    NSArray *values = [parDic allValues];

    //按字母顺序排序

    NSArray *sortedArray = [valuessortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {

    return [obj1 compare:obj2 options:NSNumericSearch];

    }];

    //拼接字符串

    for (NSString *value in sortedArray) {

    [contentString appendFormat:@"%@", value];

    }

    NSString *cardSign = [self sha1:contentString];

    return cardSign;

    }

    //sha1加密方式

    - (NSString *)sha1:(NSString *)input

    {

    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes, (unsigned int)data.length, digest);

    NSMutableString *output = [NSMutableStringstringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

    for(int i=0; i<CC_SHA1_DIGEST_LENGTH;i++){

    [output appendFormat:@"%02x", digest[i]];

    }

    return output;

    }

    4、进入微信,请求发票信息

    //请求发票信息

    + (BOOL)chooseInvoice

    {

    NSDictionary *parDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"cardSignDic"];

    WXChooseInvoiceReq *chooseInvoiceReq = [[WXChooseInvoiceReq alloc] init];

    chooseInvoiceReq.appID = appid;

    //前面获取的

    chooseInvoiceReq.cardSign = xxx;

    //前面存的

    chooseInvoiceReq.nonceStr = [parDic objectForKey:@"nonceStr"];

    chooseInvoiceReq.signType = @"SHA1";

    //前面存的

    chooseInvoiceReq.timeStamp = [[parDic objectForKey:@"timestamp"] doubleValue];

    return [WXApi sendReq:chooseInvoiceReq];

    }

    到这就可以进入到微信,看到你开的电子发票的列表了;

    怎么获取信息?

    进入appdelegate

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

    NSLog(@"%@", url.absoluteString);

    NSLog(@"Calling Application Bundle ID: %@",sourceApplication);

    NSLog(@"URL scheme:%@", [url scheme]);

    NSLog(@"URL query: %@", [url query]);

    if([sourceApplication rangeOfString:WXAPP_ID].length > 0) {

    //设置实现回调的类,写上面的方法的单例类就行

    return [WXApi handleOpenURL:url delegate:yourdelegate];

    }

    return YES;

    }

    - (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL *)url {

    //设置实现回调的类,写上面的方法的单例类就行

    return[WXApi handleOpenURL:urldelegate:yourdelegate];

    }

    回调方法:

    -(void)onResp:(BaseResp*)resp

    {

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

    //这里面有数组,自己怎么用都行了,over

    WXChooseInvoiceResp *chooseInvoiceResp = (WXChooseInvoiceResp *)resp;

    }

    }

    很简单,微信良心。

    相关文章

      网友评论

          本文标题:iOS微信电子发票获取

          本文链接:https://www.haomeiwen.com/subject/lkzfpxtx.html