Skip to content

Commit 103c5db

Browse files
committed
tests: add cases for error handling
1 parent df370a3 commit 103c5db

6 files changed

Lines changed: 342 additions & 13 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,31 @@ Once called, Flexpay will send a payment request to the user's mobile money acco
6161
after that the payment will be processed and the callback url will be called with the transaction details.
6262

6363
```typescript
64-
const response = flexpay.pay(mobile);
64+
const response = await flexpay.pay(mobile);
6565
```
6666

6767
### Visa Card Payment
6868
You can set up card payment via VPOS features, which is typically used for online payments.
6969
it's a gateway that allows you to accept payments from your customers using their credit cards.
7070

7171
```typescript
72-
const response = flexpay.pay(card);
72+
const response = await flexpay.pay(card);
7373
// redirect to response.url to complete the payment
7474
```
7575

7676
#### **handling callback (callbackUrl, approveUrl, cancelUrl, declineUrl)**
7777
Flexpay will send a POST request to the defined callbackUrl and the response will contain the transaction details.
7878
you can use the following code to handle the callback by providing incoming data as array.
7979

80-
```php
80+
```typescript
8181
const webhook = flexpay.handleCallback(req.body);
8282
flexpay.isSuccessful(webhook) // true or false
8383
````
8484

8585
### Check Transaction state
8686
You don't trust webhook ? you can always check the transaction state by providing the order number.
8787

88-
```php
88+
```typescript
8989
const tx = flexpay.check(mobile.orderNumber);
9090
flexpay.isSuccessful(tx) // true or false
9191
```

src/client.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type {z} from "zod";
1+
import type { z } from "zod";
22

3-
import {Environment, EnvironmentType} from "@/environment";
4-
import {AccountException, ClientException, NetworkException} from "@/exception";
3+
import { Environment, EnvironmentType } from "@/environment";
4+
import { AccountException, ClientException, NetworkException } from "@/exception";
55
import {
66
CardRequest,
77
CardRequestSchema,
@@ -20,21 +20,23 @@ import {
2020
PayoutResponse,
2121
PayoutResponseSchema,
2222
Status,
23+
Type,
2324
} from "@/schemas";
2425

2526
export class Client {
2627
private readonly credential: Credential;
2728
private readonly env: Environment;
2829

2930
constructor(merchant: string, token: string, env: EnvironmentType = "dev") {
30-
this.credential = CredentialSchema.parse({merchant, token});
31+
this.credential = CredentialSchema.parse({ merchant, token });
3132
this.env = new Environment(env);
3233
}
3334

3435
async mobile(request: MobileRequest): Promise<MobileResponse> {
3536
const body = {
3637
...MobileRequestSchema.parse(request),
3738
merchant: this.credential.merchant,
39+
type: Type.MOBILE,
3840
};
3941
const data = await this.requestJson("POST", this.env.getMobilePaymentUrl(), body);
4042

@@ -68,6 +70,7 @@ export class Client {
6870
const body = {
6971
...PayoutRequestSchema.parse(request),
7072
merchant: this.credential.merchant,
73+
type: Type.MOBILE,
7174
};
7275
const data = await this.requestJson("POST", this.env.getPayoutUrl(), body);
7376

src/schemas.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ const BaseRequestSchema = z.object({
9393

9494
export const MobileRequestSchema = BaseRequestSchema.extend({
9595
phone: z.string().length(12, "The phone number should be 12 characters long, eg: 243123456789"),
96-
type: z.nativeEnum(Type).optional().default(Type.MOBILE),
9796
});
9897

9998
export const CardRequestSchema = z.object({
@@ -114,7 +113,6 @@ export const PayoutRequestSchema = z.object({
114113
currency: CurrencySchema,
115114
callbackUrl: Url,
116115
phone: z.string().length(12, "The phone number should be 12 characters long, eg: 243123456789"),
117-
type: z.nativeEnum(Type).optional().default(Type.MOBILE),
118116
});
119117

120118
export type MobileResponse = z.infer<typeof MobileResponseSchema>;

0 commit comments

Comments
 (0)