欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot 下支付宝的开箱既用环境

程序员文章站 2024-04-02 10:12:58
sdk-alipay spring boot下支付宝的开箱既用环境 使用场景 spring boot应用中需要接入支付宝 开始使用 pom.xml中引入依赖...

sdk-alipay

spring boot下支付宝的开箱既用环境

使用场景

spring boot应用中需要接入支付宝

开始使用

pom.xml中引入依赖

<dependency>
  <groupid>net.guerlab</groupid>
  <artifactid>sdk-alipay-starter</artifactid>
  <version>1.0.3</version>
</dependency>

bootstrap.yml中增加配置

sdk:
 alipay:
  dev: true/false #默认false,为true表示使用沙箱环境
  sign-type: rsa2 #签名算法
  app-id: #应用id
  private-key: #应用私钥
  alipay-public-key: #支付宝公钥

增加控制器实现

import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.requestmapping;
import com.alibaba.fastjson.jsonobject;
import com.alipay.api.alipayclient;
import com.alipay.api.request.alipaytradeapppayrequest;
import com.alipay.api.request.alipaytradepagepayrequest;
import com.alipay.api.request.alipaytradewappayrequest;
import net.guerlab.sdk.alipay.controller.alipayabstractcontroller;
@requestmapping("/pay/alipay")
public class alipaycontroller extends alipayabstractcontroller {
  @autowired
  private alipayclient client;//支付宝请求sdk客户端
  /**
   * 支付请求
   */
  @getmapping("/app/{orderid}")
  public string app(
      @pathvariable long orderid,
      httpservletresponse httpresponse) {
    jsonobject data = new jsonobject();
    data.put("out_trade_no", "201701010000001234"); //商户订单号
    data.put("product_code", "quick_msecurity_pay"); //产品码, app支付 quick_msecurity_pay, pc支付 fast_instant_trade_pay, 移动h5支付 quick_wap_pay
    data.put("total_amount", "0.01"); //订单金额
    data.put("subject", "测试订单"); //订单标题
    //app支付
    alipaytradeapppayrequest request = new alipaytradeapppayrequest();
    //pc支付
    //alipaytradepagepayrequest request = new alipaytradepagepayrequest();
    //移动h5支付
    //alipaytradewappayrequest request = new alipaytradewappayrequest();
    request.setnotifyurl("http://demo/pay/alipay/notify/1"); //异步通知地址
    request.setbizcontent(data.tojsonstring()); //业务参数
    return client.sdkexecute(request).getbody();
  }
  @postmapping("/notify/{orderid}")
  public string notify(
      @pathvariable long orderid,
      httpservletrequest request) {
    if (!notify0(request.getparametermap())) {
      //这里处理验签失败
    }
    request.getparameter("trade_no");//获取请求参数中的商户订单号
    return "success";
  }
}

总结

以上所述是小编给大家介绍的spring boot 下支付宝的开箱既用环境,希望对大家有所帮助