Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
haiyen-payment
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Giang Tran
haiyen-payment
Commits
503cc251
Commit
503cc251
authored
Sep 03, 2026
by
tdgiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update code
parent
4bdc896a
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
109 additions
and
69 deletions
+109
-69
SobanleClient.js
app/libs/SobanleClient.js
+109
-69
1788403522532_5421708034066430891_5421708034066430891_74f6e61c81ca514e312f7e4246632849.jpg
..._5421708034066430891_74f6e61c81ca514e312f7e4246632849.jpg
+0
-0
Screenshot 0008-09-03 at 09.36.35.png
docs/Screenshot 0008-09-03 at 09.36.35.png
+0
-0
2026-08-31-sobanle-auto-order.md
docs/superpowers/plans/2026-08-31-sobanle-auto-order.md
+0
-0
No files found.
app/libs/SobanleClient.js
View file @
503cc251
...
...
@@ -7,22 +7,26 @@ var tokenCache = { token: null, expiresAt: 0 };
function
login
(
callback
)
{
var
url
=
config
.
sobanle
.
base_url
+
"/login"
;
ApiRequest
.
postOtherUrl
(
url
,
{
login
:
config
.
sobanle
.
username
,
password
:
config
.
sobanle
.
password
},
function
(
err
,
body
)
{
if
(
err
)
{
return
callback
(
err
);
}
if
(
!
body
||
!
body
.
access_token
)
{
return
callback
(
new
Error
(
"SOBANLE_LOGIN_FAILED"
));
}
tokenCache
.
token
=
body
.
access_token
;
var
expiresInMs
=
(
body
.
expires_in
||
0
)
*
1000
;
// Refresh a bit early to avoid racing a near-expiry token. Headroom scales
// with the token's own TTL (capped at 60s) rather than a flat 60s, so a
// short-TTL token isn't already "expired" the instant it's cached.
var
earlyRefreshMs
=
Math
.
min
(
60000
,
expiresInMs
*
0.1
);
tokenCache
.
expiresAt
=
Date
.
now
()
+
expiresInMs
-
earlyRefreshMs
;
callback
(
null
,
body
.
access_token
);
});
ApiRequest
.
postOtherUrl
(
url
,
{
login
:
config
.
sobanle
.
username
,
password
:
config
.
sobanle
.
password
},
function
(
err
,
body
)
{
if
(
err
)
{
return
callback
(
err
);
}
if
(
!
body
||
!
body
.
access_token
)
{
return
callback
(
new
Error
(
"SOBANLE_LOGIN_FAILED"
));
}
tokenCache
.
token
=
body
.
access_token
;
var
expiresInMs
=
(
body
.
expires_in
||
0
)
*
1000
;
// Refresh a bit early to avoid racing a near-expiry token. Headroom scales
// with the token's own TTL (capped at 60s) rather than a flat 60s, so a
// short-TTL token isn't already "expired" the instant it's cached.
var
earlyRefreshMs
=
Math
.
min
(
60000
,
expiresInMs
*
0.1
);
tokenCache
.
expiresAt
=
Date
.
now
()
+
expiresInMs
-
earlyRefreshMs
;
callback
(
null
,
body
.
access_token
);
},
);
}
exports
.
login
=
login
;
...
...
@@ -78,67 +82,98 @@ function withAuthRetry(makeRequest, callback) {
}
function
getProducts
(
callback
)
{
withAuthRetry
(
function
(
token
,
cb
)
{
var
url
=
config
.
sobanle
.
base_url
+
"/products?page=1&is_active=true&per_page=1000&"
;
ApiRequest
.
getOtherUrlWithHeader
(
url
,
{},
{
Authorization
:
"Bearer "
+
token
},
cb
);
},
function
(
err
,
body
)
{
if
(
err
)
{
return
callback
(
err
);
}
if
(
!
body
||
!
Array
.
isArray
(
body
.
data
))
{
return
callback
(
new
Error
(
"SOBANLE_PRODUCTS_INVALID"
));
}
callback
(
null
,
body
.
data
);
});
withAuthRetry
(
function
(
token
,
cb
)
{
var
url
=
config
.
sobanle
.
base_url
+
"/products?page=1&is_active=true&per_page=1000&"
;
ApiRequest
.
getOtherUrlWithHeader
(
url
,
{},
{
Authorization
:
"Bearer "
+
token
},
cb
,
);
},
function
(
err
,
body
)
{
if
(
err
)
{
return
callback
(
err
);
}
if
(
!
body
||
!
Array
.
isArray
(
body
.
data
))
{
return
callback
(
new
Error
(
"SOBANLE_PRODUCTS_INVALID"
));
}
callback
(
null
,
body
.
data
);
},
);
}
exports
.
getProducts
=
getProducts
;
function
createSale
(
lines
,
customerData
,
callback
)
{
withAuthRetry
(
function
(
token
,
cb
)
{
var
url
=
config
.
sobanle
.
base_url
+
"/sales"
;
var
payload
=
{
warehouse_id
:
config
.
sobanle
.
warehouse_id
,
biller_id
:
config
.
sobanle
.
biller_id
,
account_id
:
config
.
sobanle
.
account_id
,
currency_id
:
config
.
sobanle
.
currency_id
,
exchange_rate
:
"1"
,
reference_no
:
null
,
is_internal_api
:
true
,
sale_status
:
2
,
list_product
:
lines
.
map
(
function
(
line
)
{
return
{
product_id
:
String
(
line
.
product_id
),
qty
:
String
(
line
.
qty
)
};
}),
customer_data
:
customerData
,
payment_receiver
:
""
,
payment_note
:
""
,
sale_note
:
"Tạo tự động từ payment-gate"
,
staff_note
:
""
,
};
ApiRequest
.
postOtherUrlWithHeader
(
url
,
payload
,
{
Authorization
:
"Bearer "
+
token
},
cb
);
},
function
(
err
,
body
)
{
if
(
err
)
{
return
callback
(
err
);
}
if
(
!
body
||
!
body
.
data
||
typeof
body
.
data
.
id
===
"undefined"
||
typeof
body
.
data
.
grand_total
===
"undefined"
)
{
return
callback
(
new
Error
(
"SOBANLE_CREATE_SALE_INVALID"
));
}
var
total
=
Math
.
round
(
Number
(
body
.
data
.
grand_total
));
if
(
!
isFinite
(
total
)
||
total
<=
0
)
{
return
callback
(
new
Error
(
"SOBANLE_CREATE_SALE_INVALID"
));
}
callback
(
null
,
{
orderId
:
body
.
data
.
id
,
orderTotal
:
total
});
});
withAuthRetry
(
function
(
token
,
cb
)
{
var
url
=
config
.
sobanle
.
base_url
+
"/sales"
;
var
payload
=
{
warehouse_id
:
config
.
sobanle
.
warehouse_id
,
biller_id
:
config
.
sobanle
.
biller_id
,
account_id
:
config
.
sobanle
.
account_id
,
currency_id
:
config
.
sobanle
.
currency_id
,
exchange_rate
:
"1"
,
reference_no
:
null
,
is_internal_api
:
true
,
sale_status
:
2
,
list_product
:
lines
.
map
(
function
(
line
)
{
return
{
product_id
:
String
(
line
.
product_id
),
qty
:
String
(
line
.
qty
)
};
}),
customer_data
:
customerData
,
payment_receiver
:
""
,
payment_note
:
""
,
staff_note
:
""
,
};
ApiRequest
.
postOtherUrlWithHeader
(
url
,
payload
,
{
Authorization
:
"Bearer "
+
token
},
cb
,
);
},
function
(
err
,
body
)
{
if
(
err
)
{
return
callback
(
err
);
}
if
(
!
body
||
!
body
.
data
||
typeof
body
.
data
.
id
===
"undefined"
||
typeof
body
.
data
.
grand_total
===
"undefined"
)
{
return
callback
(
new
Error
(
"SOBANLE_CREATE_SALE_INVALID"
));
}
var
total
=
Math
.
round
(
Number
(
body
.
data
.
grand_total
));
if
(
!
isFinite
(
total
)
||
total
<=
0
)
{
return
callback
(
new
Error
(
"SOBANLE_CREATE_SALE_INVALID"
));
}
callback
(
null
,
{
orderId
:
body
.
data
.
id
,
orderTotal
:
total
});
},
);
}
exports
.
createSale
=
createSale
;
// saleStatus: 1 = giao dịch thành công, 4 = giao dịch thất bại (theo Sổ Bán Lẻ).
function
changeSaleStatus
(
orderId
,
saleStatus
,
callback
)
{
withAuthRetry
(
function
(
token
,
cb
)
{
var
url
=
config
.
sobanle
.
base_url
+
"/sales/change-sale-status/"
+
orderId
;
ApiRequest
.
patchOtherUrlWithHeader
(
url
,
{
sale_status
:
saleStatus
},
{
Authorization
:
"Bearer "
+
token
},
cb
);
},
function
(
err
)
{
callback
(
err
||
null
);
});
withAuthRetry
(
function
(
token
,
cb
)
{
var
url
=
config
.
sobanle
.
base_url
+
"/sales/change-sale-status/"
+
orderId
;
ApiRequest
.
patchOtherUrlWithHeader
(
url
,
{
sale_status
:
saleStatus
},
{
Authorization
:
"Bearer "
+
token
},
cb
,
);
},
function
(
err
)
{
callback
(
err
||
null
);
},
);
}
exports
.
changeSaleStatus
=
changeSaleStatus
;
...
...
@@ -158,7 +193,12 @@ function createAutoOrder(customerData, targetAmount, callback) {
if
(
order
.
orderTotal
>
targetAmount
)
{
console
.
error
(
"createAutoOrder: Sổ Bán Lẻ order total exceeds target (orphan order sobanleOrderId="
+
order
.
orderId
+
" orderTotal="
+
order
.
orderTotal
+
" targetAmount="
+
targetAmount
+
")"
order
.
orderId
+
" orderTotal="
+
order
.
orderTotal
+
" targetAmount="
+
targetAmount
+
")"
,
);
return
callback
(
new
Error
(
"SOBANLE_TOTAL_EXCEEDS_TARGET"
));
}
...
...
docs/1788403522532_5421708034066430891_5421708034066430891_74f6e61c81ca514e312f7e4246632849.jpg
0 → 100644
View file @
503cc251
480 KB
docs/Screenshot 0008-09-03 at 09.36.35.png
0 → 100644
View file @
503cc251
192 KB
docs/superpowers/plans/2026-08-31-sobanle-auto-order.md
View file @
503cc251
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment