# AlipayHK

## Example

npm
```html html
<div id="checkoutAlipayHKField"></div>
```

```javascript javascript
import { AlipayHK } from '@paykka/card-checkout-ui'

const checkoutAlipayHK = paykkaCheckout.create(AlipayHK, {
  onSubmit: formValidateError => {
    console.log('AlipayHK payment submitted:', formValidateError)
  },
  onSuccess: data => {
    console.log('AlipayHK payment successful:', data)
  },
  onError: error => {
    console.log('AlipayHK payment failed:', error)
  }
})

checkoutAlipayHK.mount('#checkoutAlipayHKField')
```

CDN
```html html
<div id="checkoutAlipayHKField"></div>
```

```javascript javascript
const { AlipayHK } = PayKKaCardCheckoutUI

const checkoutAlipayHK = paykkaCheckout.create(AlipayHK, {
  onSubmit: formValidateError => {
    console.log('AlipayHK payment submitted:', formValidateError)
  },
  onSuccess: data => {
    console.log('AlipayHK payment successful:', data)
  },
  onError: error => {
    console.log('AlipayHK payment failed:', error)
  }
})

checkoutAlipayHK.mount('#checkoutAlipayHKField')
```

## Payment Flow

- **Desktop browser**: after clicking the payment button, a QR code is displayed on the page. The customer scans it with the AlipayHK App to complete the payment.
- **Mobile browser**: after clicking the payment button, the customer is redirected to AlipayHK to complete the payment and then returns to the checkout page.


## Attributes

### AlipayHKProps

Passed when calling the `create` method to create AlipayHK.

| **Name**  | **Description**  | **Type** | **Default**  |
|  --- | --- | --- | --- |
| hidePaymentButton | Whether to hide the payment button. After hiding it, you can trigger payment manually through the component ref's `payment()` method. | `boolean` | `false` |
| onSubmit | Payment submission callback, triggered after clicking the payment button.- `formValidateError` is the error information when form validation fails.
- Return `false` to stop the payment submission.

 | `(formValidateError?: Record<string, FormValidateError[]>) => void | boolean | Promise<void | boolean>` | `undefined` |
| onSuccess | Callback after successful payment.- `PaymentSuccessData` contains the information returned after successful payment, please [refer to](#paymentsuccessdata).
- This callback is also triggered when risk control is pending.

 | `(data: PaymentSuccessData) => void` | `undefined` |
| onError | Callback after payment failure, can resubmit for payment.- `error` is a `PayKKaError` instance, typically containing the following properties:
  - `type`: Error type.
  - `message`: Error message.
  - `code`: Error code, when an API error occurs, it will return a specific error code.

 | `(error: PayKKaError) => void` | `undefined` |
| onTimeout | Callback when payment times out, can resubmit for payment. | `() => void` | `undefined` |
| onExpired | Callback when the checkout counter has expired during payment, cannot pay again. | `() => void` | `undefined` |


### AlipayHKRef

Methods and properties exposed by AlipayHK, can be called by the AlipayHK instance created via the `create` method, e.g.: `checkoutAlipayHK.ref.payment()`.

| **Name**  | **Description** | **Type** |
|  --- | --- | --- |
| payment | Make a payment, enabled when the payment button is **not visible**. | `() => void` |


Below is an explanation of some data types:

### PaymentSuccessData

Payment success information.

```typescript
interface PaymentSuccessData {
  /** URL that can be redirected to after successful payment */
  returnUrl?: string
}
```