using System.Collections.Generic; using Alipay.AopSdk.Core.Util; using GxPress.Common.AliPay; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace GxPress.Api.WebControllers { [Route("api/web/alipay")] [ApiController] [Authorize] public class AlipayController : Controller { [HttpGet()] [AllowAnonymous] public string GetNativePayUrl() { var alipay = new PcPay(); return alipay.PayRequest("637263608658642540", "汪峰讲故事", "1", "汪峰讲故事"); } /// /// 回调地址 /// [HttpGet("notify")] [AllowAnonymous] public void Callback() { /* 实际验证过程建议商户添加以下校验。 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号, 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额), 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email) 4、验证app_id是否为该商户本身。 */ Dictionary sArray = GetRequestGet(); if (sArray.Count != 0) { bool flag = AlipaySignature.RSACheckV1(sArray, Config.AlipayPublicKey, Config.CharSet, Config.SignType, false); if (flag) { //Console.WriteLine($"同步验证通过,订单号:{sArray["out_trade_no"]}"); ViewData["PayResult"] = "同步验证通过"; } else { // Console.WriteLine($"同步验证失败,订单号:{sArray["out_trade_no"]}"); ViewData["PayResult"] = "同步验证失败"; } } } private Dictionary GetRequestPost() { Dictionary sArray = new Dictionary(); ICollection requestItem = Request.Form.Keys; foreach (var item in requestItem) { sArray.Add(item, Request.Form[item]); } return sArray; } private Dictionary GetRequestGet() { Dictionary sArray = new Dictionary(); ICollection requestItem = Request.Query.Keys; foreach (var item in requestItem) { sArray.Add(item, Request.Query[item]); } return sArray; } } }