基于springboot 支付宝app端支付,可用于uni-app使用
1、添加maven依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
2、创建支付实体类:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("支付宝支付实体类")
@Accessors(chain = true)
@Url("https://openapi.alipay.com/gateway.do")
public class PayEntity implements Serializable {
@JSONField(name = "out_trade_no")
@ApiModelProperty(name = "out_trade_no" , required = true , value = "商户网站唯一订单号")
private String outTradeNo ;
@JSONField(name = "total_amount")
@ApiModelProperty(name = "total_amount" , required = true , value = "订单总金额。单位为元,精确到小数点后两位,取值范围:[0.01,100000000] ")
private String totalAmount ;
@JSONField(name = "subject")
@ApiModelProperty(name = "subject" , required = true , value = "订单标题。注意:不可使用特殊字符,如 /,=,& 等。")
private String subject ;
@JSONField(name = "product_code")
@ApiModelProperty(name = "product_code" , required = true , value = "销售产品码,商家和支付宝签约的产品码。手机网站支付为:QUICK_WAP_WAY")
private String productCode ;
@JSONField(name = "auth_token")
@ApiModelProperty(name = "auth_token" , value = "针对用户授权接口,获取用户相关数据时,用于标识用户授权关系")
private String authToken ;
@JSONField(name = "quit_url")
@ApiModelProperty(name = "quit_url" , value = "用户付款中途退出返回商户网站的地址")
private String quitUrl ;
@JSONField(name = "time_expire")
@ApiModelProperty(name = "time_expire" , value = "绝对超时时间,格式为yyyy-MM-dd HH:mm:ss")
private String timeExpire ;
@JSONField(name = "business_params")
@ApiModelProperty(name = "business_params" , value = "商户传入业务信息,具体值要和支付宝约定,应用于安全,营销等参数直传场景,格式为json格式")
private String businessParams ;
@JSONField(name = "passback_params")
@ApiModelProperty(name = "passback_params" , value = "公用回传参数,如果请求时传递了该参数,则返回给商户时会回传该参数。支付宝只会在同步返回(包括跳转回商户网站)和异步通知时将该参数原样返回。本参数必须进行UrlEncode之后才可以发送给支付宝。")
private String passbackParams ;
@JSONField(name = "merchant_order_no")
@ApiModelProperty(name = "merchant_order_no" , value = "商户原始订单号,最大长度限制32位")
private String merchantOrderNo ;
@JSONField(name = "goods_detail")
@ApiModelProperty(name = "goods_detail" , value = "订单包含的商品列表信息,json格式,其它说明详见商品明细说明")
private GoodsDetail goodsDetail ;
@JSONField(name = "extend_params")
@ApiModelProperty(name = "extend_params" , value = "业务扩展参数")
private ExtendParams extendParams ;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class GoodsDetail implements Serializable{
@JSONField(name = "goods_id")
@ApiModelProperty(name = "goods_id" , required = true , value = "商品的编号")
private String goodsId ;
@JSONField(name = "alipay_goods_id")
@ApiModelProperty(name = "alipay_goods_id" , value = "支付宝定义的统一商品编号")
private String alipayGoodsId ;
@JSONField(name = "goods_name")
@ApiModelProperty(name = "goods_name" , value = "商品名称")
private String goodsName ;
@JSONField(name = "quantity")
@ApiModelProperty(name = "quantity" , value = "商品数量")
private Integer quantity ;
@JSONField(name = "price")
@ApiModelProperty(name = "price" , value = "商品单价,单位为元")
private String price ;
@JSONField(name = "goods_category")
@ApiModelProperty(name = "goods_category" , value = "商品类目")
private String goodsCategory ;
@JSONField(name = "categories_tree")
@ApiModelProperty(name = "categories_tree" , value = "商品类目树,从商品类目根节点到叶子节点的类目id组成,类目id值使用|分割")
private String categoriesTree ;
@JSONField(name = "body")
@ApiModelProperty(name = "body" , value = "商品描述信息")
private String body ;
@JSONField(name = "show_url")
@ApiModelProperty(name = "show_url" , value = "商品的展示地址")
private String showUrl ;
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class ExtendParams implements Serializable{
@JSONField(name = "sys_service_provider_id")
@ApiModelProperty(name = "sys_service_provider_id" , value = "系统商编号该参数作为系统商返佣数据提取的依据,请填写系统商签约协议的PID")
private String sysServiceProviderId ;
@JSONField(name = "hb_fq_num")
@ApiModelProperty(name = "hb_fq_num" , value = "使用花呗分期要进行的分期数")
private String hbFqNum ;
@JSONField(name = "hb_fq_seller_percent")
@ApiModelProperty(name = "hb_fq_seller_percent" , value = "使用花呗分期需要卖家承担的手续费比例的百分值,传入100代表100%")
private String hbFqSellerPercent ;
@JSONField(name = "industry_reflux_info")
@ApiModelProperty(name = "industry_reflux_info" , value = "行业数据回流信息, 详见:地铁支付接口参数补充说明")
private String industryRefluxInfo ;
@JSONField(name = "card_type")
@ApiModelProperty(name = "card_type" , value = "卡类型")
private String cardType ;
@JSONField(name = "specified_seller_name")
@ApiModelProperty(name = "specified_seller_name" , value = "特殊场景下,允许商户指定交易展示的卖家名称")
private String specifiedSellerName ;
}
}
3、支付util类:
@Slf4j
public class PayUtils {
private AlibabaProperties alibabaProperties;
public static PayUtils newInstance(AlibabaProperties alibabaProperties){
PayUtils payUtils = new PayUtils();
payUtils.alibabaProperties = alibabaProperties;
return payUtils ;
}
public String pay(PayEntity payEntity) throws Exception{
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl(payEntity.getClass().getAnnotation(Url.class).value());
alipayConfig.setAppId(alibabaProperties.getAppId());
alipayConfig.setPrivateKey(alibabaProperties.getPrivateKey());
if(ProjectStatus.windows操作系统.getValue().equals(alibabaProperties.getEnvironment())){
try{
alipayConfig.setAppCertPath(ClassLoader.getSystemResource(alibabaProperties.getAppCertPath()).getPath());
alipayConfig.setAlipayPublicCertPath(ClassLoader.getSystemResource(alibabaProperties.getPublicCertPath()).getPath());
alipayConfig.setRootCertPath(ClassLoader.getSystemResource(alibabaProperties.getRootCertPath()).getPath());
}catch (Exception e){
log.error("秘钥位置不正确:{}" , e.getMessage());
throw BaseException.throwExNotLog(e.getMessage());
}
}else{
alipayConfig.setAppCertPath(alibabaProperties.getAppCertPath());
alipayConfig.setAlipayPublicCertPath(alibabaProperties.getPublicCertPath());
alipayConfig.setRootCertPath(alibabaProperties.getRootCertPath());
}
alipayConfig.setFormat(alibabaProperties.getFormat());
alipayConfig.setCharset(alibabaProperties.getCharset());
alipayConfig.setSignType(alibabaProperties.getSignType());
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
request.setNotifyUrl(alibabaProperties.getNotifyUrl());
request.setBizContent(JSONObject.toJSONString(payEntity.setProductCode(alibabaProperties.getProductCode())));
try{
AlipayTradeAppPayResponse response = new DefaultAlipayClient(alipayConfig).sdkExecute(request);
if(response.isSuccess()) return response.getBody();
throw BaseException.throwEx(JSONObject.toJSONString(response));
}catch (Exception e){
log.error("支付宝支付失败:{}" , e.getMessage());
throw BaseException.throwEx(e.getMessage());
}
}
public Boolean sign(Map<String , String> params) throws Exception{
if(ProjectStatus.linux操作系统.getValue().equals(alibabaProperties.getEnvironment())) return AlipaySignature.rsaCertCheckV1(params , alibabaProperties.getPublicCertPath() , alibabaProperties.getCharset() , alibabaProperties.getSignType());
return AlipaySignature.rsaCertCheckV1(params , ClassLoader.getSystemResource(alibabaProperties.getPublicCertPath()).getPath() , alibabaProperties.getCharset() , alibabaProperties.getSignType());
}
}