美文网首页
iOS 一个App多个微信商户号支付

iOS 一个App多个微信商户号支付

作者: Shadowoff | 来源:发表于2018-08-03 12:49 被阅读0次

    微信支付集成相关的东西我就不在这里复述了,网上一大堆文章,大家搜索一下按照步骤应该都没啥问题,两个商户的回调处理我还没做,有时间会更新,废话不多说,直接开整

    记得把2个商户号都填写到 URL Schemes中

    1、在AppDelegation didFinishLaunchingWithOptions 中设置

    1、注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wechatPayResigerNotifi:) name:@"wechatPayResiger" object:nil];
    
    
    2、通知回调
    #pragma mark  -------------- 微信注册通知回调
    - (void)wechatPayResigerNotifi:(NSNotification *)notifi
    {
        NSDictionary *dict =notifi.userInfo;
        if ([dict[@"key"] intValue] == 1) {
            //.商户1
            [WXApi registerApp:@"wxx124124xx1241412"];
    
        } else {
            //.商户2
            [WXApi registerApp:@"wxx124124xx1242141"];
        }
    }
    
    
    

    2、在支付界面(或者自定义界面)中设置

    dict中的参数是后台返回的参数(商户1,商户2全都用这一个方法就可以)

    [[NSNotificationCenter defaultCenter] postNotificationName:@"wechatPayResiger" object:@"" userInfo:@{@"key" : @"1"}];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"partnerId"] = @"1";
    dict[@"prepayId"] = @"2";
    dict[@"nonceStr"] = @"3";
    dict[@"api_key"] = @"4";
    dict[@"timeStamp"] = @"5";
    dict[@"appId"] = @"6";
    [self wxpay:dict];
    
    

    3、因为后台没有进行2次签名,所以前天做了2次签名,建议签名全部由后台做 ,我项目中是需要进行2次签名的,如果读者不需要2次签名,直接用后台返回的参数调起微信就可以

     记得导入 #import <CommonCrypto/CommonDigest.h>
    
    - (void)wxpay:(NSDictionary *)wechatDic {
        time_t now;
        time(&now);
        NSString *timestamp = [NSString stringWithFormat:@"%ld",now];
        NSString *noncestr = [self md5:timestamp];
        NSDictionary *dict = @{
                               @"appid":[wechatDic objectForKey:@"appId"],
                               @"noncestr":noncestr,
                               @"package":@"Sign=WXPay",
                               @"partnerid":[wechatDic objectForKey:@"partnerId"],
                               @"prepayid":[wechatDic objectForKey:@"prepayId"],
                               @"timestamp":timestamp
                               };
        NSMutableString *contentString = [NSMutableString string];
        NSArray *keys = [dict allKeys];
        // 按照ASCII 码排序
        NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            return [obj1 compare:obj2 options:NSNumericSearch];
        }];
        // 拼接字符串
        for (NSString *categoryId in sortedArray) {
            if (![[dict objectForKey:categoryId] isEqualToString:@""]&&![[dict objectForKey:categoryId] isEqualToString:@"key"]&&![[dict objectForKey:categoryId] isEqualToString:@"sign"]) {
                [contentString appendFormat:@"%@=%@&",categoryId,dict[categoryId]];
            }
        }
        
        // 添加商户key字段
        NSString *secretkey = [wechatDic objectForKey:@"api_key"];
        [contentString appendFormat:@"key=%@",secretkey];
        // 加密
        NSString *md5Sign = [self md5:contentString];
        // 支付数据
        PayReq *req = [[PayReq alloc] init];
        req.openID = [wechatDic objectForKey:@"appId"];
        req.partnerId = [wechatDic objectForKey:@"partnerId"];
        req.prepayId = [wechatDic objectForKey:@"prepayId"];
        req.package = @"Sign=WXPay";
        req.nonceStr = noncestr;
        req.timeStamp = [timestamp intValue];
        req.sign = md5Sign;
        [WXApi sendReq:req];
    }
    
    - (NSString *)md5:(NSString *)str {
        const char *cStr = [str UTF8String];
        unsigned char digest[CC_MD5_DIGEST_LENGTH];
        CC_MD5(cStr,(unsigned int)strlen(cStr),digest);
        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 一个App多个微信商户号支付

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