@waffo/waffo-node 2.0.4 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -28
- package/dist/index.d.mts +283 -166
- package/dist/index.d.ts +283 -166
- package/dist/index.js +260 -342
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +232 -321
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Waffo Node.js SDK
|
|
2
2
|
|
|
3
|
-
<!-- Synced with waffo-sdk/README.md @ commit
|
|
3
|
+
<!-- Synced with waffo-sdk/README.md @ commit 48b44fc -->
|
|
4
4
|
|
|
5
5
|
**English** | [中文](README_CN.md)
|
|
6
6
|
|
|
@@ -46,11 +46,11 @@ Official Node.js/TypeScript SDK for [Waffo Payment Platform](https://www.waffo.c
|
|
|
46
46
|
- [Custom HTTP Transport](#custom-http-transport-axios)
|
|
47
47
|
- [TLS Security Configuration](#tls-security-configuration)
|
|
48
48
|
- [Debug Logging](#debug-logging)
|
|
49
|
+
- [Handling New API Fields (ExtraParams)](#handling-new-api-fields-extraparams)
|
|
49
50
|
- [Error Handling](#error-handling)
|
|
50
|
-
- [TypeScript Support](#typescript-support)
|
|
51
|
-
- [Development & Testing](#development--testing)
|
|
52
51
|
- [Support](#support)
|
|
53
52
|
- [License](#license)
|
|
53
|
+
- [Development & Testing](#development-testing)
|
|
54
54
|
|
|
55
55
|
## Requirements
|
|
56
56
|
|
|
@@ -229,7 +229,7 @@ app.post('/webhook', express.raw({ type: 'application/json' }), async (req, res)
|
|
|
229
229
|
|
|
230
230
|
const webhookHandler = waffo.webhook()
|
|
231
231
|
.onPayment((notification) => {
|
|
232
|
-
console.log('Payment received:', notification.acquiringOrderId);
|
|
232
|
+
console.log('Payment received:', notification.result?.acquiringOrderId);
|
|
233
233
|
});
|
|
234
234
|
|
|
235
235
|
const result = await webhookHandler.handleWebhook(body, signature);
|
|
@@ -283,7 +283,7 @@ export class PaymentController {
|
|
|
283
283
|
) {
|
|
284
284
|
const webhookHandler = this.waffo.webhook()
|
|
285
285
|
.onPayment((notification) => {
|
|
286
|
-
console.log('Payment received:', notification.acquiringOrderId);
|
|
286
|
+
console.log('Payment received:', notification.result?.acquiringOrderId);
|
|
287
287
|
});
|
|
288
288
|
|
|
289
289
|
const result = await webhookHandler.handleWebhook(body, signature);
|
|
@@ -318,7 +318,7 @@ fastify.post('/webhook', async (request, reply) => {
|
|
|
318
318
|
|
|
319
319
|
const webhookHandler = waffo.webhook()
|
|
320
320
|
.onPayment((notification) => {
|
|
321
|
-
console.log('Payment received:', notification.acquiringOrderId);
|
|
321
|
+
console.log('Payment received:', notification.result?.acquiringOrderId);
|
|
322
322
|
});
|
|
323
323
|
|
|
324
324
|
const result = await webhookHandler.handleWebhook(body, signature);
|
|
@@ -688,28 +688,28 @@ const waffo = new Waffo({ /* config */ });
|
|
|
688
688
|
const webhookHandler = waffo.webhook()
|
|
689
689
|
.onPayment((notification) => {
|
|
690
690
|
console.log('Payment notification received:');
|
|
691
|
-
console.log(' Acquiring Order ID:', notification.acquiringOrderId);
|
|
692
|
-
console.log(' Order Status:', notification.orderStatus);
|
|
693
|
-
console.log(' Payment Amount:', notification.orderAmount);
|
|
694
|
-
console.log(' Payment Currency:', notification.orderCurrency);
|
|
691
|
+
console.log(' Acquiring Order ID:', notification.result?.acquiringOrderId);
|
|
692
|
+
console.log(' Order Status:', notification.result?.orderStatus);
|
|
693
|
+
console.log(' Payment Amount:', notification.result?.orderAmount);
|
|
694
|
+
console.log(' Payment Currency:', notification.result?.orderCurrency);
|
|
695
695
|
|
|
696
696
|
// Tip: First verify amount and currency match your records
|
|
697
697
|
// Then handle based on orderStatus
|
|
698
698
|
|
|
699
|
-
if (notification.orderStatus === 'PAY_SUCCESS') {
|
|
699
|
+
if (notification.result?.orderStatus === 'PAY_SUCCESS') {
|
|
700
700
|
// Payment successful - update order status, deliver goods, etc.
|
|
701
701
|
}
|
|
702
702
|
})
|
|
703
703
|
.onRefund((notification) => {
|
|
704
|
-
console.log('Refund notification:', notification.acquiringRefundOrderId);
|
|
704
|
+
console.log('Refund notification:', notification.result?.acquiringRefundOrderId);
|
|
705
705
|
// Handle refund notification
|
|
706
706
|
})
|
|
707
707
|
.onSubscriptionStatus((notification) => {
|
|
708
708
|
console.log('Subscription status notification:');
|
|
709
|
-
console.log(' Subscription ID:', notification.subscriptionId);
|
|
710
|
-
console.log(' Subscription Status:', notification.subscriptionStatus);
|
|
709
|
+
console.log(' Subscription ID:', notification.result?.subscriptionId);
|
|
710
|
+
console.log(' Subscription Status:', notification.result?.subscriptionStatus);
|
|
711
711
|
|
|
712
|
-
switch (notification.subscriptionStatus) {
|
|
712
|
+
switch (notification.result?.subscriptionStatus) {
|
|
713
713
|
case 'ACTIVE':
|
|
714
714
|
// Subscription activated - grant membership privileges
|
|
715
715
|
break;
|
|
@@ -731,28 +731,28 @@ const webhookHandler = waffo.webhook()
|
|
|
731
731
|
}
|
|
732
732
|
})
|
|
733
733
|
.onSubscriptionPeriodChanged((notification) => {
|
|
734
|
-
console.log('Subscription period changed:', notification.subscriptionId);
|
|
734
|
+
console.log('Subscription period changed:', notification.result?.subscriptionId);
|
|
735
735
|
// Key fields to track:
|
|
736
|
-
// - notification.period: Current period number
|
|
737
|
-
// - notification.nextChargeAt: Next billing time
|
|
738
|
-
// - notification.subscriptionStatus: Subscription status
|
|
739
|
-
// - notification.orderStatus: Current billing order status (SUCCESS/FAILED)
|
|
740
|
-
// - notification.orderAmount: Billing amount
|
|
741
|
-
// - notification.orderCurrency: Billing currency
|
|
736
|
+
// - notification.result?.period: Current period number
|
|
737
|
+
// - notification.result?.nextChargeAt: Next billing time
|
|
738
|
+
// - notification.result?.subscriptionStatus: Subscription status
|
|
739
|
+
// - notification.result?.orderStatus: Current billing order status (SUCCESS/FAILED)
|
|
740
|
+
// - notification.result?.orderAmount: Billing amount
|
|
741
|
+
// - notification.result?.orderCurrency: Billing currency
|
|
742
742
|
})
|
|
743
743
|
.onSubscriptionChange((notification) => {
|
|
744
744
|
console.log('Subscription change notification:');
|
|
745
|
-
console.log(' Change Request ID:', notification.subscriptionRequest);
|
|
746
|
-
console.log(' Change Status:', notification.subscriptionChangeStatus);
|
|
747
|
-
console.log(' Origin Subscription:', notification.originSubscriptionId);
|
|
748
|
-
console.log(' New Subscription:', notification.subscriptionId);
|
|
745
|
+
console.log(' Change Request ID:', notification.result?.subscriptionRequest);
|
|
746
|
+
console.log(' Change Status:', notification.result?.subscriptionChangeStatus);
|
|
747
|
+
console.log(' Origin Subscription:', notification.result?.originSubscriptionId);
|
|
748
|
+
console.log(' New Subscription:', notification.result?.subscriptionId);
|
|
749
749
|
|
|
750
|
-
if (notification.subscriptionChangeStatus === 'SUCCESS') {
|
|
750
|
+
if (notification.result?.subscriptionChangeStatus === 'SUCCESS') {
|
|
751
751
|
// Subscription change successful
|
|
752
752
|
// - Original subscription is now MERCHANT_CANCELLED
|
|
753
753
|
// - New subscription is now ACTIVE
|
|
754
754
|
// Update user's subscription level accordingly
|
|
755
|
-
} else if (notification.subscriptionChangeStatus === 'CLOSED') {
|
|
755
|
+
} else if (notification.result?.subscriptionChangeStatus === 'CLOSED') {
|
|
756
756
|
// Subscription change failed/closed
|
|
757
757
|
// Original subscription remains unchanged
|
|
758
758
|
}
|
|
@@ -1012,6 +1012,62 @@ const signature = RsaUtils.sign(data, privateKey);
|
|
|
1012
1012
|
const isValid = RsaUtils.verify(data, signature, publicKey);
|
|
1013
1013
|
```
|
|
1014
1014
|
|
|
1015
|
+
## Handling New API Fields (ExtraParams)
|
|
1016
|
+
|
|
1017
|
+
When Waffo API adds new fields that are not yet defined in the SDK, you can use the ExtraParams feature to access these fields without waiting for an SDK update.
|
|
1018
|
+
|
|
1019
|
+
### Reading Unknown Fields from Responses
|
|
1020
|
+
|
|
1021
|
+
```typescript
|
|
1022
|
+
// Get extra field from response
|
|
1023
|
+
const response = await waffo.order().inquiry({ paymentRequestId: 'REQ001' });
|
|
1024
|
+
if (response.isSuccess()) {
|
|
1025
|
+
const data = response.getData();
|
|
1026
|
+
|
|
1027
|
+
// Access field not yet defined in SDK
|
|
1028
|
+
const newField = data.extraParams?.['newField'];
|
|
1029
|
+
|
|
1030
|
+
// Or use type assertion if you know the type
|
|
1031
|
+
const typedValue = data.extraParams?.['newField'] as string;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
// Get extra field from webhook notification
|
|
1035
|
+
webhookHandler.onPaymentNotification((notification) => {
|
|
1036
|
+
const result = notification.result;
|
|
1037
|
+
const newField = result.extraParams?.['newField'];
|
|
1038
|
+
});
|
|
1039
|
+
```
|
|
1040
|
+
|
|
1041
|
+
### Sending Extra Fields in Requests
|
|
1042
|
+
|
|
1043
|
+
```typescript
|
|
1044
|
+
// TypeScript types include index signature [key: string]: unknown
|
|
1045
|
+
// You can directly add extra fields to any request
|
|
1046
|
+
const response = await waffo.order().create({
|
|
1047
|
+
paymentRequestId: 'REQ001',
|
|
1048
|
+
merchantOrderId: 'ORDER001',
|
|
1049
|
+
// ... other required fields
|
|
1050
|
+
newField: 'value', // Extra field - no type error
|
|
1051
|
+
nested: { key: 123 } // Nested object - works too
|
|
1052
|
+
});
|
|
1053
|
+
```
|
|
1054
|
+
|
|
1055
|
+
### Important Notes
|
|
1056
|
+
|
|
1057
|
+
> **Upgrade SDK Promptly**
|
|
1058
|
+
>
|
|
1059
|
+
> ExtraParams is designed as a **temporary solution** for accessing new API fields before SDK updates.
|
|
1060
|
+
>
|
|
1061
|
+
> **Best Practices:**
|
|
1062
|
+
> 1. Check SDK release notes regularly for new field support
|
|
1063
|
+
> 2. Once SDK officially supports the field, migrate from `getExtraParam("field")` to the official getter (e.g., `getField()`)
|
|
1064
|
+
> 3. The SDK logs a warning when you use `getExtraParam()` on officially supported fields
|
|
1065
|
+
>
|
|
1066
|
+
> **Why migrate?**
|
|
1067
|
+
> - Official getters provide type safety
|
|
1068
|
+
> - Better IDE auto-completion and documentation
|
|
1069
|
+
> - Reduced risk of typos in field names
|
|
1070
|
+
|
|
1015
1071
|
## Error Handling
|
|
1016
1072
|
|
|
1017
1073
|
### Error Handling Pattern
|