支付宝支付(下)

扫单:

定时查询未支付单,进行处理:
  1. 已支付更新状态。
  2. 未支付视情况通知支付。

定时任务实现:

  1. 在入口类添加注解:
   @EnableScheduling   
  1. 在要执行的方法上添加
@Scheduled(cron = "0 * * * * ? ")
// 秒 分 时 日 月 周
  1. * 表示所有值。 例如:在分的字段上设置 *,表示每一分钟都会触发。
  2. ? 表示不指定值。使用的场景为不需要关心当前设置这个字段的值。例如:要在每 月的 10 号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为”?” 具体 设置为 0 0 0 10 * ?
  3. - 表示区间。例如 在小时上设置 “10-12”,表示 10,11,12 点都会触发。
  4. , 表示指定多个值,例如在周字段上设置 “MON,WED,FRI” 表示周一,周三和周五 触发
  5. / 用于递增触发。如在秒上面设置”5/15” 表示从 5 秒开始,每增 15 秒触发 (5,20,35,50)。 在日字段上设置’1/3’所示每月 1 号开始,每隔三天触发一次。
  6. L 表示最后的意思。在日字段设置上,表示当月的最后一天(依据当前月份,如果 是二月还会依据是否是润年[leap]), 在周字段上表示星期六,相当于”7”或”SAT”。如果在”L” 前加上数字,则表示该数据的最后一个。例如在周字段上设置”6L”这样的格式,则表示“本 月最后一个星期五”
  7. W 表示离指定日期的最近那个工作日(周一至周五). 例如在日字段上置”15W”,表示 离每月 15 号最近的那个工作日触发。如果 15 号正好是周六,则找最近的周五(14 号)触 发, 如果 15 号是周未,则找最近的下周一(16 号)触发.如果 15 号正好在工作日(周一至周 五),则就在该天触发。如果指定格式为 “1W”,它则表示每月 1 号往后最近的工作日触发。 如果 1 号正是周六,则将在 3 号下周一触发。(注,”W”前只能设置具体的数字,不允许区 间”-“)。
  8. # 序号(表示每月的第几个周几),例如在周字段上设置”6#3”表示在每月的第三个周 六.注意如果指定”#5”,正好第五周没有周六,则不会触发该配置(用在母亲节和父亲节再 合适不过了) ;小提示:’L’和 ‘W’可以一组合使用。如果在日字段上设置”LW”,则表示在 本月的最后一个工作日触发;周字段的设置,若使用英文字母是不区分大小写的,即 MON 与 mon 相同。

掉单:

查询订单状态文档及示例

  @GetMapping("/query/{id}")
    public Result<Boolean> query(@PathVariable int id) throws AlipayApiException, JsonProcessingException {
        Pay pay = payService.getById(id);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(pay.getUuid());
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        Map result = objectMapper.readValue(response.getBody(), Map.class);
        result = (Map)result.get("alipay_trade_query_response");
        if("TRADE_SUCCESS".equals(result.get("trade_status"))) {
            if(PayStatus.PAID.name().equals(pay.getStatus())) {
                //不做事情
                return Result.ok(true);
            }else {
                pay.setStatus(PayStatus.PAID.name());
                payService.updateById(pay);
                return Result.ok(true);
            }
        }else {
            return Result.ok(false);
        }
        //wqt
    }

退款:

退款api文档及示例

public boolean refund(int orderId, BigDecimal amount) throws AlipayApiException {
    AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
    AlipayTradeRefundModel model = new AlipayTradeRefundModel();
    model.setOutTradeNo(String.valueOf(orderId));
    model.setRefundAmount(amount.toPlainString());
    request.setBizModel(model);
    AlipayTradeRefundResponse response = alipayClient.execute(request);
    return response.isSuccess();
}

退款查询:

退款查询文档及示例

public boolean checkRefund(int id) {
    Pay pay = payService.getById(id);
    AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
    AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
    model.setOutRequestNo(pay.getUuid());
    request.setBizModel(model);
    AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
    .....
}

下载支付单:

查询对账单文档及示例

public String billDownload(String date) throws AlipayApiException {
    AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
    AlipayDataDataserviceBillDownloadurlQueryModel model = new AlipayDataDataserviceBillDownloadurlQueryModel();
    model.setBillType("trade");
    model.setBillDate(date);
    request.setBizModel(model);
    //String content = "{\"bill_type\":\"trade\" , \"bill_date\":\""+date+"\"}";
    //System.out.println(content);
    //request.setBizContent(content);
    AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(request);
    System.out.println(response.getBody());
    String url = response.getBillDownloadUrl();
    return url;
}

功能汇总

定义工具类,完成支付所有功能。(不包含支付后回调)



public class PayUtils {

    public static String pay(String out_trade_no, String price, String name, String returnUrl, String notifyUrl) throws AlipayApiException {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(out_trade_no);
        model.setTotalAmount(price);
        model.setSubject(name);
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);

        request.setReturnUrl(returnUrl);
        request.setNotifyUrl(notifyUrl);
        AlipayTradePagePayResponse response = alipayClient().pageExecute(request);
        return response.getBody();
    }

    public static boolean check(String out_trade_no) throws AlipayApiException {
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(out_trade_no);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient().execute(request);
        return response.isSuccess();
    }

    public static boolean refund(String out_trade_no, BigDecimal amount) throws AlipayApiException {
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setOutTradeNo(out_trade_no);
        model.setRefundAmount(amount.toPlainString());
        request.setBizModel(model);
        AlipayTradeRefundResponse response = alipayClient().execute(request);
        return response.isSuccess();
    }


    public static String checkRefund(String out_trade_no) throws AlipayApiException {

        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
        model.setOutRequestNo(out_trade_no);
        request.setBizModel(model);
        AlipayTradeFastpayRefundQueryResponse response = alipayClient().execute(request);
        return response.getBody();
    }


    public static String billDownload(String date) throws AlipayApiException {
        AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
        AlipayDataDataserviceBillDownloadurlQueryModel model = new AlipayDataDataserviceBillDownloadurlQueryModel();
        model.setBillType("trade");
        model.setBillDate(date);
        request.setBizModel(model);
        //String content = "{\"bill_type\":\"trade\" , \"bill_date\":\""+date+"\"}";
        //System.out.println(content);
        //request.setBizContent(content);
        AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient().execute(request);
        System.out.println(response.getBody());
        String url = response.getBillDownloadUrl();
        return url;
    }





    public static AlipayClient alipayClient() {
        AlipayConfig config = new AlipayConfig();
        //支付宝测试环境服务器
        config.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        //支付宝应用id,在沙箱环境内获取
        config.setAppId("2016092100560986");
        //引用私钥,建议使用《支付宝开放平台开发助手》生成,RSA2
        config.setPrivateKey("MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCLq2XQiqUVyxyO+vT3VIXOhIGWvL1SIs9p28kurzgXzV8JR0X2h+TrSjfyjwYRQH9aV5FFy9NIciA1ce0ZPfxniDt4Bs0pHqLJfkRaUhkotVWn8F6kqqoypy1NfLseDx8GsGXnWVST0wV7xpeOlQM6cflVwYTxx5topyZc5u6NbLssdb/DX1ABjsMsinJQpbgutJFYwy+iWIM7MTSOds4FwZpWdowZ824zfqCMyWibiP1JmguXVcl14eWyOv6LDvDe0niXiF2qMDrTUReVVb+XwEsGb1/cdJT9wU0JDreRH1SSwE6Z+sQdQClfqB/WQJZniveugGf4bXnnZRQfo44fAgMBAAECggEAH7dCHYL+Td6bk5RFQEy/PdA1JSeizh39f4pbOvCrCiymohK/PmZJg2yNG9WCiTReNwOfh3vrdI4F5l0CfDLpOBFlf7H7sJS2Xo7/sormD6pt0v7wXvAqSepQjUH/s6m3X+t6mHhejlri6eKE0+nem8z505FoQQcUsUUTnxEJpq9s1f8SxVF0+6v2va2UqyCbPwKIR0bQLLF7K8a2jl2YBaHRntRi7BCqt4z+IQmHkI6M1zDOysSVbeVX3XlAc54nseorSHAY3ywmjkFSPmBc5MDzNSFgSG2IdHzlTU7R2F4wvExbFGPh64UODzlwcyZFYaTVwUdxhS8JxyXsnHTsQQKBgQDjJs/CMee4AWTN9GNj3XfVW29xjta7XGFJh5BaQRuABpI10nfNyadfwHC7PxjauGqHX2TTpc6991m+kaHOdX5nUyjMOAkhIrmJT1xgPHEjr15Enzp5f8L9NgpMdOJlUne3Uf9cA8pww/lQlEHHcdvoofEYSHoAxMT7wwSut8VdcQKBgQCdaFrfmHtN4AOovtauy3AbdfhhkoyCzN6g/sTDyI4CbLIbMIfqlXTG/RaJYCLVevnmSGCryflDQT69JicSNOVseRZEHli3Qr3G1sS2zU495ZlQGFe6DZomCkw3RxJcX5z25rvvcW3ECDRGpEG17SvMxXWwpD2+0WQL87wEnuwcjwKBgGyak96/SZC6ad3mqNaIftDtxJzAtH4kLwee3y+nzWQqwCEnncwwS+wF8GA2TMXWQmiy/VwL/IrrBmeM7ZXuqx7vraPmbsb++UJjRUFl5JoxMJsSnjyVDz9NZSMlB1F2WnK1q6fs0A+WQ095cvHOyFuzgbggfuR7L8tHdKesiZqhAoGAR4OU3cc6JhxjrTPe95Un/uHvEe1x9y866mw1WznwAvv9Q0seRR7X6lwr9AgAa3sutEgn24SswbiP14HQ+H2dylWNHy+mYMRq0j7bKq5GIOsCZ5hXqwjpAuVk0SxyFBPAjZAwzE19cDXGAl44GH6DisofeTx1bQ9W4/M9dd/6J9MCgYBzW54BbochobX23cFy6QTOQ1zEUGVaabC4rx8/2M9qbORjVWKIcNOLNErm/BmIXkwU/7dPGdkL98poIvYP172obCHdR/vWNEfA9SzTFO2Dkgr7zsJcMM0H/DrUWVx0PFJoQX6uPMnkF71jupqbg0uYtzkVfKVBQ2GA294fWHlvew==");

        //json
        config.setFormat("json");
        //支付宝的公钥,不是咱自己的。沙箱应用处获取
        config.setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtovPlNX2W2/vY7e9FCyb7WTeR61VbgScDzDMpRo866u8ucubHgj9HTt4ebsvwsi5d20l1tqT77mEbEwjCqA8CtfeDyATYuHB+P7SWnotjIIXfFfIAgeAqyIl+7TlsBKt13XAVwQh2LqP2xpzqUCHlhJrvQrrWckzliL/SaqfkHbXRPHqN3O4vNORfOyu5FqXxO5oZ8ADebVkouzi6Rv/wMTazERXIwg2YXYjqkOOQzrojKuHUdHDL9vlC3b0ht1z7cUPLdQE86UoOZCj55dLUvjoZsyxKM/tsGjwvmKJGxvUnDDX88KbGexxFpUS8aN2t0OkE2yzGZgbDtAO3pDTuQIDAQAB");

        //UTF-8
        config.setCharset("UTF-8");
        //RSA2
        config.setSignType("RSA2");
        try {
            return new DefaultAlipayClient(config);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
    }
}