SpringBoot接入支付宝支付的方法步骤
程序员文章站
2022-06-23 14:25:43
支付宝今年推出了新的转账接口alipay.fund.trans.uni.transfer(升级后安全性更高,功能更加强大) ,老转账接口alipay.fund.trans.toaccount.tran...
支付宝今年推出了新的转账接口alipay.fund.trans.uni.transfer(升级后安全性更高,功能更加强大) ,老转账接口alipay.fund.trans.toaccount.transfer将不再维护,新老接口的一个区别就是新接口采用的证书验签方式。使用新接口要将sdk版本升级到最新版本,博主升级时最新版本是4.10.97。接下来看集成步骤
1.将支付宝开放平台里下载的3个证书放在resources下面
2.写支付宝支付的配置文件
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
3.引入pom依赖
<dependency> <groupid>com.alipay.sdk</groupid> <artifactid>alipay-sdk-java</artifactid> <version>4.10.97.all</version> </dependency>
4.将配置信息注入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; }
5.写配置类
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; } }
6.写支付工具类
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; } }
tips:转账用到的类
@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接入支付宝支付的方法步骤的文章就介绍到这了,更多相关springboot 支付宝支付内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!