美文网首页
手把手教你iOS微信支付

手把手教你iOS微信支付

作者: 小二同學 | 来源:发表于2017-10-31 12:12 被阅读46次

    和支付宝一样,先去开放平台下载SDK和开发文档,然后添加依赖和framework,按照文档一步一步来。

    • 和支付宝不同的是,微信支付需要两次签名,这两次签名可以都放在服务器端进行,也可以第一次在服务器第二次在app,或者都在app端进行

    首先需要走自己接口获取签名,传入订单号和IP
    IP的话,需要注意WIFI和4G,因为我之前测试的时候一直用的WIFI,然后换成4G,获取不到IP地址。

    #pragma mark - 获取设备当前网络IP地址
    - (NSString *)getIPAddress:(BOOL)preferIPv4
    {
        NSArray *searchArray = preferIPv4 ?
        @[ IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
        @[ IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;
        
        NSDictionary *addresses = [self getIPAddresses];
        NSLog(@"addresses: %@", addresses);
        
        __block NSString *address;
        [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
         {
             address = addresses[key];
             //筛选出IP地址格式
             if([self isValidatIP:address]) *stop = YES;
         } ];
        return address ? address : @"0.0.0.0";
    }
    
    - (BOOL)isValidatIP:(NSString *)ipAddress {
        if (ipAddress.length == 0) {
            return NO;
        }
        NSString *urlRegEx = @"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
        
        NSError *error;
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:urlRegEx options:0 error:&error];
        
        if (regex != nil) {
            NSTextCheckingResult *firstMatch=[regex firstMatchInString:ipAddress options:0 range:NSMakeRange(0, [ipAddress length])];
            
            if (firstMatch) {
                NSRange resultRange = [firstMatch rangeAtIndex:0];
                NSString *result=[ipAddress substringWithRange:resultRange];
                //输出结果
                NSLog(@"%@",result);
                return YES;
            }
        }
        return NO;
    }
    
    - (NSDictionary *)getIPAddresses
    {
        NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
        
        // retrieve the current interfaces - returns 0 on success
        struct ifaddrs *interfaces;
        if(!getifaddrs(&interfaces)) {
            // Loop through linked list of interfaces
            struct ifaddrs *interface;
            for(interface=interfaces; interface; interface=interface->ifa_next) {
                if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
                    continue; // deeply nested code harder to read
                }
                const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
                char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
                if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
                    NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
                    NSString *type;
                    if(addr->sin_family == AF_INET) {
                        if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
                            type = IP_ADDR_IPv4;
                        }
                    } else {
                        const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
                        if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
                            type = IP_ADDR_IPv6;
                        }
                    }
                    if(type) {
                        NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
                        addresses[key] = [NSString stringWithUTF8String:addrBuf];
                    }
                }
            }
            // Free memory
            freeifaddrs(interfaces);
        }
        return [addresses count] ? addresses : nil;
    }
    

    上面这个方法是获取IP地址

    dic[@"ip"] = [self getIPAddress:YES]; // 调用方法获取IP
    

    然后接口返回签名和调起微信需要的参数

    //调起微信支付
    PayReq* req             = [[PayReq alloc] init];
    req.openID              = [dataDic objectForKey:@"appid"];
    req.partnerId           = [dataDic objectForKey:@"mch_id"];
    req.prepayId            = [dataDic objectForKey:@"prepay_id"];
    req.nonceStr            = [dataDic objectForKey:@"nonce_str"];
    req.timeStamp           = [[dataDic objectForKey:@"timestamp"] intValue];
    req.package             = @"Sign=WXPay";
    

    拿到参数之后,把链接拼接然后再进行第二次签名也就是加密接口

    NSString *origin = [NSString stringWithFormat:@"appid=%@&noncestr=%@&package=%@&partnerid=%@&prepayid=%@&timestamp=%d",[dataDic objectForKey:@"appid"],[dataDic objectForKey:@"nonce_str"],@"Sign=WXPay",[dataDic objectForKey:@"mch_id"],[dataDic objectForKey:@"prepay_id"],[[dataDic objectForKey:@"timestamp"] intValue]];
    NSString *stringSignTemp = [NSString stringWithFormat:@"%@&key=%@",origin,[dataDic objectForKey:@"key"]];
    

    最后复制sign值,调起微信搞定。

    req.sign                = [self md5:stringSignTemp]; // 加密接口用的方法 在下面
    [WXApi sendReq:req];
    
    - (NSString *) md5:(NSString *) input {
        const char *cStr = [input UTF8String];
        unsigned char digest[CC_MD5_DIGEST_LENGTH];
        CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
        
        NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
        
        for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
            [output appendFormat:@"%02x", digest[i]];
        
        return output;
    }
    
    • 基本上就这么多吧。

    相关文章

      网友评论

          本文标题:手把手教你iOS微信支付

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