UnifyPayOrderRequestManager.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // UnifyPayOrderRequestManager.m
  3. // testDEMO
  4. //
  5. // Created by SunXP on 17/4/28.
  6. // Copyright © 2017年 L. All rights reserved.
  7. //
  8. #import "UnifyPayOrderRequestManager.h"
  9. #import "UnifyPayTool.h"
  10. #define TIME_OUT_ORDER 15.0
  11. NSString *const Test_1_Title = @"测试环境_1";
  12. NSString *const Test_2_Title = @"测试环境_2";
  13. NSString *const Test_3_Title = @"测试环境_3";
  14. NSString *const Prod_Title = @"生产环境";
  15. NSString *const True_Title = @"true";
  16. NSString *const False_Title = @"false";
  17. NSString *const MsgSrcId_1016 = @"1016";
  18. NSString *const MsgSrcId_3028 = @"3028";
  19. NSString *const MsgSrcId_3816 = @"3816";
  20. NSString *const MsgSrcId_3194 = @"3194";
  21. NSString *const MsgSrcId_3245 = @"3245";
  22. NSString *const MsgSrcId_1028 = @"1028";
  23. NSString *const MsgSrcId_1000 = @"1000";
  24. NSString *const MsgSrcId_5000 = @"5000";
  25. NSString *const MsgSrc_TestPay = @"WWW.TEST.COM";
  26. NSString *const MsgSrc_NetPay_Demo = @"WWW.PRODTEST.COM";
  27. static UnifyPayOrderRequestManager *shareInstance;
  28. @implementation UnifyPayOrderRequestManager
  29. - (NSData *)packToData {
  30. NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
  31. switch (self.payChannel) {
  32. case unifyPayChannelWXPay:
  33. {
  34. EncodeUnEmptyStrObjctToDic(dataDic, self.requestTimestamp, @"requestTimestamp");
  35. EncodeUnEmptyStrObjctToDic(dataDic, self.merOrderId, @"merOrderId");
  36. EncodeUnEmptyStrObjctToDic(dataDic, self.mid, @"mid");
  37. EncodeUnEmptyStrObjctToDic(dataDic, self.tid, @"tid");
  38. EncodeUnEmptyStrObjctToDic(dataDic, self.instMid, @"instMid");
  39. EncodeUnEmptyStrObjctToDic(dataDic, self.totalAmount, @"totalAmount");
  40. EncodeUnEmptyStrObjctToDic(dataDic, self.msgSrc, @"msgSrc");
  41. EncodeUnEmptyStrObjctToDic(dataDic, self.notifyUrl, @"notifyUrl");
  42. EncodeUnEmptyStrObjctToDic(dataDic, self.subAppId, @"subAppId");
  43. if (self.subOrdersArray.count > 0) {
  44. EncodeUnEmptyStrObjctToDic(dataDic, self.divisionFlag, @"divisionFlag");
  45. EncodeUnEmptyStrObjctToDic(dataDic, self.platformAmount, @"platformAmount");
  46. EncodeUnEmptyStrObjctToDic(dataDic, self.subOrdersStr, @"subOrders");
  47. }
  48. EncodeUnEmptyStrObjctToDic(dataDic, @"wx.appPreOrder", @"msgType");
  49. EncodeUnEmptyStrObjctToDic(dataDic, @"APP", @"tradeType");
  50. EncodeUnEmptyStrObjctToDic(dataDic, @"SHA256", @"signType");
  51. EncodeUnEmptyStrObjctToDic(dataDic, self.srcReserve, @"srcReserve");
  52. EncodeUnEmptyStrObjctToDic(dataDic, self.secureTransaction, @"secureTransaction");
  53. EncodeUnEmptyStrObjctToDic(dataDic, [UnifyPayTool getSHA256Sign:dataDic MD5Key:self.MD5Key], @"sign");
  54. }
  55. break;
  56. case unifyPayChannelPosPay:
  57. {
  58. EncodeUnEmptyStrObjctToDic(dataDic, self.requestTimestamp, @"requestTimestamp");
  59. EncodeUnEmptyStrObjctToDic(dataDic, self.merOrderId, @"merOrderId");
  60. EncodeUnEmptyStrObjctToDic(dataDic, self.mid, @"mid");
  61. EncodeUnEmptyStrObjctToDic(dataDic, self.tid, @"tid");
  62. EncodeUnEmptyStrObjctToDic(dataDic, self.instMid, @"instMid");
  63. EncodeUnEmptyStrObjctToDic(dataDic, self.merchantUserId, @"merchantUserId");
  64. EncodeUnEmptyStrObjctToDic(dataDic, self.mobile, @"mobile");
  65. EncodeUnEmptyStrObjctToDic(dataDic, self.totalAmount, @"totalAmount");
  66. if (self.subOrdersArray.count > 0) {
  67. EncodeUnEmptyStrObjctToDic(dataDic, self.divisionFlag, @"divisionFlag");
  68. EncodeUnEmptyStrObjctToDic(dataDic, self.platformAmount, @"platformAmount");
  69. EncodeUnEmptyStrObjctToDic(dataDic, self.subOrdersStr, @"subOrders");
  70. }
  71. EncodeUnEmptyStrObjctToDic(dataDic, [UnifyPayTool getUUID], @"msgId");
  72. EncodeUnEmptyStrObjctToDic(dataDic, @"ERP_SCANPAY", @"msgSrc");
  73. EncodeUnEmptyStrObjctToDic(dataDic, @"qmf.order", @"msgType");
  74. EncodeUnEmptyStrObjctToDic(dataDic, @"NETPAY", @"orderSource");
  75. EncodeUnEmptyStrObjctToDic(dataDic, self.secureTransaction, @"secureTransaction");
  76. EncodeUnEmptyStrObjctToDic(dataDic, @"SHA256", @"signType");
  77. EncodeUnEmptyStrObjctToDic(dataDic, [UnifyPayTool getSign:dataDic MD5Key:self.MD5Key], @"sign");
  78. }
  79. break;
  80. case unifyPayChannelAlipay:
  81. {
  82. EncodeUnEmptyStrObjctToDic(dataDic, self.requestTimestamp, @"requestTimestamp");
  83. EncodeUnEmptyStrObjctToDic(dataDic, self.merOrderId, @"merOrderId");
  84. EncodeUnEmptyStrObjctToDic(dataDic, self.mid, @"mid");
  85. EncodeUnEmptyStrObjctToDic(dataDic, self.tid, @"tid");
  86. EncodeUnEmptyStrObjctToDic(dataDic, self.instMid, @"instMid");
  87. EncodeUnEmptyStrObjctToDic(dataDic, self.totalAmount, @"totalAmount");
  88. EncodeUnEmptyStrObjctToDic(dataDic, self.msgSrc, @"msgSrc");
  89. if (self.subOrdersArray.count > 0) {
  90. EncodeUnEmptyStrObjctToDic(dataDic, self.divisionFlag, @"divisionFlag");
  91. EncodeUnEmptyStrObjctToDic(dataDic, self.platformAmount, @"platformAmount");
  92. EncodeUnEmptyStrObjctToDic(dataDic, self.subOrdersStr, @"subOrders");
  93. }
  94. EncodeUnEmptyStrObjctToDic(dataDic, [UnifyPayTool getUUID], @"msgId");
  95. EncodeUnEmptyStrObjctToDic(dataDic, @"trade.precreate", @"msgType");
  96. EncodeUnEmptyStrObjctToDic(dataDic, self.srcReserve, @"srcReserve");
  97. EncodeUnEmptyStrObjctToDic(dataDic, @"APP", @"tradeType");
  98. EncodeUnEmptyStrObjctToDic(dataDic, self.secureTransaction, @"secureTransaction");
  99. EncodeUnEmptyStrObjctToDic(dataDic, @"SHA256", @"signType");
  100. EncodeUnEmptyStrObjctToDic(dataDic, [UnifyPayTool getSHA256Sign:dataDic MD5Key:self.MD5Key], @"sign");
  101. }
  102. break;
  103. case unifyPayChannelAliMiniProgramPay:
  104. {
  105. EncodeUnEmptyStrObjctToDic(dataDic, self.requestTimestamp, @"requestTimestamp");
  106. EncodeUnEmptyStrObjctToDic(dataDic, self.merOrderId, @"merOrderId");
  107. EncodeUnEmptyStrObjctToDic(dataDic, self.mid, @"mid");
  108. EncodeUnEmptyStrObjctToDic(dataDic, self.tid, @"tid");
  109. EncodeUnEmptyStrObjctToDic(dataDic, self.instMid, @"instMid");
  110. EncodeUnEmptyStrObjctToDic(dataDic, self.totalAmount, @"totalAmount");
  111. EncodeUnEmptyStrObjctToDic(dataDic, self.msgSrc, @"msgSrc");
  112. if (self.subOrdersArray.count > 0) {
  113. EncodeUnEmptyStrObjctToDic(dataDic, self.divisionFlag, @"divisionFlag");
  114. EncodeUnEmptyStrObjctToDic(dataDic, self.platformAmount, @"platformAmount");
  115. EncodeUnEmptyStrObjctToDic(dataDic, self.subOrdersStr, @"subOrders");
  116. }
  117. EncodeUnEmptyStrObjctToDic(dataDic, [UnifyPayTool getUUID], @"msgId");
  118. EncodeUnEmptyStrObjctToDic(dataDic, @"trade.appPreOrder", @"msgType");
  119. EncodeUnEmptyStrObjctToDic(dataDic, self.srcReserve, @"srcReserve");
  120. EncodeUnEmptyStrObjctToDic(dataDic, @"APP", @"tradeType");
  121. EncodeUnEmptyStrObjctToDic(dataDic, self.secureTransaction, @"secureTransaction");
  122. EncodeUnEmptyStrObjctToDic(dataDic, @"SHA256", @"signType");
  123. EncodeUnEmptyStrObjctToDic(dataDic, [UnifyPayTool getSHA256Sign:dataDic MD5Key:self.MD5Key], @"sign");
  124. }
  125. break;
  126. case unifyPayChannelCloudPay:
  127. {
  128. EncodeUnEmptyStrObjctToDic(dataDic, self.requestTimestamp, @"requestTimestamp");
  129. EncodeUnEmptyStrObjctToDic(dataDic, self.merOrderId, @"merOrderId");
  130. EncodeUnEmptyStrObjctToDic(dataDic, self.mid, @"mid");
  131. EncodeUnEmptyStrObjctToDic(dataDic, self.tid, @"tid");
  132. EncodeUnEmptyStrObjctToDic(dataDic, self.instMid, @"instMid");
  133. EncodeUnEmptyStrObjctToDic(dataDic, self.totalAmount, @"totalAmount");
  134. EncodeUnEmptyStrObjctToDic(dataDic, self.msgSrc, @"msgSrc");
  135. EncodeUnEmptyStrObjctToDic(dataDic, self.notifyUrl, @"notifyUrl");
  136. if (self.subOrdersArray.count > 0) {
  137. EncodeUnEmptyStrObjctToDic(dataDic, self.divisionFlag, @"divisionFlag");
  138. EncodeUnEmptyStrObjctToDic(dataDic, self.platformAmount, @"platformAmount");
  139. EncodeUnEmptyStrObjctToDic(dataDic, self.subOrdersStr, @"subOrders");
  140. }
  141. EncodeUnEmptyStrObjctToDic(dataDic, @"uac.appOrder", @"msgType");
  142. EncodeUnEmptyStrObjctToDic(dataDic, @"APP", @"tradeType");
  143. EncodeUnEmptyStrObjctToDic(dataDic, self.srcReserve, @"srcReserve");
  144. EncodeUnEmptyStrObjctToDic(dataDic, self.secureTransaction, @"secureTransaction");
  145. EncodeUnEmptyStrObjctToDic(dataDic, @"SHA256", @"signType");
  146. EncodeUnEmptyStrObjctToDic(dataDic, [UnifyPayTool getSHA256Sign:dataDic MD5Key:self.MD5Key], @"sign");
  147. }
  148. break;
  149. case unifyPayChannelApplePay:
  150. {
  151. EncodeUnEmptyStrObjctToDic(dataDic, [UnifyPayTool getUUID], @"msgId");
  152. EncodeUnEmptyStrObjctToDic(dataDic, @"QMFGROUP", @"msgSrc");
  153. EncodeUnEmptyStrObjctToDic(dataDic, @"applepay.order", @"msgType");
  154. EncodeUnEmptyStrObjctToDic(dataDic, self.requestTimestamp, @"requestTimestamp");
  155. EncodeUnEmptyStrObjctToDic(dataDic, self.merOrderId, @"merOrderId");
  156. EncodeUnEmptyStrObjctToDic(dataDic, self.srcReserve, @"srcReserve");
  157. EncodeUnEmptyStrObjctToDic(dataDic, self.mid, @"mid");
  158. EncodeUnEmptyStrObjctToDic(dataDic, self.tid, @"tid");
  159. EncodeUnEmptyStrObjctToDic(dataDic, self.instMid, @"instMid");
  160. EncodeUnEmptyStrObjctToDic(dataDic, self.attachedData, @"attachedData");
  161. EncodeUnEmptyStrObjctToDic(dataDic, self.expireTime, @"expireTime");
  162. EncodeUnEmptyStrObjctToDic(dataDic, self.orderDesc, @"orderDesc");
  163. if (self.subOrdersArray.count > 0) {
  164. EncodeUnEmptyStrObjctToDic(dataDic, self.divisionFlag, @"divisionFlag");
  165. EncodeUnEmptyStrObjctToDic(dataDic, self.platformAmount, @"platformAmount");
  166. EncodeUnEmptyStrObjctToDic(dataDic, self.subOrdersStr, @"subOrders");
  167. }
  168. EncodeUnEmptyStrObjctToDic(dataDic, self.originalAmount, @"originalAmount");
  169. EncodeUnEmptyStrObjctToDic(dataDic, self.totalAmount, @"totalAmount");
  170. EncodeUnEmptyStrObjctToDic(dataDic, self.customerId, @"customerId");
  171. EncodeUnEmptyStrObjctToDic(dataDic, self.notifyUrl, @"notifyUrl");
  172. EncodeUnEmptyStrObjctToDic(dataDic, self.signType, @"signType");
  173. EncodeUnEmptyStrObjctToDic(dataDic, self.mobile, @"mobile");
  174. EncodeUnEmptyStrObjctToDic(dataDic, self.secureTransaction, @"secureTransaction");
  175. EncodeUnEmptyStrObjctToDic(dataDic, self.merchantUserId, @"merchantUserId");
  176. EncodeUnEmptyStrObjctToDic(dataDic, [UnifyPayTool getSign:dataDic MD5Key:self.MD5Key], @"sign");
  177. }
  178. break;
  179. default:
  180. return nil;
  181. break;
  182. }
  183. if (self.subOrdersArray.count > 0) {
  184. [dataDic setObject:self.subOrdersArray forKey:@"subOrders"];
  185. }
  186. NSLog(@"\nrequest = %@", dataDic);
  187. return [NSJSONSerialization dataWithJSONObject:dataDic options:NSJSONWritingPrettyPrinted error:nil];
  188. }
  189. - (void)sendOrderRequestWithPostData:(NSData *)postData successHandler:(SuccessHandler)successHandler failHandler:(FailHandler)failHandler {
  190. NSURL *url = [NSURL URLWithString: self.urlStr];
  191. NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url];
  192. request.HTTPMethod = @"POST";
  193. request.HTTPBody = postData;
  194. request.timeoutInterval = TIME_OUT_ORDER;
  195. NSURLSession *session = [NSURLSession sharedSession];
  196. NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  197. dispatch_async(dispatch_get_main_queue(), ^{
  198. if (data) {
  199. NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  200. if ((responseDic != nil) && ([responseDic count] != 0)) {
  201. successHandler(responseDic);
  202. } else {
  203. failHandler();
  204. }
  205. } else {
  206. failHandler();
  207. }
  208. });
  209. }];
  210. [sessionDataTask resume];
  211. }
  212. void EncodeUnEmptyStrObjctToDic(NSMutableDictionary *dic,NSString *object, NSString *key)
  213. {
  214. if (dic == nil || [dic isEqual:[NSNull null]])
  215. {
  216. return;
  217. }
  218. if (object == nil || [object isEqual:[NSNull null]] || [object isEqualToString:@""])
  219. {
  220. return;
  221. }
  222. if (key == nil || [key isEqual:[NSNull null]] || [key isEqualToString:@""])
  223. {
  224. return;
  225. }
  226. [dic setObject:object forKey:key];
  227. }
  228. + (UnifyPayOrderRequestManager *)shareInstance {
  229. @synchronized(self)
  230. {
  231. if (shareInstance==Nil) {
  232. shareInstance=[[self alloc]init];
  233. }
  234. return shareInstance;
  235. }
  236. }
  237. @end