本帖僅供參考,非官方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