本帖仅供参考,非官方demo
一、文档地址
文档地址:点击【查看】
sdk下载:点击【查看】
官方demo下载地址:点击【查看】
二、开发前准备工作
1、签约:[url]https://openclub.alipay.com/read.php?tid=276&fid=72[/url]
2、创建应用:[url]https://openclub.alipay.com/read.php?tid=1606&fid=72[/url]
3、生成密钥:[url]https://openclub.alipay.com/read.php?tid=1833&fid=69[/url]
4、下载服务端sdk:[url]https://docs.open.alipay.com/54/106370/[/url]
如何签约以及签约无法成功等相关签约问题:[url]https://openclub.alipay.com/read.php?tid=276&fid=72[/url]
检查调用appid的应用功能列表中是否添加“电脑网站支付”并显示已签约。
三、接口集成示例代码
1、参数说明
(1)out_trade_no:商户订单号,由商户自定义设置;
(2)subject:商品标题;
(3)product_code:固定产品码值:FAST_INSTANT_TRADE_PAY;
(4)total_amount:交易订单金额,精确到小数点后2位,最小设置为0.01;
2、JAVA版请求示例
(适用于jdk1.5以上)
//获得初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //设置请求参数 AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); //同步通知地址:支付成功后页面自动跳转到传值地址中 alipayRequest.setReturnUrl("https://docs.open.alipay.com/200"); //异步通知地址:支付成功生成之后会post返回订单信息详情到该地址,用于商家做业务逻辑处理 alipayRequest.setNotifyUrl("https://docs.open.alipay.com/200"); AlipayTradePagePayModel model = new AlipayTradePagePayModel(); model.setSubject("电脑网站支付测试"); model.setTotalAmount("0.01"); model.setProductCode("FAST_INSTANT_TRADE_PAY"); //自定义参数请转码后传入该参数内,可以传值逗号,等号之类,但不能传引号,否则会报错,其他特殊符号是否报错以实测为准 String passback_params = "{ab=测试一下;tdst=公共参数;ccsd=gds;dfa=23·12}"; String passback_params2 =URLEncoder.encode(passback_params,"UTF-8"); model.setPassbackParams(passback_params2); alipayRequest.setBizModel(model); /** * BizContent传值模式 alipayRequest.setBizContent("{\"out_trade_no\":\"20150320010101001\"," + "\"total_amount\":\"88.88\"," + "\"subject\":\"subject\"," + "\"qr_pay_mode\":\"4\"," + "\"timeout_express\":\"1m\"," + "\"passback_params2\":\"passback_params2\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); **/ //get请求方式,返回http链接 AlipayTradePagePayResponse respose = alipayClient.pageExecute(alipayRequest,"get"); //post请求方式,返回from表单 //AlipayTradePagePayResponse respose = alipayClient.pageExecute(alipayRequest); //打印请求信息 System.out.println(respose.getBody());
3、PHP版请求示例
(适用于ph5.5以上环境)
<?php require_once 'AopSdk.php'; $aop = new AopClient (); $aop->appId = '填写您的appid'; $aop->rsaPrivateKey = '填写您的私钥'; $aop->alipayrsaPublicKey='填写您的支付宝公钥'; $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do'; $aop->apiVersion = '1.0'; $aop->postCharset='utf-8'; $aop->format='json'; $aop->signType = 'RSA2'; //生成随机订单号 $date=date("YmdHis"); $arr=range(1000,9999); shuffle($arr); $request = new AlipayTradePagePayRequest(); //收款方账户类型为:ALIPAY_LOGONID:支付宝登录号,支持邮箱和手机号格式。 $request->setBizContent("{" . ""out_trade_no":"".$date.$arr[0].""," . ""total_amount":"0.1"," . ""subject":"电脑网站支付测试"," . ""product_code":"FAST_INSTANT_TRADE_PAY"" . "}"); //get请求,返回链接 $result = $aop->pageExecute($request,"get"); //post请求,返回表单 //$result = $aop->pageExecute($request); print_r(htmlspecialchars($result)); ?>
4、.NET版请求示例
注:适用于Visual studio 2010及以上版本、Framework3.5 SP1及以上版本的开发环境
沙箱测试代码示例:
using Aop.Api; using Aop.Api.Request; using Aop.Api.Response; using System; using System.IO; /** * 沙箱电脑网站支付 * 文档地址:[url]https://docs.open.alipay.com/270/alipay.trade.page.pay[/url] * */ namespace AlipayTradePagePay { class Program { //沙箱支付宝公钥,查看地址:[url]https://openhome.alipay.com/platform/appDaily.htm[/url] public static string alipay_public_key = @"支付宝公钥"; //沙箱pkcs1私钥,千万不要用pkcs8格式的 ,如何获取,请查看:[url]https://openclub.alipay.com/read.php?tid=1833&fid=25[/url] public static string merchant_private_key = @"私钥"; static void Main(string[] args) { //初始化 IAopClient alipayClient = new DefaultAopClient("https://openapi.alipaydev.com/gateway.do", "appid", merchant_private_key, "json", "1.0", "RSA2", alipay_public_key, "utf-8", false); AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); //请求参数设置 request.BizContent = "{"out_trade_no":"201810201450FDSa","subject":"沙箱测试","product_code":"FAST_INSTANT_TRADE_PAY","total_amount":"0.01"}"; //同步跳转地址,用于订单支付成功之后同步跳转 request.SetReturnUrl("https://docs.open.alipay.com/200"); //异步跳转地址,用于接收异步返回结果 request.SetNotifyUrl("https://docs.open.alipay.com/200"); //默认是post请求方式,返回form表单 AlipayTradePagePayResponse response = alipayClient.pageExecute(request); //get请求方式,返回请求链接 //AlipayTradePagePayResponse response = client.pageExecute(alipayRequest, "", "GET"); //打印返回结果 Console.WriteLine(response.Body); } // 私钥文件类型转换成纯文本类型 返回过滤后的字符串类型私钥 public static string getMerchantPriveteKeyStr() { StreamReader sr = new StreamReader(merchant_private_key); string pubkey = sr.ReadToEnd(); sr.Close(); if (pubkey != null) { pubkey = pubkey.Replace("-----BEGIN PUBLIC KEY-----", ""); pubkey = pubkey.Replace("-----END PUBLIC KEY-----", ""); pubkey = pubkey.Replace("r", ""); pubkey = pubkey.Replace("n", ""); } return pubkey; } } }
post请求返回结果:
<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipaydev.com/gateway.do?charset=utf-8' method='get' style='display:none;'><input name='app_id' value='2016101800718925'/><input name='biz_content' value='{"out_trade_no":"201810201450FDSa","subject":"沙箱测试","product_code":"FAST_INSTANT_TRADE_PAY","total_amount":"0.01"}'/><input name='charset' value='utf-8'/><input name='format' value='json'/><input name='method' value='alipay.trade.page.pay'/><input name='notify_url' value='https://docs.open.alipay.com/200'/><input name='return_url' value='https://docs.open.alipay.com/200'/><input name='sign_type' value='RSA2'/><input name='timestamp' value='2018-05-10 10:22:40'/><input name='version' value='1.0'/><input name='sign' value='iQ0YEXzDlVDMtQoLZg6plCvZnknTrM7/UcbsFPOxF4DiDsZTcqDxkGHngxrUHq4fqlJCiVT6c5PanCpHDhJ9qb2xZenw/hWaakwQ4AR1xyl7sC21C37fB6Z68lE6CVqqsIzsOok0An4UsbJ8iPrQnBy7HZjF/DqU5CT/hUy/0mczcyCcS7GhzIZGLfeWKULEpDnghJpYLcIDZQ7UNxxj/gRY4+6QnE9EiuuyEDMba4X47y/KUqPBaXeCL72VKfbx9hif299BBBv6y88cbWh/zbr/wqB568AYYhaXX5P74/0O6alnVFiq5MIMR6n4d34IKe1hAAb+jNN1+dz3jssxPA=='/><input type='submit' value='get' style='display:none