您好,登錄后才能下訂單哦!
這篇文章給大家介紹支付寶支付如何接入到SpringBoot項目中,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
支付寶今年推出了新的轉賬接口alipay.fund.trans.uni.transfer(升級后安全性更高,功能更加強大) ,老轉賬接口alipay.fund.trans.toaccount.transfer將不再維護,新老接口的一個區別就是新接口采用的證書驗簽方式。使用新接口要將sdk版本升級到最新版本,博主升級時最新版本是4.10.97。接下來看集成步驟
alipay.properties
alipay.appId=你的應用id alipay.serverUrl=https://openapi.alipay.com/gateway.do alipay.privateKey=你的應用私鑰 alipay.format=json alipay.charset=UTF-8 alipay.signType=RSA2 alipay.appCertPath=/cert/appCertPublicKey_2021001164652941.crt alipay.alipayCertPath=/cert/alipayCertPublicKey_RSA2.crt alipay.alipayRootCertPath=/cert/alipayRootCert.crt
<dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.10.97.ALL</version> </dependency>
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:/production/alipay.properties") @ConfigurationProperties(prefix = "alipay") @Data public class AliPayBean { private String appId; private String privateKey; private String publicKey; private String serverUrl; private String domain; private String format; private String charset; private String signType; private String appCertPath; private String alipayCertPath; private String alipayRootCertPath; }
import com.alipay.api.AlipayClient; import com.alipay.api.CertAlipayRequest; import com.alipay.api.DefaultAlipayClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.FileCopyUtils; import java.io.InputStream; @Configuration public class AliConfig { @Value("${custom.http.proxyHost}") private String proxyHost; @Value("${custom.http.proxyPort}") private int proxyPort; @Value("${spring.profiles.active}") private String activeEnv; @Autowired private AliPayBean aliPayBean; @Bean(name = {"alipayClient"}) public AlipayClient alipayClientService() throws Exception{ CertAlipayRequest certAlipayRequest = new CertAlipayRequest(); //設置網關地址 certAlipayRequest.setServerUrl(aliPayBean.getServerUrl()); //設置應用Id certAlipayRequest.setAppId(aliPayBean.getAppId()); //設置應用私鑰 certAlipayRequest.setPrivateKey(aliPayBean.getPrivateKey()); //設置請求格式,固定值json certAlipayRequest.setFormat(aliPayBean.getFormat()); //設置字符集 certAlipayRequest.setCharset(aliPayBean.getCharset()); //設置簽名類型 certAlipayRequest.setSignType(aliPayBean.getSignType()); //如果是生產環境或者預演環境,則使用代理模式 if ("prod".equals(activeEnv) || "stage".equals(activeEnv) || "test".equals(activeEnv)) { //設置應用公鑰證書路徑 certAlipayRequest.setCertContent(getCertContentByPath(aliPayBean.getAppCertPath())); //設置支付寶公鑰證書路徑 certAlipayRequest.setAlipayPublicCertContent(getCertContentByPath(aliPayBean.getAlipayCertPath())); //設置支付寶根證書路徑 certAlipayRequest.setRootCertContent(getCertContentByPath(aliPayBean.getAlipayRootCertPath())); certAlipayRequest.setProxyHost(proxyHost); certAlipayRequest.setProxyPort(proxyPort); }else { //local String serverPath = this.getClass().getResource("/").getPath(); //設置應用公鑰證書路徑 certAlipayRequest.setCertPath(serverPath+aliPayBean.getAppCertPath()); //設置支付寶公鑰證書路徑 certAlipayRequest.setAlipayPublicCertPath(serverPath+aliPayBean.getAlipayCertPath()); //設置支付寶根證書路徑 certAlipayRequest.setRootCertPath(serverPath+aliPayBean.getAlipayRootCertPath()); } return new DefaultAlipayClient(certAlipayRequest); } public String getCertContentByPath(String name){ InputStream inputStream = null; String content = null; try{ inputStream = this.getClass().getClassLoader().getResourceAsStream(name); content = new String(FileCopyUtils.copyToByteArray(inputStream)); }catch (Exception e){ e.printStackTrace(); } return content; } }
import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.domain.AlipayTradeAppPayModel; import com.alipay.api.domain.AlipayTradeQueryModel; import com.alipay.api.request.AlipayTradeAppPayRequest; import com.alipay.api.request.AlipayTradeQueryRequest; import com.alipay.api.response.AlipayTradeAppPayResponse; import com.alipay.api.response.AlipayTradeQueryResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; /** * @description:支付寶工具類 * @Date:2020-08-26 */ @Slf4j @Service public class AliPayUtils { @Autowired @Qualifier("alipayClient") private AlipayClient alipayClient; /** * 交易查詢接口 * @param request * @return * @throws Exception */ public boolean isTradeQuery(AlipayTradeQueryModel model) throws AlipayApiException { AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); request.setBizModel(model); AlipayTradeQueryResponse alipayTradeQueryResponse = alipayClient.certificateExecute(request); if(alipayTradeQueryResponse.isSuccess()){ return true; } else { return false; } } /** * app支付 * @param model * @param notifyUrl * @return * @throws AlipayApiException */ public String startAppPay(AlipayTradeAppPayModel model, String notifyUrl) throws AlipayApiException { AlipayTradeAppPayRequest aliPayRequest = new AlipayTradeAppPayRequest(); model.setProductCode("QUICK_MSECURITY_PAY"); aliPayRequest.setNotifyUrl(notifyUrl); aliPayRequest.setBizModel(model); // 這里和普通的接口調用不同,使用的是sdkExecute AlipayTradeAppPayResponse aliResponse = alipayClient.sdkExecute(aliPayRequest); return aliResponse.getBody(); } /** * 轉賬接口 * * @param transferParams * @return AlipayFundTransToaccountTransferResponse */ public AlipayFundTransUniTransferResponse doTransferNew(TransferParams transferParams) throws Exception { String title = (StringUtils.isNotBlank(transferParams.getRemark()) ? transferParams .getRemark() : "轉賬"); //轉賬請求入參 AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest(); //轉賬參數 BizContentForUniTransfer bizContent = new BizContentForUniTransfer(); bizContent.setOut_biz_no(transferParams.getOutBizNo()); bizContent.setTrans_amount(MathUtil.changeF2Y(Math.abs(Integer.parseInt(transferParams.getAmount())))); bizContent.setProduct_code("TRANS_ACCOUNT_NO_PWD"); bizContent.setBiz_scene("DIRECT_TRANSFER"); bizContent.setOrder_title(title); Participant participant = new Participant(); participant.setIdentity(transferParams.getPayeeAccount()); participant.setIdentity_type(transferParams.getPayeeType()); participant.setName((StringUtils.isNotBlank(transferParams.getPayeeRealName()) ? transferParams .getPayeeRealName() : StringUtils.EMPTY)); bizContent.setPayee_info(participant); bizContent.setRemark(title); request.setBizContent(JSON.toJSONString(bizContent)); //轉賬請求返回 AlipayFundTransUniTransferResponse response = null; try { response = alipayClient.certificateExecute(request); } catch (Exception e) { log.info("doTransfer exception,異常信息:{}", e.toString()); log.info("doTransfer exception,支付寶返回信息:{}", JSONObject.toJSONString(response)); } log.info("doTransfer,AlipayFundTransUniTransferResponse:{}", JSONObject.toJSONString(response)); return response; } }
@Data public class TransferParams { /** * 應用編號 */ private Long appId; /** * 創建人id */ private Long createdBy; /** * 轉賬業務訂單號 */ private String outBizNo; /** * 收款方識別方式 */ private String payeeType; /** * 收款方賬號,可以是支付寶userId或者支付寶loginId */ private String payeeAccount; /** * 轉賬金額,單位分 */ private String amount; /** * 付款方名稱 */ private String payerShowName; /** * 收款方名稱 */ private String payeeRealName; /** * 備注 */ private String remark; /** * 支付寶轉賬流水號 */ private String orderId; }
import lombok.Data; import java.math.BigDecimal; /** * 支付寶轉賬參數 */ @Data public class BizContentForUniTransfer { /** * 業務訂單號 */ private String out_biz_no; /** * 訂單總金額,單位為元,精確到小數點后兩位, */ private BigDecimal trans_amount; /** * 業務產品碼, * 單筆無密轉賬到支付寶賬戶固定為:TRANS_ACCOUNT_NO_PWD; * 單筆無密轉賬到銀行卡固定為:TRANS_BANKCARD_NO_PWD; * 收發現金紅包固定為:STD_RED_PACKET; */ private String product_code; /** * 描述特定的業務場景,可傳的參數如下: * DIRECT_TRANSFER:單筆無密轉賬到支付寶/銀行卡, B2C現金紅包; * PERSONAL_COLLECTION:C2C現金紅包-領紅包 */ private String biz_scene; /** * 轉賬業務的標題,用于在支付寶用戶的賬單里顯示 */ private String order_title; /** * 原支付寶業務單號。C2C現金紅包-紅包領取時,傳紅包支付時返回的支付寶單號; * B2C現金紅包、單筆無密轉賬到支付寶/銀行卡不需要該參數。 */ private String original_order_id; /** * 業務備注 */ private String remark; /** * 轉賬業務請求的擴展參數,支持傳入的擴展參數如下: * 1、sub_biz_scene 子業務場景,紅包業務必傳,取值REDPACKET,C2C現金紅包、B2C現金紅包均需傳入; * 2、withdraw_timeliness為轉賬到銀行卡的預期到賬時間,可選(不傳入則默認為T1), * 取值T0表示預期T+0到賬,取值T1表示預期T+1到賬,因到賬時效受銀行機構處理影響,支付寶無法保證一定是T0或者T1到賬; */ private String business_params; /** * 支付收款對象 */ private Participant payee_info; }
@Data public class Participant { /** * 參與方的唯一標識 */ private String identity; /** * 參與方的標識類型,目前支持如下類型: * 1、ALIPAY_USER_ID 支付寶的會員ID * 2、ALIPAY_LOGON_ID:支付寶登錄號,支持郵箱和手機號格式 */ private String identity_type; /** * 參與方真實姓名,如果非空,將校驗收款支付寶賬號姓名一致性。 * 當identity_type=ALIPAY_LOGON_ID時,本字段必填。 */ private String name; }
關于支付寶支付如何接入到SpringBoot項目中就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。