Java实现支付宝支付功能

alipay:
  easy:
    protocol: https
    gatewayHost: openapi-sandbox.dl.alipaydev.com
#    gatewayHost: openapi.alipay.com
    signType: RSA2
    appId: 
    merchantPrivateKey: 
    notifyUrl: 
@Configuration
@Data
@ConfigurationProperties(prefix = "alipay.easy")
public class AliPayProperties {
    private String protocol;
    private String gatewayHost;
    private String signType;
    private String appId;
    private String merchantPrivateKey;
    private String alipayPublicKey;
    private String notifyUrl;
    private String encryptKey;
}
@Configuration
@Data
public class AlipayConfig {

    @Bean
    public Config config(AliPayProperties aliPayProperties) {
        Config config = new Config();
        config.protocol = aliPayProperties.getProtocol();
        config.gatewayHost = aliPayProperties.getGatewayHost();
        config.signType = aliPayProperties.getSignType();
        config.appId = aliPayProperties.getAppId();
        config.merchantPrivateKey = aliPayProperties.getMerchantPrivateKey();
        config.alipayPublicKey = aliPayProperties.getAlipayPublicKey();
        //可设置异步通知接受服务地址
        config.notifyUrl = aliPayProperties.getNotifyUrl();
        config.encryptKey = "";
        return config;


    }
}
@RestController
@Slf4j
@AllArgsConstructor
public class EasyPayController {


    private final Config config;

    //请求预下单给支付宝后台

    @GetMapping("/pay")
    public String pay() throws Exception {
        // 1. 设置参数(全局只需设置一次)
        Factory.setOptions(config);

        //调用支付宝接口
        AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace()
                .preCreate("iphone13 256g", "20150320010101005", "12345");

        // 3. 处理响应或异常
//        if (ResponseChecker.success(response)) {
//            System.out.println("调用成功");
//        } else {
//            System.out.println("调用失败");
//        }
        //解析结果
        /*
        * {
            "alipay_trade_precreate_response": {
            "code": "10000",
            "msg": "Success",
            "out_trade_no": "6823789339978248",
            "qr_code": "https://qr.alipay.com/bavh4wjlxf12tper3a",
            "share_code": "chb4n8A94Qc"
            },
            "sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
        }
        * */
        String body = response.getHttpBody();
        //转json对象
        JSONObject jsonObject = JSONObject.parseObject(body);
        String qrUrl = jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();
        //生成二维码
        QrCodeUtil.generate(qrUrl, 300, 300, new File("E://img//pay.jpg"));

        return body;

    }

    @GetMapping("/notify")
    public String notify(HttpServletRequest request) {
        log.info("收到支付成功通知");
        String out_trade_no = request.getParameter("out_trade_no");
        log.info("流水号{}", out_trade_no);
        //todo 后续业务流程发货
        return "success";

    }

    //主动查询接口
    @GetMapping("/query")
    public String query() throws Exception {
        Factory.setOptions(config);
        AlipayTradeQueryResponse response = Factory.Payment.Common().query("20150320010101001");
        String body = response.getHttpBody();
        JSONObject jsonObject = JSONObject.parseObject(body);
        String msg = (String) jsonObject.getJSONObject("alipay_trade_query_response").get("msg");
        log.info("返回信息:{}",msg);
        return msg;

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值