Skip to content

Commit

Permalink
feat(void): implement void API
Browse files Browse the repository at this point in the history
  • Loading branch information
enylin committed Dec 27, 2021
1 parent 8b439d3 commit 3c4869c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { checkPaymentStatusWithClient } from './line-pay-api/check-payment-statu
import { payPreapprovedWithClient } from './line-pay-api/pay-preapproved'
import { checkRegKeyWithClient } from './line-pay-api/check-regkey'
import { expireRegKeyWithClient } from './line-pay-api/expire-regkey'
import { voidWithClient } from './line-pay-api/void'

/**
* Create a client for LINE Pay API.
Expand All @@ -37,6 +38,7 @@ export function createLinePayClient(config: LineMerchantConfig): LinePayClient {
request: createPaymentApi('request', requestWithClient, httpClient),
confirm: createPaymentApi('confirm', confirmWithClient, httpClient),
capture: createPaymentApi('capture', captureWithClient, httpClient),
void: createPaymentApi('void', voidWithClient, httpClient),
refund: createPaymentApi('refund', refundWithClient, httpClient),
paymentDetails: createPaymentApi(
'paymentDetails',
Expand Down
36 changes: 36 additions & 0 deletions src/line-pay-api/void.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { LinePayApiClients } from '@/payment-api/type'
import { FormatError } from './error/format'
import { EmptyObject, GeneralRequestConfig, GeneralResponseBody } from './type'

export type VoidRequestBody = EmptyObject

export type VoidRequestConfig = GeneralRequestConfig & {
/**
* ID of the transaction
*/
transactionId: string
/**
* Request body of void API
*/
body?: VoidRequestBody
}

export type VoidResponseBody = GeneralResponseBody

export const defaultTimeout = 20000

export const voidWithClient: LinePayApiClients['void'] =
httpClient =>
async ({ transactionId, body = {}, timeout }) => {
if (!transactionId) throw new FormatError('"transactionId" is required')

const { data } = await httpClient.post<VoidRequestBody, VoidResponseBody>(
`/v3/payments/authorizations/${transactionId}/void`,
body,
{
timeout: timeout ?? defaultTimeout
}
)

return data
}
2 changes: 2 additions & 0 deletions src/payment-api/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
ExpireRegKeyRequestConfig,
ExpireRegKeyResponseBody
} from '@/line-pay-api/expire-regkey'
import { VoidRequestConfig, VoidResponseBody } from '@/line-pay-api/void'

/**
* All LINE Pay API Clients supported by this library.
Expand All @@ -44,6 +45,7 @@ export type LinePayApiClients = {
request: ApiClientBuilder<RequestRequestConfig, RequestResponseBody>
confirm: ApiClientBuilder<ConfirmRequestConfig, ConfirmResponseBody>
capture: ApiClientBuilder<CaptureRequestConfig, CaptureResponseBody>
void: ApiClientBuilder<VoidRequestConfig, VoidResponseBody>
refund: ApiClientBuilder<RefundRequestConfig, RefundResponseBody>
paymentDetails: ApiClientBuilder<
PaymentDetailsRequestConfig,
Expand Down
4 changes: 4 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export type LinePayClient = {
* ```
*/
capture: PaymentApi<'capture'>
/**
* An API to void payment data that are in authorization status. The API cancels authorization transaction after the payment is completed with the Confirm API. Only the transactions that are in authorization status(purchase standby status) can be cancelled and purchased transactions should be refunded with [Refund API](https://pay.line.me/documents/online_v3_en.html#refund-api).
*/
void: PaymentApi<'void'>
/**
* An API to refund transactions that has been completed the payment (purchase). The transaction ID of LINE Pay user must be passed when refunded and partial refund is also possible.
*
Expand Down

0 comments on commit 3c4869c

Please sign in to comment.