IOS-发起微信支付方法

举报
清雨小竹 发表于 2022/09/25 00:14:54 2022/09/25
【摘要】 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


  
  1. //
  2. // NSObject+MD5.h
  3. // NEWDLD
  4. //
  5. // Created by zz on 16/7/28.
  6. // Copyright © 2016年 dld. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "WXApi.h"
  10. @interface MD5 : NSObject
  11. + (NSString *)getMd5_32Bit_String:(NSString *)srcString;
  12. //创建发起支付时的sign签名
  13. +(NSString *)createMD5SingForPayWithAppID:(PayReq *)req;
  14. @end

WXPay+MD5.m


  
  1. //
  2. // NSObject+MD5.m
  3. // NEWDLD
  4. //
  5. // Created by zz on 16/7/28.
  6. // Copyright © 2016年 dld. All rights reserved.
  7. //
  8. #import "WXPay+MD5.h"
  9. #import <CommonCrypto/CommonDigest.h>
  10. @implementation MD5
  11. + (NSString *)getMd5_32Bit_String:(NSString *)srcString
  12. {
  13. const char *cStr = [srcString UTF8String];
  14. unsigned char digest[CC_MD5_DIGEST_LENGTH];
  15. CC_MD5( cStr, strlen(cStr), digest );
  16. NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
  17. for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
  18. [result appendFormat:@"%02x", digest[i]];
  19. return result;
  20. }
  21. //创建发起支付时的sign签名
  22. +(NSString *)createMD5SingForPayWithAppID:(PayReq *)req
  23. {
  24. NSString *parter_key = @"dld123******************";
  25. NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
  26. [signParams setObject:req.openID forKey:@"appid"];//微信appid
  27. [signParams setObject:req.nonceStr forKey:@"noncestr"];//随机字符串
  28. [signParams setObject:req.package forKey:@"package"];//扩展字段 参数为 Sign=WXPay
  29. [signParams setObject:req.partnerId forKey:@"partnerid"];//商户账号
  30. [signParams setObject:req.prepayId forKey:@"prepayid"];//此处为统一下单接口返回的预支付订单号
  31. [signParams setObject:[NSString stringWithFormat:@"%ld",req.timeStamp] forKey:@"timestamp"];//时间戳
  32. NSMutableString *contentString =[NSMutableString string];
  33. NSArray *keys = [signParams allKeys];
  34. //按字母顺序排序
  35. NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  36. return [obj1 compare:obj2 options:NSNumericSearch];
  37. }];
  38. //拼接字符串
  39. for (NSString *categoryId in sortedArray) {
  40. if ( ![[signParams objectForKey:categoryId] isEqualToString:@""]
  41. && ![[signParams objectForKey:categoryId] isEqualToString:@"sign"]
  42. && ![[signParams objectForKey:categoryId] isEqualToString:@"key"]
  43. )
  44. {
  45. [contentString appendFormat:@"%@=%@&", categoryId, [signParams objectForKey:categoryId]];
  46. }
  47. }
  48. //添加商户密钥key字段 API 密钥
  49. [contentString appendFormat:@"key=%@", parter_key];
  50. NSString *result = [MD5 getMd5_32Bit_String:contentString];//md5加密
  51. return result;
  52. }
  53. @end


  
  1. PayReq *req = [[PayReq alloc]init];
  2. req.openID = [[NSString alloc]initWithFormat:@"%@",payUrl[@"appid"]];
  3. req.partnerId = payUrl[@"partnerid"];
  4. req.prepayId = payUrl[@"prepayid"];
  5. req.nonceStr = payUrl[@"noncestr"];
  6. req.timeStamp = (UInt32)[payUrl[@"timestamp"]integerValue];
  7. req.package = @"Sign=WXPay";
  8. req.sign = [MD5 createMD5SingForPayWithAppID:req];//payUrl[@"sign"];
  9. BOOL flag = [WXApi sendReq:req];


文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/52209065

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。