33 generateCheckMacValue ,
44 generateRedirectPostForm ,
55 getEncodedInvoice ,
6+ parseIntegerFileds ,
7+ PostRequest ,
68} from '../utils' ;
79import {
810 ALLPaymentParamsSchema ,
@@ -31,7 +33,9 @@ import {
3133 ALLPaymentParams ,
3234 InvoiceParams ,
3335 ECPayPaymentType ,
36+ PaymentInfoData ,
3437} from '../types' ;
38+ import { PaymentInfoQuery } from './Query' ;
3539
3640export class Payment < T > {
3741 merchant : Merchant ;
@@ -92,6 +96,37 @@ export class Payment<T> {
9296
9397 return generateRedirectPostForm ( this . apiUrl , postOrder ) ;
9498 }
99+
100+ async _placeOrder ( invoice ?: InvoiceParams ) : Promise < PaymentInfoData > {
101+ const { MerchantID, HashKey, HashIV } = this . merchant . config ;
102+ const postParams = {
103+ MerchantID,
104+ ...this . params ,
105+ } ;
106+
107+ const CheckMacValue = generateCheckMacValue ( postParams , HashKey , HashIV ) ;
108+
109+ try {
110+ const _result = await PostRequest < T > ( {
111+ apiUrl : this . apiUrl ,
112+ params : {
113+ ...postParams ,
114+ CheckMacValue,
115+ } ,
116+ responseEncoding : 'utf8' ,
117+ } ) ;
118+
119+ const result = parseIntegerFileds ( _result , [ 'TradeAmt' , 'RtnCode' ] ) ;
120+
121+ return this . merchant
122+ . createQuery ( PaymentInfoQuery , {
123+ MerchantTradeNo : this . baseParams . MerchantTradeNo ,
124+ } )
125+ . read ( ) ;
126+ } catch ( err ) {
127+ throw err ;
128+ }
129+ }
95130}
96131
97132export class CreditOneTimePayment extends Payment < CreditOneTimePaymentParams > {
@@ -170,11 +205,17 @@ export class ATMPayment extends Payment<ATMPaymentParams> {
170205
171206 this . params . ClientRedirectURL =
172207 params . ClientRedirectURL ?? this . merchant . config . ClientRedirectURL ;
208+
173209 this . params . PaymentInfoURL =
174210 params . PaymentInfoURL ?? this . merchant . config . PaymentInfoURL ;
175211
176212 ATMPaymentParamsSchema . validateSync ( this . params ) ;
177213 }
214+
215+ async placeOrder ( invoice ?: InvoiceParams ) : Promise < PaymentInfoData > {
216+ this . params . ChooseSubPayment = this . params . ChooseSubPayment || 'BOT' ;
217+ return this . _placeOrder ( invoice ) ;
218+ }
178219}
179220
180221export class CVSPayment extends Payment < CVSPaymentParams > {
@@ -188,11 +229,17 @@ export class CVSPayment extends Payment<CVSPaymentParams> {
188229
189230 this . params . ClientRedirectURL =
190231 params . ClientRedirectURL ?? this . merchant . config . ClientRedirectURL ;
232+
191233 this . params . PaymentInfoURL =
192234 params . PaymentInfoURL ?? this . merchant . config . PaymentInfoURL ;
193235
194236 CVSPaymentParamsSchema . validateSync ( this . params ) ;
195237 }
238+
239+ async placeOrder ( invoice ?: InvoiceParams ) : Promise < PaymentInfoData > {
240+ this . params . ChooseSubPayment = this . params . ChooseSubPayment || 'CVS' ;
241+ return this . _placeOrder ( invoice ) ;
242+ }
196243}
197244
198245export class BARCODEPayment extends Payment < BARCODEPaymentParams > {
@@ -206,11 +253,17 @@ export class BARCODEPayment extends Payment<BARCODEPaymentParams> {
206253
207254 this . params . ClientRedirectURL =
208255 params . ClientRedirectURL ?? this . merchant . config . ClientRedirectURL ;
256+
209257 this . params . PaymentInfoURL =
210258 params . PaymentInfoURL ?? this . merchant . config . PaymentInfoURL ;
211259
212260 BARCODEPaymentParamsSchema . validateSync ( this . params ) ;
213261 }
262+
263+ async placeOrder ( invoice ?: InvoiceParams ) : Promise < PaymentInfoData > {
264+ this . params . ChooseSubPayment = this . params . ChooseSubPayment || 'BARCODE' ;
265+ return this . _placeOrder ( invoice ) ;
266+ }
214267}
215268
216269export class AndroidPayPayment extends Payment < AndroidPayPaymentParams > {
@@ -241,6 +294,7 @@ export class ALLPayment extends Payment<Partial<ALLPaymentParams>> {
241294
242295 this . params . ClientRedirectURL =
243296 params . ClientRedirectURL ?? this . merchant . config . ClientRedirectURL ;
297+
244298 this . params . PaymentInfoURL =
245299 params . PaymentInfoURL ?? this . merchant . config . PaymentInfoURL ;
246300
0 commit comments