@@ -14,6 +14,7 @@ import type { ComposedItem } from '../types'
14
14
import {
15
15
ErrorOr ,
16
16
isNotError ,
17
+ whenDefined ,
17
18
whenDefinedAll ,
18
19
whenNotError ,
19
20
whenNotErrorAll ,
@@ -33,14 +34,15 @@ const { POP_SERVER_KEY, REDIS_URL, REDIS_USERNAME, REDIS_PASSWORD } =
33
34
export const AUTH_STRING = Buffer . from ( `${ POP_SERVER_KEY } :` ) . toString ( 'base64' )
34
35
35
36
export type Success = {
36
- payment_key : string // '28GTzdVOZNeImaHMDeQF1319'
37
- result_code : string //'R000'
37
+ payment_key ? : string // '28GTzdVOZNeImaHMDeQF1319'
38
+ result_code ? : string //'R000'
38
39
status : 'success'
39
- message : string //'"Payment Key" has been generated successfully'
40
- payment_key_expiry_time : string //'20200401000000'
40
+ message ? : string //'"Payment Key" has been generated successfully'
41
+ payment_key_expiry_time ? : string //'20200401000000'
41
42
_clubs : {
42
43
// Injected by CLUBS
43
44
order_id : string // 'ORDER-<UUID>'
45
+ gross_amount : number
44
46
}
45
47
}
46
48
@@ -122,24 +124,36 @@ export type PaymentKeyOptions = {
122
124
}
123
125
124
126
export const callNRes = async ( options : ErrorOr < PaymentKeyOptions > ) => {
125
- const paymentKey = await whenNotError ( options , ( opts ) =>
126
- fetch ( 'https://pay3.veritrans.co.jp/pop/v1/payment-key' , {
127
- method : 'POST' ,
128
- headers : {
129
- 'Content-Type' : 'application/json; charset=utf-8' ,
130
- Authorization : `Basic ${ AUTH_STRING } ` ,
131
- } ,
132
- body : JSON . stringify ( opts ) ,
133
- } ) . catch ( ( err ) => new Error ( err ) ) ,
127
+ const isFree = whenNotError ( options , ( opts ) => opts . gross_amount === 0 )
128
+ const paymentKey = await whenNotErrorAll ( [ options , isFree ] , ( [ opts , free ] ) =>
129
+ free
130
+ ? undefined
131
+ : fetch ( 'https://pay3.veritrans.co.jp/pop/v1/payment-key' , {
132
+ method : 'POST' ,
133
+ headers : {
134
+ 'Content-Type' : 'application/json; charset=utf-8' ,
135
+ Authorization : `Basic ${ AUTH_STRING } ` ,
136
+ } ,
137
+ body : JSON . stringify ( opts ) ,
138
+ } ) . catch ( ( err ) => new Error ( err ) ) ,
134
139
)
135
140
136
141
const result = await whenNotErrorAll (
137
142
[ paymentKey , options ] ,
138
143
( [ res , { order_id } ] ) =>
139
- res
140
- . json ( )
141
- . then ( ( x ) => ( { ...x , _clubs : { order_id } } ) as Success )
142
- . catch ( ( err ) => new Error ( err ) ) ,
144
+ whenDefined ( res , ( _res ) =>
145
+ _res
146
+ . json ( )
147
+ . then (
148
+ ( x ) =>
149
+ ( {
150
+ ...x ,
151
+ _clubs : { order_id, gross_amount : x . gross_amount } ,
152
+ } ) as Success ,
153
+ )
154
+ . catch ( ( err ) => new Error ( err ) ) ,
155
+ ) ??
156
+ ( { status : 'success' , _clubs : { order_id, gross_amount : 0 } } as Success ) ,
143
157
)
144
158
145
159
return result instanceof Error
0 commit comments