Alipay alipay has launched a new transfer interface. The fund. Trans. Uni. Transfer (upgraded security is higher, more powerful), the old transfer interface alipay. The fund. Trans. Toaccount. The transfer will no longer maintain, One difference between the old and new interfaces is the certificate verification method adopted by the new interface.

To use the new interface, upgrade the SDK version to the latest version, which is 4.10.97 when the blogger upgrades.

Next, look at the integration steps.

1. Place the three certificates downloaded from alipay open platform under Resources 2. Write the configuration file of Alipay payment alipay.properties

Alipay. AppId = your application id alipay.serverUrl=https://openapi.alipay.com/gateway.do alipay. The privateKey = you private key of the application of alipay. The 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.crtCopy the code

3. Introduce POM dependencies

< the dependency > < groupId > com. Alipay. The SDK < / groupId > < artifactId > alipay - SDK - Java < / artifactId > < version > 4.10.97. ALL < / version > </dependency>Copy the code

4. Inject the configuration information into AliPayBean

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;
 
}
Copy the code

5. Write configuration classes

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(); / / set the gateway address certAlipayRequest. SetServerUrl (aliPayBean. GetServerUrl ()); / / set application Id certAlipayRequest. SetAppId (aliPayBean. GetAppId ()); / / set up the private key certAlipayRequest. SetPrivateKey (aliPayBean. GetPrivateKey ()); / / set the request format, a fixed value json certAlipayRequest. SetFormat (aliPayBean. GetFormat ()); / / set character set certAlipayRequest setCharset (aliPayBean getCharset ()); / / set the signature type certAlipayRequest. SetSignType (aliPayBean. GetSignType ()); // If it is a production environment or a rehearsal environment, Use the proxy pattern if (" prod ". The equals (activeEnv) | | "stage". The equals (activeEnv) | | "test". The equals (activeEnv)) {/ / set up the public key certificate path certAlipayRequest.setCertContent(getCertContentByPath(aliPayBean.getAppCertPath())); / / set pay treasure to public key certificate path certAlipayRequest setAlipayPublicCertContent (getCertContentByPath (aliPayBean. GetAlipayCertPath ())); / / set pay treasure root certificate path certAlipayRequest setRootCertContent (getCertContentByPath (aliPayBean. GetAlipayRootCertPath ())); certAlipayRequest.setProxyHost(proxyHost); certAlipayRequest.setProxyPort(proxyPort); }else { //local String serverPath = this.getClass().getResource("/").getPath(); / / set up the public key certificate path certAlipayRequest. SetCertPath (serverPath + aliPayBean. GetAppCertPath ()); / / set pay treasure to public key certificate path certAlipayRequest setAlipayPublicCertPath (serverPath + aliPayBean. GetAlipayCertPath ()); / / set pay treasure root certificate path 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; }}Copy the code

6. Write the payment tool class

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 pay * @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); / / and common interface call here is different, use sdkExecute AlipayTradeAppPayResponse aliResponse = alipayClient. SdkExecute (aliPayRequest); return aliResponse.getBody(); } / transfer interface * * * * * @ param transferParams * @ return AlipayFundTransToaccountTransferResponse * / public AlipayFundTransUniTransferResponse doTransferNew(TransferParams transferParams) throws Exception { String title = (StringUtils.isNotBlank(transferParams.getRemark()) ? Transferparams.getremark () : "transfer "); / / transfer request into the ginseng 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)); / / transfer request returns AlipayFundTransUniTransferResponse response = null; try { response = alipayClient.certificateExecute(request); } catch (Exception e) {log.info("doTransfer Exception: {}", e.tostring ()); Log.info ("doTransfer exception, alipay return message: {}", jsonObject.tojsonString (response)); } log.info("doTransfer,AlipayFundTransUniTransferResponse:{}", JSONObject.toJSONString(response)); return response; }}Copy the code

Tips: Classes used for transfers

@data public class TransferParams {/** * private Long TransferParams; /** * private Long createdBy; /** * private String outBizNo; /** * private String payeeType; /** * Private String payeeAccount; /** private String payeeAccount; /** * private String amount; /** * payer name */ private String payerShowName; /** * private String payeeRealName; /** * note */ private String remark; /** * private String orderId; } import lombok.Data;Copy the code
import java.math.BigDecimal; /** * private String out_biz_no; /** private String out_biz_no; /** * Total order amount, in yuan, to two decimal places, */ private BigDecimal trans_amount; /** * Business product code, * Single non-secret transfer to Alipay account fixed :TRANS_ACCOUNT_NO_PWD; * Single non-secret transfer to bank card is fixed :TRANS_BANKCARD_NO_PWD; * Fixed cash red packets :STD_RED_PACKET; */ private String product_code; * DIRECT_TRANSFER: single non-secret transfer to Alipay/bank card, B2C cash red envelope; * PERSONAL_COLLECTION: C2C cash red envelope - Get red envelope */ private String biz_scene; /** * private String order_title; /** * The original alipay business slip number. C2C cash red envelope - the number of alipay returned when the red envelope is received and the red envelope is paid; * B2C cash red packets, single non-secret transfer to Alipay/bank card do not need this parameter. */ private String original_order_id; /** * Service remarks */ private String remark; * 1. In the sub_biz_scene business scenario, the REDPACKET business must be transmitted. The value REDPACKET, C2C cash REDPACKET, and B2C cash REDPACKET must be transmitted. * * T0 = expected T+0 and T1 = expected T+1; since the timeliness of arrival was affected by the handling of banking institutions, alipay could not guarantee T0 or T1; */ private String business_params; /** * Private Participant payEE_info; }Copy the code
@data public class Participant {/** * Private String identity; /** * ID of a participant. Currently, the following types are supported: * 1. ALIPAY_USER_ID Alipay member ID * 2. */ private String identity_type; /** * The real name of the participant. If it is not empty, the consistency of the alipay account name will be verified. * This field is mandatory when identity_type=ALIPAY_LOGON_ID. */ private String name; }Copy the code

Author: Li Xiucai Original link: blog.csdn.net/qq_33556185…