39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import type { BsbpCreateRequest, BsbpCreateResponse } from '#shared/bsbp_create'
|
|
import https from 'node:https'
|
|
import axios from 'axios'
|
|
import { defineEventHandler, readBody } from 'h3'
|
|
|
|
export default defineEventHandler(async (event): Promise<BsbpCreateResponse | { error: string }> => {
|
|
const merchantId = process.env.BSPB_MERCHANT_ID!
|
|
const merchantPassword = process.env.BSPB_MERCHANT_PASSWORD!
|
|
const apiUrl = process.env.BSPB_API_URL!
|
|
|
|
const assetsStorage = useStorage('assets:server')
|
|
const bspbKey = await assetsStorage.getItem<string>('pgtest_key.key')
|
|
const bspbCert = await assetsStorage.getItem<string>('pgtest_cer_2025.pem')
|
|
|
|
const agent = new https.Agent({
|
|
key: bspbKey!,
|
|
cert: bspbCert!,
|
|
rejectUnauthorized: false,
|
|
})
|
|
|
|
const orderData = await readBody<BsbpCreateRequest>(event)
|
|
|
|
try {
|
|
const response = await axios.post<BsbpCreateResponse>(`${apiUrl}/order`, orderData, {
|
|
httpsAgent: agent,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Basic ${Buffer.from(`TerminalSys/${merchantId}:${merchantPassword}`).toString('base64')}`,
|
|
},
|
|
})
|
|
|
|
return response.data
|
|
}
|
|
catch (e: any) {
|
|
return { error: e?.message ?? 'Unknown BSPB error' }
|
|
}
|
|
},
|
|
)
|