package wechat

import (
	"context"

	pb "bsm/full/module/finance/wallet/pb"
)

// 微信JSAPI下单
func JsapiPreOrder(ctx context.Context, in *pb.WxpayJSAPIPreOrderRequest) (reply *pb.WxpayJSAPIPreOrderReply, err error) {
	if err := CheckParam(in.Amount, in.AuthCode, in.UserIdentification, in.OrderNo); err != nil {
		return nil, err
	}

	var (
		attach  = in.AuthCode           // 授权码
		orderNo = in.OrderNo            // 订单唯一码
		desc    = in.Description        // 商品描述
		openid  = in.UserIdentification // 用户微信唯一标识
		amount  = in.Amount             // 订单金额
	)
	wx, _ := NewWechat()
	res, err := wx.GetResponse(attach, orderNo, desc, openid, amount)
	if err != nil {
		return nil, err
	}
	return &pb.WxpayJSAPIPreOrderReply{PrepayId: res.PrepayId}, nil
}
