Order.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // Order.m
  3. // AliSDKDemo
  4. //
  5. // Created by 方彬 on 07/25/16.
  6. //
  7. //
  8. #import "Order.h"
  9. @implementation BizContent
  10. - (NSString *)description {
  11. NSMutableDictionary *tmpDict = [NSMutableDictionary new];
  12. // NOTE: 增加不变部分数据
  13. [tmpDict addEntriesFromDictionary:@{@"subject":_subject?:@"",
  14. @"out_trade_no":_out_trade_no?:@"",
  15. @"total_amount":_total_amount?:@"",
  16. @"seller_id":_seller_id?:@"",
  17. @"product_code":_product_code?:@"QUICK_MSECURITY_PAY"}];
  18. // NOTE: 增加可变部分数据
  19. if (_body.length > 0) {
  20. [tmpDict setObject:_body forKey:@"body"];
  21. }
  22. if (_timeout_express.length > 0) {
  23. [tmpDict setObject:_timeout_express forKey:@"timeout_express"];
  24. }
  25. // NOTE: 转变得到json string
  26. NSData* tmpData = [NSJSONSerialization dataWithJSONObject:tmpDict options:0 error:nil];
  27. NSString* tmpStr = [[NSString alloc]initWithData:tmpData encoding:NSUTF8StringEncoding];
  28. return tmpStr;
  29. }
  30. @end
  31. @implementation Order
  32. - (NSString *)orderInfoEncoded:(BOOL)bEncoded {
  33. if (_app_id.length <= 0) {
  34. return nil;
  35. }
  36. // NOTE: 增加不变部分数据
  37. NSMutableDictionary *tmpDict = [NSMutableDictionary new];
  38. [tmpDict addEntriesFromDictionary:@{@"app_id":_app_id,
  39. @"method":_method?:@"alipay.trade.app.pay",
  40. @"charset":_charset?:@"utf-8",
  41. @"timestamp":_timestamp?:@"",
  42. @"version":_version?:@"1.0",
  43. @"biz_content":_biz_content.description?:@"",
  44. @"sign_type":_sign_type?:@"RSA"}];
  45. // NOTE: 增加可变部分数据
  46. if (_format.length > 0) {
  47. [tmpDict setObject:_format forKey:@"format"];
  48. }
  49. if (_return_url.length > 0) {
  50. [tmpDict setObject:_return_url forKey:@"return_url"];
  51. }
  52. if (_notify_url.length > 0) {
  53. [tmpDict setObject:_notify_url forKey:@"notify_url"];
  54. }
  55. if (_app_auth_token.length > 0) {
  56. [tmpDict setObject:_app_auth_token forKey:@"app_auth_token"];
  57. }
  58. // NOTE: 排序,得出最终请求字串
  59. NSArray* sortedKeyArray = [[tmpDict allKeys] sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  60. return [obj1 compare:obj2];
  61. }];
  62. NSMutableArray *tmpArray = [NSMutableArray new];
  63. for (NSString* key in sortedKeyArray) {
  64. NSString* orderItem = [self orderItemWithKey:key andValue:[tmpDict objectForKey:key] encoded:bEncoded];
  65. if (orderItem.length > 0) {
  66. [tmpArray addObject:orderItem];
  67. }
  68. }
  69. return [tmpArray componentsJoinedByString:@"&"];
  70. }
  71. - (NSString*)orderItemWithKey:(NSString*)key andValue:(NSString*)value encoded:(BOOL)bEncoded
  72. {
  73. if (key.length > 0 && value.length > 0) {
  74. if (bEncoded) {
  75. value = [self encodeValue:value];
  76. }
  77. return [NSString stringWithFormat:@"%@=%@", key, value];
  78. }
  79. return nil;
  80. }
  81. - (NSString*)encodeValue:(NSString*)value
  82. {
  83. NSString* encodedValue = value;
  84. if (value.length > 0) {
  85. encodedValue = (__bridge_transfer NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)value, NULL, (__bridge CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );
  86. }
  87. return encodedValue;
  88. }
  89. @end