在ASPNET MVC中使用支付宝进行沙箱的详情

在ASP.NET MVC中使用支付宝进行沙箱测试的步骤如下:

  1. 注册支付宝开发者账号,登录后进入“沙箱环境”页面,创建一个沙箱账号,获取沙箱环境的APPID、公钥、私钥等信息。

  2. 在ASP.NET MVC项目中安装AlipaySDK.NET SDK包,可以通过Nuget包管理器安装。

  3. 在ASP.NET MVC项目中创建一个支付控制器,编写支付宝支付的相关代码。具体代码如下:

public class PaymentController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Pay()
    {
        string app_id = "沙箱环境的APPID";
        string private_key = "沙箱环境的私钥";
        string alipay_public_key = "沙箱环境的公钥";
        string subject = "订单标题";
        string body = "订单描述";
        string total_amount = "订单总金额";
        string out_trade_no = "商户订单号";

        IAopClient client = new DefaultAopClient("https://openapi.alipaydev.com/gateway.do", app_id, private_key, "json", "1.0", "RSA2", alipay_public_key, "UTF-8", false);

        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.Body = body;
        model.Subject = subject;
        model.TotalAmount = total_amount;
        model.OutTradeNo = out_trade_no;
        model.ProductCode = "FAST_INSTANT_TRADE_PAY";

        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.SetReturnUrl("http://localhost:xxxx/Payment/ReturnUrl");
        request.SetNotifyUrl("http://localhost:xxxx/Payment/NotifyUrl");
        request.SetBizModel(model);

        AlipayTradePagePayResponse response = client.pageExecute(request);

        return Content(response.Body);
    }

    public ActionResult ReturnUrl()
    {
        return Content("支付成功");
    }

    public ActionResult NotifyUrl()
    {
        return Content("支付成功");
    }
}
  1. 在视图文件中创建一个按钮,点击该按钮将会跳转到支付宝支付页面。具体代码如下:
@{
    ViewBag.Title = "Index";
}

<h2>支付宝支付</h2>

<button onclick="location.href='@Url.Action("Pay", "Payment")'">支付</button>
  1. 在支付宝开发者中心的沙箱环境中,可以查看测试订单的支付情况。

以上就是在ASP.NET MVC中使用支付宝进行沙箱测试的详细步骤

标签: 科技


原文地址: https://cveoy.top/t/topic/hxeI 著作权归作者所有。请勿转载和采集!