ViewController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // ViewController.m
  3. // Demo
  4. //
  5. // Created by BensonZhang on 15/11/13.
  6. // Copyright © 2015年 xunlian. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "AllPayRequest.h"
  10. #import "PayOrder.h"
  11. #import "DateUtil.h"
  12. #import "SignUtil.h"
  13. #import "AllPaySDK.h"
  14. @interface ViewController ()
  15. @end
  16. @implementation ViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. // [self.view setBackgroundColor:[UIColor yellowColor]];
  20. UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  21. btn1.tag = 2;
  22. btn1.frame = CGRectMake(80, 60, 200, 50);
  23. [btn1 setTitle:@"支付宝支付" forState:UIControlStateNormal];
  24. [btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  25. [self.view addSubview:btn1];
  26. UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  27. btn2.tag = 3;
  28. btn2.frame = CGRectMake(80, 110, 200, 50);
  29. [btn2 setTitle:@"微信支付" forState:UIControlStateNormal];
  30. [btn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  31. [self.view addSubview:btn2];
  32. UIButton * btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  33. btn3.tag = 4;
  34. btn3.frame = CGRectMake(80, 160, 200, 50);
  35. [btn3 setTitle:@"UP支付" forState:UIControlStateNormal];
  36. [btn3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  37. [self.view addSubview:btn3];
  38. UIButton * btn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  39. btn4.tag = 5;
  40. btn4.frame = CGRectMake(80, 210, 200, 50);
  41. [btn4 setTitle:@"Apple Pay" forState:UIControlStateNormal];
  42. [btn4 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  43. [self.view addSubview:btn4];
  44. }
  45. -(void)btnClick:(UIButton *)btn
  46. {
  47. NSMutableDictionary * paramDic=[[NSMutableDictionary alloc]init];
  48. [paramDic setObject:@"VER000000002" forKey:@"version"];
  49. [paramDic setObject:@"UTF-8" forKey:@"charSet"];
  50. [paramDic setObject:@"PURC" forKey:@"transType"];
  51. [paramDic setObject:[DateUtil getDate:@"YYYYMMddHHmmss"] forKey:@"orderNum"];
  52. [paramDic setObject:@"0.1" forKey:@"orderAmount"];
  53. [paramDic setObject:@"CNY" forKey:@"orderCurrency"];
  54. [paramDic setObject:@"nil" forKey:@"frontURL"];
  55. [paramDic setObject:@"http://example.com/example" forKey:@"backURL"];
  56. [paramDic setObject:@"你好吗" forKey:@"merReserve"];
  57. [paramDic setObject:@"test" forKey:@"goodsInfo"];
  58. [paramDic setObject:@"000000000000015" forKey:@"merID"];
  59. [paramDic setObject:@"99020344" forKey:@"acqID"];
  60. [paramDic setObject:[DateUtil getDate:@"YYYYMMddHHmmss"] forKey:@"transTime"];
  61. [paramDic setObject:@"MD5" forKey:@"signType"];
  62. NSString *detailInfo = @"IFt7Imdvb2RzX25hbWUiOiLkuIDkuKrlj4jlpKflj4jlnIbnmoToi7nmnpwiLCJxdWFudGl0eSI6IjEifSx7Imdvb2RzX25hbWUiOiLljY7kuLpQMjAgcGx1cyIsInF1YW50aXR5IjoiNCJ9XQ==";
  63. [paramDic setObject:detailInfo forKey:@"detailInfo"];
  64. if (btn.tag == 2)//支付宝
  65. {
  66. [paramDic setObject:@"APMP" forKey:@"paymentSchema"];
  67. }else if (btn.tag == 3)//微信
  68. {
  69. [paramDic setObject:@"WX" forKey:@"paymentSchema"];
  70. }else if (btn.tag == 4)//银联UP
  71. {
  72. [paramDic setObject:@"UP" forKey:@"paymentSchema"];
  73. }else if (btn.tag == 5)//APP
  74. {
  75. [paramDic setObject:@"APP" forKey:@"paymentSchema"];
  76. }
  77. [self startPay:paramDic];
  78. }
  79. -(void)startPay:(NSMutableDictionary*)paramDic
  80. {
  81. [paramDic setObject:[SignUtil getSign:paramDic] forKey:@"signature"];
  82. NSString *url = @"https://testapi.allpayx.com/app/pay";
  83. // NSString *url = @"https://api.allpayx.com/app/pay";
  84. [AllPayRequest postRequestWithURL:url paramters:paramDic succeed:^(id finResponseData)
  85. {
  86. NSError *error;
  87. NSDictionary * diction = [NSJSONSerialization JSONObjectWithData:finResponseData options:NSJSONReadingMutableContainers error:&error];
  88. if(!error){
  89. NSLog(@"-----输出数据---%@",diction);
  90. [AllPaySDK pay:[diction objectForKey:@"tn"] mode:NO scheme:@"com.xxx.xxx" ViewController:self onResult: ^(NSDictionary *resultDic) {
  91. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resultDic options:NSJSONWritingPrettyPrinted error:nil];
  92. NSString *message =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  93. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付结果" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  94. [alert show];
  95. }];
  96. }else{
  97. NSString * dataString = [[NSString alloc]initWithData:finResponseData encoding:NSUTF8StringEncoding];
  98. NSLog(@"参数出错===\n %@",dataString);
  99. }
  100. } failed:^(id faiResponseData) {
  101. }];
  102. }
  103. - (NSString *)getCurrentLanguage
  104. {
  105. NSArray *languages = [NSLocale preferredLanguages];
  106. NSString *currentLanguage = [languages objectAtIndex:0];
  107. NSLog( @"%@" , currentLanguage);
  108. return currentLanguage;
  109. }
  110. @end