|
@@ -5,6 +5,7 @@ import { InjectEntityModel } from '@midwayjs/typeorm';
|
|
|
import { PaymentChannelEntity } from '../entity/channel';
|
|
|
import axios from 'axios';
|
|
|
import { CustomerEntity } from '../entity/customer';
|
|
|
+import qs = require('qs');
|
|
|
|
|
|
class TokenManager {
|
|
|
authUrl = '' // 认证URL
|
|
@@ -26,11 +27,17 @@ class TokenManager {
|
|
|
// 登录方法,用于获取访问令牌和刷新令牌
|
|
|
async login() {
|
|
|
try {
|
|
|
- const response = await axios.post(this.authUrl, {
|
|
|
+ const dataString = qs.stringify({
|
|
|
client_id: this.clientId, // 传递客户端ID
|
|
|
client_secret: this.clientSecret, // 传递客户端密钥
|
|
|
grant_type: 'client_credentials' // 假设使用密码授权类型进行初始登录
|
|
|
});
|
|
|
+ const config = {
|
|
|
+ method: 'post',
|
|
|
+ url: this.authUrl,
|
|
|
+ data: dataString
|
|
|
+ };
|
|
|
+ const response = await axios(config);
|
|
|
|
|
|
const data = response.data;
|
|
|
this.accessToken = data.access_token; // 获取访问令牌
|
|
@@ -123,8 +130,8 @@ export class EasyPayAdapter {
|
|
|
* 发送请求到 EasyPay API
|
|
|
*/
|
|
|
async request(method: string, endpoint: string, data?: any) {
|
|
|
- return Promise.resolve(`https://api.easypayx.com${endpoint.replace('/api/open', '')}`);
|
|
|
await this.initConfig();
|
|
|
+ // return Promise.resolve(`https://api.easypayx.com${endpoint.replace('/api/open', '')}`);
|
|
|
let url= this.config.apiUrl;
|
|
|
try {
|
|
|
url = `${url}${endpoint.replace('/api/open', '')}`;
|
|
@@ -139,18 +146,23 @@ export class EasyPayAdapter {
|
|
|
'Authorization': `Bearer ${accessToken}`,
|
|
|
},
|
|
|
});
|
|
|
-
|
|
|
// 检查响应
|
|
|
- if (response.data.code !== 200) {
|
|
|
- console.log(response.data);
|
|
|
- throw new Error(`FusionPay API ${response.data.msg}`);
|
|
|
- }
|
|
|
- return response.data;
|
|
|
+ // if (response.data.code !== 200) {
|
|
|
+ // console.log(response.data);
|
|
|
+ // throw new Error(`FusionPay API ${response.data.msg}`);
|
|
|
+ // }
|
|
|
+ // console.log('response', response.data.data);
|
|
|
+ return response.data.data;
|
|
|
} catch (error) {
|
|
|
// console.log(error.response.data);
|
|
|
if (axios.isAxiosError(error) && error.response) {
|
|
|
- console.log(error.response.data);
|
|
|
- throw new Error(`FusionPay API Network ${error.response.data.msg}`);
|
|
|
+ // console.log(error.response.data);
|
|
|
+ // throw new Error(`FusionPay API Network ${error.response.data.msg}`);
|
|
|
+ this.ctx.status = error.response.status; // 服务器错误
|
|
|
+ // this.ctx.body = error.response.data;
|
|
|
+ // return this.res.status(500).json({});
|
|
|
+ return error.response.data;
|
|
|
+ // throw this.ctx.body;
|
|
|
}
|
|
|
throw error;
|
|
|
}
|