Lucent's Blog

当时明月在 曾照彩云归

人生不相见,动如参与商。

6LCi5Y2O5qKFLOaIkeaDs+S9oOS6hg==


实现个人免签支付宝收款(官方回调通知)-代码开发

开发之前需要先引入依赖

 <dependency>
     <groupId>com.alipay.sdk</groupId>
     <artifactId>alipay-sdk-java</artifactId>
     <version>3.7.26.ALL</version>
 </dependency>

代码

我使用的是SpringBoot+SpringMVC+Mybatis-Plus开发

Controller
@RestController
@RequestMapping("pay")
public class PayController {

    @Autowired
    AliPayService aliPayService;

    /**
     * 生成二维码接口
     * @param payLog
     * @return 二维码链接
     */
    @PostMapping("alipay/qr")
    public Map<String, Object> makeAliPayQrCode(@RequestBody PayLog payLog){
        return aliPayService.getQrCode(payLog);
    }

    /**
     * 查询支付状态接口
     * 支付成功返回true
     * @param orderId
     * @return 
     */
    @GetMapping("alipay/status")
    public Boolean queryAliPayStatus(String orderId){
        return aliPayService.queryPaymentStatus(orderId);
    }

    /**
     * 支付宝支付成功回调接口
     * @param out_trade_no
     * @param trade_status
     */
    @RequestMapping("alipay/notify")
    public void notify(@RequestParam(required = false)String out_trade_no,@RequestParam(required = false)String trade_status){
        aliPayService.callBack(out_trade_no,trade_status);
    }
}
Service
/**
 * @author Lucent
 */
@Service
public class AliPayService {

    /**
     * 私钥
     */
    private static final String PRIVATE_KEY = "你生成的支付宝私钥";

    /**
     * 公钥
     */
    private static final String PUBLIC_KEY = "你生成的支付宝公钥";

    /**
     * 应用ID
     */
    private static final String APP_ID = "APPID";

    /**
     * 回调通知接口链接
     */
    private static final String NOTIFY_URL = "http://url/alipay/notify";

    /**
     * 支付宝api
     */
    private static final String ALI_PAY_API = "https://openapi.alipay.com/gateway.do";

    /**
     * 支付宝支付成功状态
     */
    private static final String ALI_PAY_SUCCESS = "TRADE_SUCCESS";

    @Autowired
    IPayLogService payLogService;

    /**
     * 生成二维码链接
     * @param payLog
     * @return
     */
    public Map<String, Object> getQrCode(PayLog payLog){
        payLog.setCreateTime(DateUtils.getTimeNow());
        payLog.setId(DateUtils.getSystemNumber()+ RandomCodeUtil.getRandomString(5));
        Boolean i = payLogService.save(payLog);
        if (i){
            AlipayClient alipayClient = new DefaultAlipayClient(ALI_PAY_API, APP_ID,PRIVATE_KEY,"json","GBK",PUBLIC_KEY,"RSA2");
            AlipayTradePrecreateRequest r = new AlipayTradePrecreateRequest();
            //设置请求参数
            AliPayRequestEntity aliPayRequestEntity=new AliPayRequestEntity();
            aliPayRequestEntity.setOut_trade_no(payLog.getId());
            aliPayRequestEntity.setTotal_amount(payLog.getMoney().toString());
            aliPayRequestEntity.setSubject("Depth游戏加速器");
            r.setBizContent(JsonUtil.objToJson(aliPayRequestEntity));
            // 设置通知回调链接
            r.setNotifyUrl(NOTIFY_URL);
            AlipayTradePrecreateResponse response=null;
            try {
                response = alipayClient.execute(r);
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
            if (!response.isSuccess()){
                throw new CustomException(0,"0","调用支付宝接口失败,请反馈!");
            }
            Map<String, Object> result = new HashMap<>(16);
            result.put("id", payLog.getId());
            result.put("qrCode", response.getQrCode());
            return result;
        }
        return null;
    }

    /**
     * 查询支付宝订单状态
     * 支付成功返回true
     * @param orderId
     * @return
     */
    public Boolean queryPaymentStatus(String orderId){
        AlipayClient alipayClient = new DefaultAlipayClient(ALI_PAY_API, APP_ID,PRIVATE_KEY,"json","GBK",PUBLIC_KEY,"RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AliPayRequestEntity aliPayRequestEntity=new AliPayRequestEntity();
        aliPayRequestEntity.setOut_trade_no(orderId);
        request.setBizContent(JsonUtil.objToJson(aliPayRequestEntity));
        AlipayTradeQueryResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response!=null&&response.isSuccess()&&ALI_PAY_SUCCESS.equals(response.getTradeStatus())){
           return true;
        }
        return false;
    }

    /**
     * 支付宝支付成功回调
     * @param out_trade_no
     * @param trade_status
     */
    public void callBack(String out_trade_no,String trade_status){
        if (ALI_PAY_SUCCESS.equals(trade_status)){
            QueryWrapper<PayLog> wrapper=new QueryWrapper<>();
            wrapper.eq("id",out_trade_no);
            PayLog payLog=payLogService.getOne(wrapper);
            if (null!=payLog&&payLog.getState()==1){
                return;
            }
            payLog.setState(1);
            payLogService.updateById(payLog);
        }
    }

    @Data
    private class AliPayRequestEntity {
        private String out_trade_no;
        private String total_amount;
        private String subject;
    }
}
Entity
/**
 * 订单
 * @author Lucent
 * @since 2020-05-15
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("payLog")
public class PayLog implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id")
    private String id;

    private String createTime;

    private BigDecimal money;

}

以上这些代码即可实现免签支付收款

测试

生成二维码

链接即为二维码,在前端渲染出来给用户扫码即可 渲染完成 扫码效果 支付完成 查询支付状态 返回true即为已支付 支付完成之后支付宝官方也会对你填写的回调地址进行一次通知,通知你的服务器收款到账

如果想要保证正常接收支付宝的通知,服务器一定要有公网ip,否则只能主动发起查询

上一篇

神童诗【作者】汪洙(宋)天子重英豪,文章教尔曹;万般皆下品,惟有读书高。少小须勤学,文章可立身;满朝朱紫贵,尽是读书人。学问勤中得,萤窗万卷书;三冬今足用,谁笑腹空虚。自小多才学,平生志气高;别人怀宝剑,我有笔如刀。朝为田舍郎,暮登天子堂;将相本无种,男儿当自强。学乃身之宝,儒为席上珍;君看为宰相,…

阅读
下一篇

在开发之前需要先开通支付宝当面付,个人可以直接在支付宝APP申请注册商家打开支付宝APP,搜索蚂蚁金服商家平台,关注并进入点击右上角开通支付点击立即签约填写信息提交即可,门头照可以百度图片或者美团开发准备获取支付宝公钥和私钥签约成功之后登陆蚂蚁金服开放平台https://open.alipay.co…

阅读