/**
  * 支付失败
  *
  * @param sn 支付编号
  */
 public void failure(String sn) {
   Payment payment = get(sn);
   payment.setPaymentStatus(Payment.PaymentStatus.failure);
   payment.setTradeNo(PaymentContext.getContext().getPayResult().getTradeNo());
   payment = this.paymentDao.save(payment);
   //        PaymentContext.getContext().payFailure(PaymentContext.getContext().getPayment());
 }
 /**
  * 付款成功
  *
  * @param sn 支付编号
  */
 public void success(String sn, String tradeNo) {
   Payment payment = get(sn);
   payment.setPaymentStatus(Payment.PaymentStatus.success);
   payment.setTradeNo(tradeNo);
   this.paymentDao.save(payment);
   // TODO 订单事件触发方式
   //        PaymentContext.getContext().paySuccess(PaymentContext.getContext().getPayment());
 }
  /**
   * 支付准备
   *
   * @param order 订单信息
   * @param payConfig 支付配置
   * @param payProduct 支付产品
   * @param payer 付款人
   * @return Payment
   * @throws PayException
   */
  public Payment ready(Order order, PayConfig payConfig, PayProduct payProduct, String payer)
      throws PayException {
    // 在线支付
    if (PayConfig.PayConfigType.online != payConfig.getPayConfigType()) {
      throw new PayException("暂时只支持在线支付");
    }

    // 支付配置类型(线下支付、在线支付)
    Payment.PaymentType paymentType = Payment.PaymentType.online;
    BigDecimal paymentFee = BigDecimal.ZERO; // 支付手续费

    BigDecimal amountPayable = order.getPayableFee(); // 应付金额(含支付手续费)

    Payment payment =
        this.paymentDao.findUnique(
            Restrictions.eq("payConfig.id", payConfig.getId()),
            Restrictions.eq("orderType", order.getType()),
            Restrictions.eq("orderSn", order.getSN()),
            Restrictions.eq("paymentStatus", Payment.PaymentStatus.ready));
    if (payment != null) {
      // 如果存在未完成的支付信息
      if (amountPayable.compareTo(payment.getTotalAmount().subtract(payment.getPaymentFee()))
          == 0) {
        return payment;
      } else {
        this.invalid(payment.getSn());
      }
    }

    payment = new Payment();

    // 在线支付
    String bankName = payProduct.getName();
    String bankAccount = payConfig.getBargainorId();
    payment.setPaymentType(paymentType);
    payment.setPaymentConfigName(payConfig.getName());
    payment.setBankName(bankName);
    payment.setBankAccount(bankAccount);
    payment.setTotalAmount(amountPayable.add(paymentFee));
    payment.setPaymentFee(paymentFee);
    payment.setPayer(payer);
    payment.setMemo(null);
    payment.setPaymentStatus(Payment.PaymentStatus.ready);
    payment.setPayConfig(payConfig);
    payment.setOrderType(order.getType());
    payment.setOrderSn(order.getSN());
    return this.paymentDao.save(payment);
  }
 public void close(String sn, String tradeNo) {
   Payment payment = get(sn);
   payment.setPaymentStatus(Payment.PaymentStatus.invalid);
   payment.setTradeNo(tradeNo);
   this.paymentDao.save(payment);
 }
 /**
  * 过期支付单
  *
  * @param sn 支付编号
  */
 public void invalid(String sn) {
   Payment payment = get(sn);
   payment.setPaymentStatus(Payment.PaymentStatus.invalid);
   this.paymentDao.save(payment);
 }