package com.ghy.web.controller.pay;

import com.ghy.common.adapay.AdapayService;
import com.ghy.common.adapay.callback.PayCallback;
import com.ghy.common.adapay.model.PayParam;
import com.ghy.common.core.controller.BaseController;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.order.domain.OrderMaster;
import com.ghy.order.service.OrderMasterService;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 支付宝支付API
 *
 * @author HH 2022/5/10
 */
@RestController
@RequestMapping("pay/ali")
public class AlipayController extends BaseController {

    @Resource
    private PayCallback payCallback;
    @Resource
    private AdapayService adapayService;
    @Resource
    private OrderMasterService orderMasterService;

    /**
     * 支付宝正扫支付
     */
    @PostMapping("qr")
    public AjaxResult qrPay(String orderMasterCode) {
        OrderMaster orderMaster = orderMasterService.selectByCode(orderMasterCode);
        if (orderMaster == null) {
            return AjaxResult.error("订单不存在");
        }
        Map<String, Object> map;
        try {
            // TODO 订单里需要补充支付金额、tittle、简要描述、分账信息、description
            PayParam payParam = new PayParam(orderMaster.getCode(),
                    "orderMaster.get支付金额", "orderMaster.getTittle()", "orderMaster.get商品描述信息");
            map = adapayService.alipayQrPay(payParam, payCallback, null, null, null);
        } catch (BaseAdaPayException e) {
            logger.error("获取微信用户信息失败", e);
            return AjaxResult.error();
        }
        return AjaxResult.success(map);
    }
}
