wangzhibo
4 天以前 c19f1f7e21c93d1f13b4f44a8a615f65af577559
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
import {
  CoolController,
  BaseController,
  CoolUrlTag,
  CoolTag,
  TagTypes,
} from '@cool-midway/core';
import { Body, Inject, Post } from '@midwayjs/core';
import { PluginService } from '../../../plugin/service/info';
import { OrderPayService } from '../../service/pay';
 
/**
 * 支付
 */
@CoolUrlTag()
@CoolController()
export class OrderPayController extends BaseController {
  @Inject()
  pluginService: PluginService;
 
  @Inject()
  orderPayService: OrderPayService;
 
  @Inject()
  ctx;
 
  @CoolTag(TagTypes.IGNORE_TOKEN)
  @Post('/wxNotify', { summary: '微信支付回调' })
  async wxNotify(@Body() body) {
    // 获得插件实例
    const plugin: any = await this.pluginService.getInstance('pay-wx');
    // 获得微信支付 SDK 实例
    const instance = await plugin.getInstance();
    const { ciphertext, associated_data, nonce } = body.resource;
    const data: any = instance.decipher_gcm(ciphertext, associated_data, nonce);
    const check = await plugin.signVerify(this.ctx);
    // 验签通过,处理业务逻辑
    if (check && data.trade_state == 'SUCCESS') {
      return await this.orderPayService.paySuccess(data.out_trade_no, 1);
    }
    return 'fail';
  }
 
  @Post('/wxMiniPay', { summary: '微信小程序支付' })
  async wxMiniPay(@Body('orderId') orderId) {
    return this.ok(
      await this.orderPayService.wxMiniPay(orderId, this.ctx.user.id)
    );
  }
 
  @Post('/wxMpPay', { summary: '微信公众号支付' })
  async wxMpPay(@Body('orderId') orderId) {
    return this.ok(
      await this.orderPayService.wxMpPay(orderId, this.ctx.user.id)
    );
  }
 
  @Post('/wxAppPay', { summary: '微信APP支付' })
  async wxAppPay(@Body('orderId') orderId) {
    return this.ok(
      await this.orderPayService.wxAppPay(orderId, this.ctx.user.id)
    );
  }
 
  @Post('/carbonPay', { summary: '碳积分支付' })
  async carbonPay(@Body('orderId') orderId) {
    return this.ok(
      await this.orderPayService.carbonPay(orderId, this.ctx.user.id)
    );
  }
}