sealos is a cloud operating system distribution that can manage the life cycle of kubernetes very simply, and use the cloud like win11. Recently, I wrote the payment module of sealos cloud and found that WeChat payment can be done through the command line, which is very interesting. Share how to do it.
Enter a command, the terminal will output a QR code, and you can pay by scanning directly on WeChat. This is really good news for geeks, isn't it delicious for someone who despises the use of GUI? In order to stand at the top of the contempt chain (API despise as code despise CLI despise GUI) how can we not support such a cool feature~
scenes to be used:
- Think about clicking an order when you are a member of iQIYI, swiping to get it done, and your girlfriend can't stay beside her?
- Thinking about the many front-end pages that look like a mountain of shit, I can't find the button for a long time. Isn't it sour and refreshing with a command to recharge?
- Think about you executing a command and suddenly prompting arrears to pop up a QR code for you to recharge and execute
- Think about the database backup log showing a QR code, scan the code to continue the backup
Let's start the tutorial:
Use WeChat native payment method
The native payment will send a request to the WeChat payment service, and the WeChat payment will return a codeurl, directly convert the codeurl into a QR code, and then scan and pay with WeChat.
We write an interface to get code-url on our own server:
ws.Route(ws.GET("/wechat/code-url").To(u.getCodeURL)
func (u Payment) getCodeURL(request *restful.Request, response *restful.Response) {
amount := request.QueryParameter("amount")
user := request.QueryParameter("user")
a, err := strconv.Atoi(amount)
codeURL, err := WechatPay(int64(a), user, "", "", os.Getenv(CallbackURL))
_, err = response.Write([]byte(codeURL))
}
Here to get who (user) recharges (amout) money, (eliminates non-core logic)
Payment request implementation:
func WechatPay(amount int64, user, tradeNO, describe, callback string) (string, error) {
mchID := os.Getenv(MchID) // 商户号
mchCertificateSerialNumber := os.Getenv(MchCertificateSerialNumber) // 商户证书序列号
mchAPIv3Key := os.Getenv(MchAPIv3Key) // 商户APIv3密钥
mchPrivateKey, err := utils.LoadPrivateKey(os.Getenv(WechatPrivateKey))
ctx := context.Background()
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
}
client, err := core.NewClient(ctx, opts...)
svc := native.NativeApiService{Client: client}
resp, _, err := svc.Prepay(ctx,
native.PrepayRequest{
Appid: core.String(os.Getenv(AppID)),
Mchid: core.String(os.Getenv(MchID)),
Description: core.String(describe),
OutTradeNo: core.String(tradeNO),
TimeExpire: core.Time(time.Now()),
Attach: core.String(user),
NotifyUrl: core.String(callback),
GoodsTag: core.String("sealos recharge"),
SupportFapiao: core.Bool(false),
Amount: &native.Amount{
Currency: core.String("CNY"),
Total: core.Int64(amount),
},
Detail: &native.Detail{
CostPrice: core.Int64(608800),
GoodsDetail: []native.GoodsDetail{
{
GoodsName: core.String("sealos cloud recharge"),
MerchantGoodsId: core.String("ABC"),
Quantity: core.Int64(1),
UnitPrice: core.Int64(828800),
WechatpayGoodsId: core.String("1001"),
}},
},
SettleInfo: &native.SettleInfo{
ProfitSharing: core.Bool(false),
},
},
)
return *resp.CodeUrl, nil
}
This interface will return such a string, which we need to convert into a QR code:
weixin://wxpay/bizpayurl?pr=aIQrOYOzz
Command line client implementation
The server-side code is implemented above. Now you can implement the client-side code on the command line. The client-side is nothing more than an http request, and then convert the returned result with the command-line QR code library.
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("Use WeChat to scan the QR code below to recharge, please make sure the username and amount are correct\n")
fmt.Printf("User: %s\n", config.User)
fmt.Printf("Amount: %d\n", config.Amount)
return api.QRTerminalPay(config.User, config.Amount*100, "")
}
accomplish:
func QRTerminalPay(user string, amount int64, domain string) error {
if domain == "" {
domain = "http://localhost:8071"
}
url := fmt.Sprintf("%s/payment/wechat/code-url?amount=%d&user=%s", domain, amount, user)
resp, err := http.Get(url)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
qrterminal.Generate(string(b), qrterminal.L, os.Stdout)
return nil
}
Effect:
Execute the command directly:
sealos recharge --user fanux --amount 1
The terminal will output a QR code
Mobile phone WeChat scan:
You're done~
The complete code is in sealos , you can copy it if you need it~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。