坑
在配置螞蟻開發平台的時候,切記一定要注意選擇的加密方式是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 }

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 }