xiongzhu 2 жил өмнө
parent
commit
a3c81fbd90

+ 6 - 0
src/weixin/weixin.controller.ts

@@ -40,6 +40,12 @@ export class WeixinController {
         return await this.weixinService.pay1(openid)
     }
 
+    @Public()
+    @Get('/pay2')
+    public async pay2(@Query() { openid }) {
+        return await this.weixinService.pay2(openid)
+    }
+
     @Public()
     @Get('/jsapiSign')
     public async jsapiSign(@Query() { url }) {

+ 51 - 0
src/weixin/weixin.service.ts

@@ -224,6 +224,57 @@ export class WeixinService {
         }
     }
 
+    async pay2(openid: string) {
+        try {
+            let reqObj = {
+                appid: this.weixinConfiguration.appId,
+                mch_id: this.weixinConfiguration.mchId,
+                nonce_str: Kits.generateStr(), //生成随机字符串
+                body: 'IJPay 让支付触手可及',
+                attach: 'TNWX 微信系开发脚手架',
+                out_trade_no: Kits.generateStr(),
+                total_fee: 666,
+                spbill_create_ip: '192.168.101.1',
+                notify_url: 'https://chillgpt.raexmeta.com/weixin/notify',
+                trade_type: WX_TRADE_TYPE.JSAPI,
+                sign_type: SIGN_TYPE.SIGN_TYPE_HMACSHA256,
+                openid
+            }
+            let xml = await Kits.generateSignedXml(
+                reqObj,
+                this.weixinConfiguration.mchKey,
+                SIGN_TYPE.SIGN_TYPE_HMACSHA256
+            )
+
+            let data = await HttpKit.getHttpDelegate.httpPost(WX_DOMAIN.CHINA.concat(WX_API_TYPE.UNIFIED_ORDER), xml)
+            const res = await Kits.xml2obj(data)
+            console.log(res)
+
+            const signObj = {
+                appId: this.weixinConfiguration.appId,
+                timeStamp: new Date().getTime().toString(),
+                nonceStr: Kits.generateStr(),
+                package: 'prepay_id=' + res.prepay_id,
+                signType: 'HMAC-SHA256'
+            }
+            const signStr = Object.keys(signObj)
+                .sort()
+                .map((key) => {
+                    return `${key}=${signObj[key]}`
+                })
+                .join()
+            console.log(signStr)
+            const paySign = Kits.hmacsha256(signStr, this.privateKey.toString())
+            console.log(paySign)
+            return {
+                ...signObj,
+                paySign
+            }
+        } catch (error) {
+            console.log(error)
+        }
+    }
+
     async getCert() {
         try {
             let result = await PayKit.exeGet(