package ink.wgink.module.wechat.controller.app.miniapp;

import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.wechat.pojo.pay.v3.PaySign;
import ink.wgink.module.wechat.pojo.pay.v3.jsapi.pojo.PayPlaceOrder;
import ink.wgink.module.wechat.service.pay.v3.jsapi.IJsapiService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.util.OrderUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: MiniappPayAppController
 * @Description: 小程序支付
 * @Author: wanggeng
 * @Date: 2021/8/20 5:48 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "小程序用户")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/miniapp/pay")
public class MiniappPayAppController {

    @Autowired
    private IJsapiService jsapiService;

    @ApiOperation(value = "下订单测试", notes = "下订单测试接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", paramType = "header")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save-place-order-demo")
    public PaySign savePlaceOrderDemo(@RequestHeader("token") String token) throws Exception {
        String atomicOrder = OrderUtil.getAtomicOrder("0001");
        System.out.println("订单号：" + atomicOrder);

        PayPlaceOrder payPlaceOrder = new PayPlaceOrder();
        payPlaceOrder.setDescription("测试商品");
        payPlaceOrder.setOutTradeNo(atomicOrder);
        payPlaceOrder.setNotifyUrl("https://www.wgink.ink/study/wechat/pay/notice");

        PayPlaceOrder.Amount amount = new PayPlaceOrder.Amount();
        amount.setTotal(1);
        payPlaceOrder.setAmount(amount);

        PayPlaceOrder.Payer payer = new PayPlaceOrder.Payer();
        payer.setOpenid("");
        payPlaceOrder.setPayer(payer);

        return jsapiService.placeOrder(payPlaceOrder);
    }

}
