1,环境 我用的是 vs 创建一个 mvc项目 新建一个文件夹将 SDK 文件放进去
sdk地址
:https://support./docs/doc.htm?spm=a219a.7386797.0.0.k0rwWc&treeId=193&articleId=105201&docType=1
下载后直接解压到箭头指向的AliPaySDK
创建一个控制器:
using Aop.Api;using Aop.Api.Request;using Aop.Api.Response;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using WsyCardInfo.App_Start;namespace WsyCardInfo.Controllers{public class AliPayController : Controller{// GET: AliPay[NoPermissionRequiredAttribute]public ActionResult Index(){return View();}[NoPermissionRequiredAttribute]public ActionResult FaceToPay(){ //域名const string URL = "/gateway.do"; //沙箱环境与正式环境不同 这里要用沙箱的 支付宝地址//APPID即创建应用后生成const string APPID = "091500513966";//开发者应用私钥,由开发者自己生成 开发者私钥到底是什么玩意 原来开发者私钥就是商户应用私钥const string APP_PRIVATE_KEY = "MIIEowIBAAKCAQEArVWg55eFMf7rFLVpFAxGgMQfxGWd7swqxNkOu1Y/0Wt14HZik4+U0kycQuYtOFBbwael8vlr/Q+BaqtpwwI+g+yJVma+uLYkmVaWHqswh1XocX3gOfLWxFp1DwPcNesFpbZ9wndxrZL9+wWMtbiB4TV3jBAcFbAzOgLvH4+IveS88H6jjTTpHeogxJpZ0EU37Nucpsae2yiJJyACmgZDlY3qEABkFpiaD2Kc+EvGDPXyEKSufv8f+GxUTKgE25c0NhP9hSJPND1+reygiJy4sFIEuZ5960LRd/1kJgUIuW+JgA50AXI/SXEzHqBJO0ZvT9hZjSIbOtvGjbQp+d9azwIDAQABAoIBAHQjJn9l13nLKqmibzhejTKjtgE6cNpac+GW6bb9sB7aGI3/5EaocneBm0V7whq0RPZ5JMiq8/8Hz5ewKFnf/BEagF6i0vEiIV2YVVdRLl/PYMICLCXHnrzLOxmHZZm+0fbZUmPk8gbxsTjq2/+6E1PZJjNza83gVEJvStbjriRErp5LwMYCO3Qz3KDaPHFUaP5IRR6b+qJeLbrArFNv3zKV9b3TRe1loSIqrUfgSfW7Qh8rEjTub0BxjFqkOeVUrCPlrNz4NqTjOmPBf1tJ+xLFXsYlom4mGoaXKx9UIQgirLkh++HM+fHHrsICg3CA1tKBjf6OYXqLsvsXCd7T0okCgYEA4Yrrhzgmp9OWt1HS5HkAAlErYQoal6JrmfWd5cZOMwgp5YqmfXoV/QgwC0mbXun01JshgnOsmKy2GiOeUlsCRLhGJCm0piI/o9pSwiUyhe65AFLltyQJXmAVWsCeXjyEDDXSheZ1dL6akjVcs4dKSA+E3+lYs+m3mHBy7cXD8esCgYEAxL3bFNAe3iYqnt0/Ncv1SM7cxYJTZMjtEOiVp3sMvcgQCPPzl5CONkIuNKT5F4o8BCMQU0AzzDHxyxAuaspT4EcXQvCm5UIi5BioYnq0IEVVxuRa+AT6bHLxdyd0wsAzEhbrncUGe2ncQ1SKLuKy7UygCA3DBn++v0aqPrGX3a0CgYBPKtAZmqAW8LJKjrIT+FUEezFa0o0bBKv4Urql2MHCL+9k3xIpoRzuwFz82U1sN3r01a8hdzDyNk2FR1NfXQqizHvyXaFHX2YvQYcjFqNxV6YgcvinHEMdmWRlgo8UTPGcx5ep4IcB/5WKFKkBa0+rk6b0YZf9LuB+5Zmx1GU/GQKBgQCufahp7DAEXRtd7OJcBznTykx6DB2EfMPtRCs+8F6bj23RUvQPz9ChhM7QOCUIYLzb+nFHNOD70KwfqolQg4QinUqfPyr2mFKztL7bIPLS0EvEa9HWuSuVtdg9Esx20do5yARO2GjoWjpVqQwpizygRF6G5hW7lN8LbFQf6IjFvQKBgBdlb1h1rfJcPjkFGyIgr7B36eCrKdlRvNVsnMiqXi+3z5kbYXyePEiIfRYolg7UZI2tAAuHFZmxWwSFccREwKvs1rXSqWs0LqvaR7UizGxDq6AUdtz60jfsnkq6RAqwlA2m5ZW0Kf0QivElWxV1pM71ddU7evUNZTOgwL6qX1oG";//参数返回格式,只支持jsonconst string FORMAT = "json";//请求和签名使用的字符编码格式,支持GBK和UTF-8const string CHARSET = "UTF-8";//支付宝公钥,由支付宝生成到蚂蚁金服复制const string ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApdBe/PUjBBEwYM4/H9clg0yrD1f8KoFusXqgiL4OegnizB2M4lGq7kCkXgwTIxJcOnaLTq2PsE/f0DFNq9GW3xxuLeioh0pjUqG4KV7bFhNy2Wipeja6A+IX1wbhH1AcHYfoGBVjfqEPvqvNWJPCE2ClAZLkNv2pwbC1cKXG48jKX9iVcCmJBl/qgMAcYfgdm37zjcdcgsd/EfHGT8HtiYb08UR+olsxG6Pi7nfc+yLaqGNJyhu6L6lgnQBUxD+fjrFFwPrF6fE0MMm7dljWRO8HiZTTjt8ia4rs8Be05dkd3IDRCoWTcNAd9+tTxV62gcw6rMM92zXMWlzWePk4NQIDAQAB";IAopClient client = new DefaultAopClient(URL, APPID, APP_PRIVATE_KEY, FORMAT, "1.0", "RSA2", ALIPAY_PUBLIC_KEY, CHARSET, false);//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称如:AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();//创建API对应的request类//SDK已经封装掉了公共参数,这里只需要传入业务参数//此次只是参数展示,未进行字符串转义,实际情况下请转义request.BizContent = "{" +" \"out_trade_no\":\"0320010101002\"," +" \"total_amount\":\"88.88\"," +" \"subject\":\"Iphone6 16G\"," +" \"store_id\":\"NJ_001\"," +" \"timeout_express\":\"90m\"}";AlipayTradePrecreateResponse response = client.Execute(request);//调用成功,则处理业务逻辑if (!response.IsError){var res = new{ success = true,out_trade_no = response.OutTradeNo,qr_code =response.QrCode //发现返回的是一个网址,后续需要用这个网址字符串创建二维码};return Json(res);}else {var res = new{success = false,};return Json(res);}}}}
随便写一个页面 Index
@{Layout = null;}<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width" /><style>/** {margin: 0;}.wr-qr {width: 500px;border: 1px solid black;position: absolute;left: 50%;top: 50%;}.wr-qr .qr-order {width: 100%;}.wr-qr .qr-order label {color: black;font-weight: bold;font-size: 12px;}.qr-code-img {}*/</style><script src="~/scripts/jquery-easyui-1.4.1/jquery.min.js"></script><script>$(function () {var img = $('.qr-code-img');var test = $('#test');$('#btn').on('click', function (e) {$.ajax({url: '@Url.Action("FaceToPay", "AliPay")',type: 'post',success: function (e) {if (true) {$('.qr-code-img').attr('src', e.qr_code);$('#test').html(e.out_trade_no);} else {alert('失败');}},error : function (e) {alert('发送失败');}})})})</script><title>支付宝支付Demo</title></head><body><div> <input id="btn" type="button" name="name" value="直接购买充气娃娃" /></div><div class="wr-qr"><div class="qr-order"><label id="test">内容</label></div><div class="qr-order"><img src="#" class="qr-code-img" alt="Alternate Text" /></div></div></body></html>
填坑 : 点击调试 :发现 启动不了
报错内容如下
原因有两个:
1.在添加进来的sdk有如下文件,这个项目的特性与原项目冲突,注释掉即可
2. 在sdk文件中的AopResponse.cs 类 属性中的 Code 和 Msg 会和继承这个类的 属性发生冲突
解决方法:
将 Code 改为 M_Code 将 Msg 改为 M_Msg
然后f5调试就可以顺利启动了
最后要注意 因为是沙盒环境 控制器中的 URl是 如下与正式环境不同
const string URL = "/gateway.do";
点击页面按钮发现 支付宝返回的是一个网址 需要自己 把 这个网址字符串 转为二维码 而且这个二维码只有沙箱账号的买家账号才可以实现扫码
打开页面已经可以获取到订单号 和二维码的网址 :
将网址/bax036906seucrzwkwlu003c 放到草料二维码生成器
有沙箱买家账号扫码:
二维码 等待后续开发