IOS-发起微信支付方法
【摘要】
WXPay+MD5.h
//// NSObject+MD5.h// NEWDLD//// Created by zz on 16/7/28.// Copyright © 2016年 dld. All rights reserved.// #import <Foundation/Foundation.h>#impo...
WXPay+MD5.h
-
//
-
// NSObject+MD5.h
-
// NEWDLD
-
//
-
// Created by zz on 16/7/28.
-
// Copyright © 2016年 dld. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
#import "WXApi.h"
-
-
@interface MD5 : NSObject
-
+ (NSString *)getMd5_32Bit_String:(NSString *)srcString;
-
-
//创建发起支付时的sign签名
-
+(NSString *)createMD5SingForPayWithAppID:(PayReq *)req;
-
-
@end
WXPay+MD5.m
-
//
-
// NSObject+MD5.m
-
// NEWDLD
-
//
-
// Created by zz on 16/7/28.
-
// Copyright © 2016年 dld. All rights reserved.
-
//
-
-
#import "WXPay+MD5.h"
-
#import <CommonCrypto/CommonDigest.h>
-
-
-
@implementation MD5
-
-
+ (NSString *)getMd5_32Bit_String:(NSString *)srcString
-
{
-
const char *cStr = [srcString UTF8String];
-
unsigned char digest[CC_MD5_DIGEST_LENGTH];
-
CC_MD5( cStr, strlen(cStr), digest );
-
NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
-
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
-
[result appendFormat:@"%02x", digest[i]];
-
-
return result;
-
}
-
-
//创建发起支付时的sign签名
-
+(NSString *)createMD5SingForPayWithAppID:(PayReq *)req
-
{
-
NSString *parter_key = @"dld123******************";
-
NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
-
[signParams setObject:req.openID forKey:@"appid"];//微信appid
-
[signParams setObject:req.nonceStr forKey:@"noncestr"];//随机字符串
-
[signParams setObject:req.package forKey:@"package"];//扩展字段 参数为 Sign=WXPay
-
[signParams setObject:req.partnerId forKey:@"partnerid"];//商户账号
-
[signParams setObject:req.prepayId forKey:@"prepayid"];//此处为统一下单接口返回的预支付订单号
-
[signParams setObject:[NSString stringWithFormat:@"%ld",req.timeStamp] forKey:@"timestamp"];//时间戳
-
-
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 ( ![[signParams objectForKey:categoryId] isEqualToString:@""]
-
&& ![[signParams objectForKey:categoryId] isEqualToString:@"sign"]
-
&& ![[signParams objectForKey:categoryId] isEqualToString:@"key"]
-
)
-
{
-
[contentString appendFormat:@"%@=%@&", categoryId, [signParams objectForKey:categoryId]];
-
}
-
}
-
//添加商户密钥key字段 API 密钥
-
[contentString appendFormat:@"key=%@", parter_key];
-
NSString *result = [MD5 getMd5_32Bit_String:contentString];//md5加密
-
return result;
-
}
-
@end
-
PayReq *req = [[PayReq alloc]init];
-
req.openID = [[NSString alloc]initWithFormat:@"%@",payUrl[@"appid"]];
-
req.partnerId = payUrl[@"partnerid"];
-
req.prepayId = payUrl[@"prepayid"];
-
req.nonceStr = payUrl[@"noncestr"];
-
req.timeStamp = (UInt32)[payUrl[@"timestamp"]integerValue];
-
req.package = @"Sign=WXPay";
-
req.sign = [MD5 createMD5SingForPayWithAppID:req];//payUrl[@"sign"];
-
-
BOOL flag = [WXApi sendReq:req];
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/52209065
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)