服务端集成支付宝踩过的坑RSA、RSA2

 坑

在配置蚂蚁开发平台的时候,切记一定要注意选择的加密方式是RSA,还是RSA2。因为这两种方式生成的支付宝公匙是不一样的。RSA2对应的是2048位支付宝公匙。在配置类Config中,要根据加密方式配置支付宝公匙。

使用

在有支付场景的IOS、android的APP中,为了保证数据的安全,在服务器端保存商家的密匙等敏感信息,并且在服务器生成签名供给手机端。如下流程图:

 

为了以后项目中快速集成,因此把代码整理成只需在服务器端配置商家信息。如下:

 1         static Config()
 2         {
 3             //合作身份者ID,以2088开头由16位纯数字组成的字符串
 4             partner = "";
 5             //商户的私钥            
 6             private_key = @"";
 7 
 8             //支付宝的公钥无需修改该值
 9             public_key = @"";            
10 
11             appId = "";
12 
13             //字符编码格式 目前支持gbk或utf-8
14             input_charset = "utf-8";
15 
16             //签名方式,选择项:RSA、DSA、MD5
17             sign_type = "RSA";
18             //支付宝消息验证地址
19             https_veryfy_url = "https://mapi.alipay.com/gateway.do?service=notify_verify&";
20 
21             //通知回调url
22             notify_url = "";
23         }

第二、在WebAPI中调用

在手机app端调用的时候,只要传商品信息即可:

 1 public class PayModel
 2     {
 3         public string body { get; set; }
 4 
 5         public string subject { get; set; }
 6 
 7         public string Id { get; set; }
 8 
 9         public DateTime CreateTime { get; set; }
10 
11         public decimal total_amount { get; set; }
12     }
View Code
 1 namespace ITWheels.WebApi.Controllers
 2 {
 3     public class ItWheelPayController : ApiController
 4     {
 5         [HttpPost]
 6         public string GetAliPaySign([FromBody]PayModel model)
 7         {
 8             //调用helper的签名方法
 9             return Helper.GetSign(model);
10         }
11     }
12 }
webApi代码

 源码下载

原文地址:https://www.cnblogs.com/liangxiarong/p/8059199.html