微信支付sign生成方法

时间:2016-01-18 17:34:28   收藏:0   阅读:2602

        NSMutableDictionary *signParams = [NSMutableDictionary dictionary];

        [signParams setObject: req.openID        forKey:@"appid"];

        [signParams setObject: req.nonceStr    forKey:@"noncestr"];

        [signParams setObject: req.package      forKey:@"package"];

        [signParams setObject: req.partnerId        forKey:@"partnerid"];

        [signParams setObject: @(req.timeStamp)    forKey:@"timestamp"];

        [signParams setObject: req.prepayId      forKey:@"prepayid"];

        

        NSMutableString *contentString  =[NSMutableString string];

        NSArray *keys = [signParams allKeys];

        //按字母顺序排序

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

            return [obj1 compare:obj2 options:NSNumericSearch];

        }];

        //拼接字符串

        for (NSString *categoryId in sortedArray) {

            if (   ![[dict objectForKey:categoryId] isEqualToString:@""]

                && ![categoryId isEqualToString:@"sign"]

                && ![categoryId isEqualToString:@"key"]

                )

            {

                [contentString appendFormat:@"%@=%@&", categoryId, [signParams objectForKey:categoryId]];

            }

            

        }

        //添加key字段

        [contentString appendFormat:@"key=%@", @"商户api密钥"];

 

        req.sign                = [contentString MD5];

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!