Skip to content

Commit

Permalink
chore(server): fix payment amount
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Mar 15, 2023
1 parent 2cf64ec commit 3d817b2
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
2 changes: 1 addition & 1 deletion deploy/scripts/install-on-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ gpgcheck=0
EOF
# yum update
yum clean all
yum install sealos=4.1.4 -y
yum install sealos -y
fi

ARCH=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/)
Expand Down
6 changes: 4 additions & 2 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ type RegionClusterConf {
}

type RegionDatabaseConf {
driver String // mongodb
connectionUri String
driver String // mongodb
connectionUri String
controlConnectionUri String
}

type RegionGatewayConf {
Expand All @@ -79,6 +80,7 @@ type RegionStorageConf {
internalEndpoint String
accessKey String
secretKey String
controlEndpoint String
}

model Region {
Expand Down
9 changes: 9 additions & 0 deletions server/src/account/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ export class AccountController {
result,
})
}

/**
* WeChat payment notify
*/
@ApiOperation({ summary: 'WeChat payment notify' })
@Post('wechat-notify')
async wechatNotify() {
// todo
}
}
2 changes: 1 addition & 1 deletion server/src/account/dto/create-charge-order.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class CreateChargeOrderDto {
@IsNumber()
amount: number

@ApiProperty()
@ApiProperty({ example: PaymentChannelType.WeChat })
@IsString()
@IsEnum(PaymentChannelType)
paymentChannel: PaymentChannelType
Expand Down
16 changes: 6 additions & 10 deletions server/src/account/payment/wechat-pay.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Injectable } from '@nestjs/common'
import { GenerateOrderNumber, GenerateRandomString } from 'src/utils/random'
import { WeChatPaymentChannelSpec, WeChatPaymentRequestBody } from './types'
import * as crypto from 'crypto'
import { PaymentChannelService } from './payment-channel.service'
import { HttpService } from '@nestjs/axios'
import { ServerConfig } from 'src/constants'

Expand All @@ -11,10 +10,7 @@ export class WeChatPaymentService {
static readonly API_BASE_URL = 'https://api.mch.weixin.qq.com'
static readonly API_NATIVE_PAY_URL = '/v3/pay/transactions/native'

constructor(
private readonly channelService: PaymentChannelService,
private readonly httpService: HttpService,
) {}
constructor(private readonly httpService: HttpService) {}

async pay(
order: WeChatPaymentRequestBody,
Expand All @@ -25,12 +21,12 @@ export class WeChatPaymentService {
const signature = this.createSign(timestamp, nonceStr, order, channelSpec)
const serialNo = channelSpec.certificateSerialNumber

const token = `WECHATPAY2-SHA256-RSA2048 mchid="xxxx",nonce_str="${nonceStr}",timestamp="${timestamp}",signature="${signature}",serial_no="${serialNo}"`
const token = `WECHATPAY2-SHA256-RSA2048 mchid="${channelSpec.mchid}",nonce_str="${nonceStr}",timestamp="${timestamp}",signature="${signature}",serial_no="${serialNo}"`
const headers = {
Authorization: token,
}

const apiUrl = `${WeChatPaymentService.API_BASE_URL}${WeChatPaymentService.API_NATIVE_PAY_URL}}`
const apiUrl = `${WeChatPaymentService.API_BASE_URL}${WeChatPaymentService.API_NATIVE_PAY_URL}`
const res = await this.httpService.axiosRef.post(apiUrl, order, {
headers,
})
Expand All @@ -47,7 +43,7 @@ export class WeChatPaymentService {
out_trade_no: orderNumber,
notify_url: this.getNotifyUrl(),
amount: {
total: amount,
total: amount * 100,
currency: 'CNY',
},
}
Expand All @@ -56,14 +52,14 @@ export class WeChatPaymentService {

createSign(
timestamp: number,
nonce_str: string,
nonceStr: string,
order: WeChatPaymentRequestBody,
channelSpec: WeChatPaymentChannelSpec,
) {
const method = 'POST'
const url = WeChatPaymentService.API_NATIVE_PAY_URL
const orderStr = JSON.stringify(order)
const signStr = `${method}\n${url}\n${timestamp}\n${nonce_str}\n${orderStr}\n`
const signStr = `${method}\n${url}\n${timestamp}\n${nonceStr}\n${orderStr}\n`
const cert = channelSpec.privateKey
const sign = crypto.createSign('RSA-SHA256')
sign.update(signStr)
Expand Down
3 changes: 3 additions & 0 deletions server/src/initializer/initializer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class InitializerService {
set: {
driver: 'mongodb',
connectionUri: ServerConfig.DEFAULT_REGION_DATABASE_URL,
controlConnectionUri: ServerConfig.DEFAULT_REGION_DATABASE_URL,
},
},
storageConf: {
Expand All @@ -46,6 +47,8 @@ export class InitializerService {
ServerConfig.DEFAULT_REGION_MINIO_INTERNAL_ENDPOINT,
accessKey: ServerConfig.DEFAULT_REGION_MINIO_ROOT_ACCESS_KEY,
secretKey: ServerConfig.DEFAULT_REGION_MINIO_ROOT_SECRET_KEY,
controlEndpoint:
ServerConfig.DEFAULT_REGION_MINIO_INTERNAL_ENDPOINT,
},
},
gatewayConf: {
Expand Down
2 changes: 1 addition & 1 deletion server/src/utils/random.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as nanoid from 'nanoid'
import dayjs from 'dayjs'
import * as dayjs from 'dayjs'

export function GenerateAlphaNumericPassword(length: number) {
const nano = nanoid.customAlphabet(
Expand Down

0 comments on commit 3d817b2

Please sign in to comment.