ALiPayHandle.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // ALiPayHandle.m
  3. // tuoyan
  4. //
  5. // Created by admin on 2017/1/19.
  6. // Copyright © 2017年 czy. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "ALiPayHandle.h"
  10. #import "Order.h"
  11. #import "RSADataSigner.h"
  12. #import <AlipaySDK/AlipaySDK.h>
  13. @implementation ALiPayHandle
  14. + (NSString *)doAlipayPay:(NSString *)orderId amount:(NSString *)amount productName:(NSString *)productName productDescription:(NSString *)productDescription notifyURL:(NSString *)notifyURL
  15. {
  16. NSString *appID = @"2017050207079289";
  17. NSString *rsa2PrivateKey = @"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDGD3fGoJrGXYNQ7AyMh2YdMqu4izf6OTg2yy+RoOb9g9wmeeJWBC2IZh+vNwqdE+wHRPu9oq+7iflonq0oXNNttAZ3xnS9cKejhIflzWcRgFyVxE+mZvruKBMOPh1Mk5UxoGqwQAuR3Nr+sDO2hMRKyFtpK4CcfGpq2FD7SCshIVdz71zbvY4l0ZGIDavuUfHWZoTYHt80BUtlS51oFaVWhP8ykwDdZemOv/xPEvbkyMetIopjsKgqARCdzJFUTltaGvP7fiOEFgNHAbpVYeKh4FkIXyxbeVyRdSLKF0zfZIp+eNd263+jNoojHrl5QusGn1L8eanIxSaJQiA3GEfPAgMBAAECggEBAKxjNitKCqzxI6TBb8K47uYm/lPnyNcgCo+Wlla4p5Ssumc62wdnJ4J68MT+sVw6xMsm/b325eHjm5O12J0y/kuTz7VcpKtmbGLkrawU9crD4NHyRildpLa5ZqyU38eRBOBm8bVqaSeSBYJ8NuZ79KXqcbW+SXjqyW0pzoKINBHmenfFGUyZz4ObWqN5sjLVEqLC/D7BulJNXdgoRGdGLJt0ntsV+XJFHdBxBezQmJxStsc6VgBMgd7J7gD9f2ob6hK1ulyJCGHSuH0Ai+sjaxNvdgN/jBxJ3cGQU7MtP8PFdGTgkjBktkXtURTWPL3lDIvE2lacSp9pHoYKaBcMdUECgYEA/fbPMr1rS3YniUzWon81yjEyoDDhgohRXcYBmJ+rzlzBiorswSCVj1ajDvXVGFAoukwNgDi+DqC3mJBb63ftVco97UxHqd3+AzlJqK6p2lhXAt63nCLzE4ipIaB17bkXJVk/yr5DkW86+UrZJyc8sUMXYTYrH6YnYoHeLXLmDzECgYEAx6XuiER0Z3Hjd6vw60XXortWveop6BC+zBvK5Cz55w2TnzzS/byx1Yybmf/paMuh161RZ90dp/avedENhFHky5WOEd39FqjMrG1KBV+rSSSSUJyAnnLAPfqK+lWQFsXXqs4wUgT7hP6NI/VLMTyy2yVRCem/49UHvmnsTLvUBv8CgYAU5wVytahpE5Z5dHuN8E5rD9WM3bkR10prH2ruNuYbHANJ9dk6pAXrQEQ3bQBW+fIAM1ZBIWpzJcM2qVsHbF16XCTwrQCQ+JZ5iXOIkKemMvMmZ1JRZYcAKpVRETXjlQJUrp3TgAlrpO4Dqqdo04/pPw5jj+PIvRIsx2eT8gsBYQKBgDG2mMnnudCUZF7FOs1q68ICwhuyNlMp8Eik1wWwSV4GXG73V1aFaQbS52AWgElvA1LQPogNYDgVziYYLzR2q/EhwWmjjETbLXP2Fst1whIJgEGAZuqNil8khvzE14dEJmrKwu95fCPmusBscRfGlIbLKP297eFOh4obmBKXZrmDAoGAOwQd/g2N7bupnqVF7+B3TfHT2wAXqY84ppn6m4gOZcuXkKx2MKprzNLuAJcQNx4BPGm4+HSxZsaow1gX4230G2yFdXrUknrxEWCqRlZQEPqmnfgC/CsGAKRzXcBw5ufVlJ/tDj+EpcsrALLMHh8rbAF16+D/qVdo5ING2lCEDKc=";
  18. NSString *rsaPrivateKey = @"";
  19. /*============================================================================*/
  20. /*============================================================================*/
  21. /*============================================================================*/
  22. //partner和seller获取失败,提示
  23. if ([appID length] == 0 ||
  24. ([rsa2PrivateKey length] == 0 && [rsaPrivateKey length] == 0))
  25. {
  26. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
  27. message:@"缺少appId或者私钥。"
  28. delegate:self
  29. cancelButtonTitle:@"确定"
  30. otherButtonTitles:nil];
  31. [alert show];
  32. }else{
  33. /*
  34. *生成订单信息及签名
  35. */
  36. //将商品信息赋予AlixPayOrder的成员变量
  37. Order* order = [Order new];
  38. // NOTE: app_id设置
  39. order.app_id = appID;
  40. // NOTE: 支付接口名称
  41. order.method = @"alipay.trade.app.pay";
  42. // NOTE: 参数编码格式
  43. order.charset = @"utf-8";
  44. // NOTE: 当前时间点
  45. NSDateFormatter* formatter = [NSDateFormatter new];
  46. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  47. order.timestamp = [formatter stringFromDate:[NSDate date]];
  48. order.notify_url = notifyURL;
  49. // NOTE: 支付版本
  50. order.version = @"1.0";
  51. // NOTE: sign_type 根据商户设置的私钥来决定
  52. order.sign_type = (rsa2PrivateKey.length > 1)?@"RSA2":@"RSA";
  53. // NOTE: 商品数据
  54. order.biz_content = [BizContent new];
  55. order.biz_content.body = productName;
  56. order.biz_content.subject = productDescription;
  57. order.biz_content.out_trade_no = orderId; //订单ID(由商家自行制定)
  58. order.biz_content.timeout_express = @"30m"; //超时时间设置
  59. order.biz_content.total_amount = amount; //商品价格
  60. //将商品信息拼接成字符串
  61. NSString *orderInfo = [order orderInfoEncoded:NO];
  62. NSString *orderInfoEncoded = [order orderInfoEncoded:YES];
  63. NSLog(@"orderSpec = %@",orderInfo);
  64. // NOTE: 获取私钥并将商户信息签名,外部商户的加签过程请务必放在服务端,防止公私钥数据泄露;
  65. // 需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
  66. NSString *signedString = nil;
  67. RSADataSigner* signer = [[RSADataSigner alloc] initWithPrivateKey:((rsa2PrivateKey.length > 1)?rsa2PrivateKey:rsaPrivateKey)];
  68. if ((rsa2PrivateKey.length > 1)) {
  69. signedString = [signer signString:orderInfo withRSA2:YES];
  70. } else {
  71. signedString = [signer signString:orderInfo withRSA2:NO];
  72. }
  73. // NOTE: 如果加签成功,则继续执行支付
  74. if (signedString != nil) {
  75. //应用注册scheme,在AliSDKDemo-Info.plist定义URL types
  76. // NOTE: 将签名成功字符串格式化为订单字符串,请严格按照该格式
  77. NSString *orderString = [NSString stringWithFormat:@"%@&sign=%@",
  78. orderInfoEncoded, signedString];
  79. return orderString;
  80. }
  81. }
  82. return @"";
  83. }
  84. @end