1 NSURL * myURL_APP_A = [NSURL URLWithString:@"alipay:"];
2 if (![[UIApplication sharedApplication] canOpenURL:myURL_APP_A]) {
3 //如果没有安装支付宝客户端那么需要安装
4 [MBProgressHUD showMBPAlertView:@"您还没有安装支付宝" withSecond:2.0];
5 return;
6 }
7 NSDictionary *dic = @{
8 @"orderNo":orderModel.orderNo
9 };
10 [[SG_HttpsTool sharedSG_HttpsTool] postWithURL:SHAlipayRequestUrl params:dic success:^(id JSON, int code, NSString *msg) {
11
12 if (code == 0) {
13
14 SH_PayAlipayModel *aliModel = [SH_PayAlipayModel mj_objectWithKeyValues:JSON[@"alipaymentOrder"]];
15 //私钥是后台分发的
16 NSString *rsa2PrivateKey = AliPrivateKey;
17 NSString *rsaPrivateKey = @"";
18 //AppIDhe PrivateKey没有配置的提示
19 if ([AlipayAppId length] == 0 || [AliPrivateKey length] == 0) {
20 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"缺少appid或者私钥" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
21 [alert show];
22 return;
23 }
24
25
26 /**
27 * 生成订单信息
28 * 将商品信息赋予AliPayOrder的成员变量
29 */
30 Order *order = [[Order alloc] init];
31 //appid---》之前获得的APPID
32 order.app_id = AlipayAppId;
33 //支付接口名称
34 order.method = @"alipay.trade.app.pay";
35 //参数编码格式
36 order.charset = @"utf-8";
37 //当前时间点
38 NSDateFormatter *formatter = [NSDateFormatter new];
39 [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
40 order.timestamp = [formatter stringFromDate:[NSDate date]];
41
42 //支付版本
43 order.version = @"1.0";
44 order.sign_type = (rsa2PrivateKey.length > 1)?@"RSA2":@"RSA";
45 order.notify_url = aliModel.notifyUrl;
46
47 order.biz_cOntent= [BizContent new];
48 order.biz_content.body = aliModel.Description;
49 //支付宝支付页面的订单信息
50 order.biz_content.subject = aliModel.title;
51 //订单id----》后台返回的
52 order.biz_content.out_trade_no = aliModel.outTradeNo;
53 //超时时间
54 order.biz_content.timeout_express = @"30s";
55 //价格---》后台返回
56 order.biz_content.total_amount = aliModel.totalAmount;
57
58 order.biz_content.product_code = @"QUICK_MSECURITY_PAY";
59
60 /**
61 * 将商品信息拼接成字符串
62 */
63 NSString *orderInfo = [order orderInfoEncoded:NO];
64 NSString *orderInfoEncoded = [order orderInfoEncoded:YES];
65 //SHLog(@"orderSpec = %@", orderInfo)
66
67 //获取私钥并将商户信息签名,外部商户的加签过程务必放在服务端,防止公钥数据泄露
68 //需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
69 NSString *signedString = nil;
70 APRSASigner* signer = [[APRSASigner alloc] initWithPrivateKey:((rsa2PrivateKey.length > 1)?rsa2PrivateKey:rsaPrivateKey)];
71 if ((rsa2PrivateKey.length > 1)) {
72 signedString = [signer signString:orderInfo withRSA2:YES];
73 } else {
74 signedString = [signer signString:orderInfo withRSA2:NO];
75 }
76 SHLog(@"%@", signedString)
77 //如果加签成功,则继续执行支付
78 if (signedString != nil) {
79 //应用注册scheme,在info.plist定义URL types
80 //将签名成功字符串格式化为订单字符串,请严格按照该格式
81 NSString *orderString = [NSString stringWithFormat:@"%@&sign=%@", orderInfoEncoded, signedString];
82 //调用支付结果开始支付, AliAppScheme-->之前设置的Url Schemes
83
84 [[AlipaySDK defaultService] payOrder:orderString fromScheme:AliAppScheme callback:^(NSDictionary *resultDic) {
85 int resultStatus = [resultDic[@"resultStatus"] intValue];
86 SHLog(@"%d", resultStatus)
87 if (resultStatus == 9000) {
88
89 }
90 }];
91
92 }
93
94 }
95 } failure:^(NSError *error) {
96
97 }];