master
2022-02-14 97eb928060da31383c48d5275b2c4775933eeb66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { ALL, App, Body, Inject, Post, Provide } from '@midwayjs/decorator';
import { IMidwayWebApplication } from '@midwayjs/web';
import { Context } from 'egg';
import { CoolController, BaseController } from '@cool-midway/core';
import { ICoolWxPay } from '@cool-midway/wxpay';
import { parseString } from 'xml2js';
import { ICoolAliPay } from '@cool-midway/alipay';
 
/**
 * 支付示例
 */
@Provide()
@CoolController()
export class DemoPayController extends BaseController {
  // 微信支付
  @Inject('wxpay:sdk')
  wxPay: ICoolWxPay;
 
  // 支付宝支付
  @Inject('alipay:sdk')
  aliPay: ICoolAliPay;
 
  @Inject()
  ctx: Context;
 
  @App()
  app: IMidwayWebApplication;
 
  /**
   * 微信扫码支付
   */
  @Post('/wx')
  async wx() {
    const orderNum = await this.wxPay.createOrderNum();
    const data = await this.wxPay.getInstance().unifiedOrder({
      out_trade_no: orderNum,
      body: '测试微信支付',
      total_fee: 1,
      trade_type: 'NATIVE',
      product_id: 'test001',
    });
    return this.ok(data);
  }
 
  /**
   * 微信支付通知回调
   */
  @Post('/wxNotify')
  async wxNotify() {
    let data = '';
    this.ctx.req.setEncoding('utf8');
    this.ctx.req.on('data', chunk => {
      data += chunk;
    });
    const results = await new Promise((resolve, reject) => {
      this.ctx.req.on('end', () => {
        parseString(data, { explicitArray: false }, async (err, json) => {
          if (err) {
            return reject('success');
          }
          const checkSign = await this.wxPay.signVerify(json.xml);
          if (checkSign && json.xml.result_code === 'SUCCESS') {
            // 处理业务逻辑
            console.log('微信支付成功', json.xml);
            return resolve(true);
          }
          return resolve(false);
        });
      });
    });
    if (results) {
      this.ctx.body =
        '<xml><return_msg>OK</return_msg><return_code>SUCCESS</return_code></xml>';
    }
  }
 
  /**
   * 支付宝app支付
   * @returns
   */
  @Post('/alipay')
  async alipay() {
    const orderNum = await this.aliPay.createOrderNum();
    // app支付
    const params = await this.aliPay.getInstance().appPay({
      subject: '测试商品',
      body: '测试商品描述',
      outTradeId: orderNum,
      timeout: '10m',
      amount: '10.00',
      goodsType: '0',
    });
    return this.ok(params);
  }
 
  /**
   * 支付宝支付回调
   */
  @Post('/aliNotify')
  async aliNotify(@Body(ALL) body: any) {
    const { trade_status, out_trade_no } = body;
    const check = await this.aliPay.signVerify(body);
    if (check && trade_status === 'TRADE_SUCCESS') {
      // 处理逻辑
      console.log('支付宝支付成功', out_trade_no);
    }
    this.ctx.body = 'success';
  }
}