Skip to content

Commit

Permalink
fix: trim strings
Browse files Browse the repository at this point in the history
  • Loading branch information
seanghay committed Jan 12, 2024
1 parent 5c6acb5 commit 352e7f1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { FormData } from 'formdata-node';

export declare function trim<T = string | null | undefined>(value: T): T

export declare type TransactionStatus =
| "APPROVED"
| "DECLINED"
Expand Down
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ const { createHmac } = require('node:crypto');
const axios = require('axios').default;
const { FormData } = require("formdata-node");

exports.trim = function trim(value) {
if (typeof value === 'string') return value.trim()
return value
}

class PayWayClient {

constructor(base_url, merchant_id, api_key, client_factory) {
Expand Down Expand Up @@ -81,18 +86,18 @@ class PayWayClient {
if (typeof return_url === 'string') return_url = base64(return_url);
if (typeof return_deeplink === 'string') return_deeplink = base64(return_deeplink);
if (typeof return_deeplink === 'object' && return_deeplink != null) return_deeplink = base64(JSON.stringify(return_deeplink));

const response = await this._client.post(
"/api/payment-gateway/v1/payments/purchase",
// order matters here
this.create_payload({
tran_id,
amount,
pwt,
firstname,
lastname,
email,
phone,
firstname: trim(firstname),
lastname: trim(lastname),
email: trim(email),
phone: trim(phone),
payment_option,
return_url,
continue_success_url,
Expand Down
11 changes: 10 additions & 1 deletion index.test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import test from 'ava'
import { PayWayClient } from './index.js'
import { PayWayClient, trim } from './index.js'

test("should trim", t => {
t.is(trim("abc "), "abc")
t.is(trim(" abc "), "abc")
t.is(trim(), undefined)
t.is(trim(null), null)
t.is(trim(1), 1)
t.is(trim(NaN), NaN)
})

test("should hash", t => {
const client = new PayWayClient("http://example.com", "1", "1");
Expand Down

0 comments on commit 352e7f1

Please sign in to comment.