ข้ามไปยังเนื้อหาหลัก

ภาพรวม Balance & Transactions

Balance ในบัญชี Omise ของคุณแสดงถึงเงินทุนที่มีอยู่ในบัญชี คู่มือนี้อธิบายวิธีตรวจสอบ balance, ทำความเข้าใจประเภท balance ต่างๆ และจัดการเงินทุนอย่างมีประสิทธิภาพ

ภาพรวม

การทำความเข้าใจ balance ในบัญชีมีความสำคัญสำหรับ:

  • การวางแผน Transfer: การรู้จำนวนเงินที่พร้อมใช้งานสำหรับการจ่ายเงิน
  • การจัดการกระแสเงินสด: การติดตามเงินเข้าและเงินออก
  • Reconciliation: การจับคู่ transaction กับเงินฝากธนาคาร
  • การวางแผนทางการเงิน: การคาดการณ์เงินทุนที่พร้อมใช้งาน
  • การตัดสินใจเชิงปฏิบัติการ: การวางแผน refund และ transfer

แนวคิดหลัก

  • Total Balance: เงินทุนทั้งหมดในบัญชีของคุณ (available + pending)
  • Available Balance: เงินทุนที่พร้อมใช้งานทันที
  • Pending Balance: เงินทุนที่รอ settlement หรือการประมวลผล
  • Transferable Balance: จำนวนเงินที่สามารถโอนได้หลังหักค่าธรรมเนียม
  • Currency: Balance ที่ติดตามตามสกุลเงิน

การตรวจสอบ Balance

ดึง Balance ปัจจุบัน

const omise = require('omise')({
secretKey: 'skey_test_123456789',
});

// Get current balance
async function getBalance() {
try {
const balance = await omise.balance.retrieve();

console.log('Balance Information:');
console.log('------------------');
console.log(`Total: ${balance.total / 100} ${balance.currency.toUpperCase()}`);
console.log(`Available: ${balance.available / 100} ${balance.currency.toUpperCase()}`);
console.log(`Currency: ${balance.currency.toUpperCase()}`);
console.log(`Livemode: ${balance.livemode}`);

return balance;
} catch (error) {
console.error('Failed to retrieve balance:', error.message);
throw error;
}
}

// Get detailed balance breakdown
async function getBalanceBreakdown() {
const balance = await omise.balance.retrieve();

const pending = balance.total - balance.available;

return {
total: balance.total,
available: balance.available,
pending: pending,
currency: balance.currency,
breakdown: {
total_formatted: `${balance.total / 100} ${balance.currency.toUpperCase()}`,
available_formatted: `${balance.available / 100} ${balance.currency.toUpperCase()}`,
pending_formatted: `${pending / 100} ${balance.currency.toUpperCase()}`
}
};
}

// Check if sufficient balance for operation
async function hasSufficientBalance(requiredAmount) {
const balance = await omise.balance.retrieve();

return {
sufficient: balance.available >= requiredAmount,
available: balance.available,
required: requiredAmount,
shortfall: Math.max(0, requiredAmount - balance.available)
};
}

// Example usage
getBalance();
getBalanceBreakdown().then(breakdown => console.log(breakdown));
hasSufficientBalance(100000).then(result => {
if (result.sufficient) {
console.log('✓ Sufficient balance');
} else {
console.log(`✗ Insufficient balance. Short by ${result.shortfall / 100}`);
}
});

API Response

{
"object": "balance",
"livemode": false,
"location": "/balance",
"available": 500000,
"total": 750000,
"currency": "thb"
}

ทำความเข้าใจประเภท Balance

Available Balance กับ Total Balance

// Available Balance: พร้อมใช้งานทันที
// Total Balance: Available + Pending เงินทุน

function explainBalance(balance) {
const pending = balance.total - balance.available;

console.log('Balance Explanation:');
console.log(`Total Balance: ${balance.total / 100} ${balance.currency.toUpperCase()}`);
console.log(` ├─ Available: ${balance.available / 100} (พร้อมใช้งาน)`);
console.log(` └─ Pending: ${pending / 100} (กำลังประมวลผล)`);

console.log('\nสิ่งที่คุณสามารถทำได้:');
console.log(` • สร้าง transfer ได้สูงสุด: ${balance.available / 100}`);
console.log(` • สร้าง refund ได้สูงสุด: ${balance.available / 100}`);
console.log(` • Pending จะกลายเป็น available หลัง settlement`);
}

Pending Balance

Pending balance ประกอบด้วย:

  • Charge ล่าสุดที่รอ settlement
  • Refund ที่กำลังประมวลผล
  • Transfer ที่กำลังประมวลผล
  • Charge ที่มีข้อพิพาทค้างอยู่
def analyze_pending_balance():
"""Analyze why funds are pending"""
balance = omise.Balance.retrieve()
pending = balance.total - balance.available

if pending > 0:
print(f"Pending Balance: {pending / 100:.2f} {balance.currency.upper()}")
print("\nสาเหตุที่เป็นไปได้ของเงินทุนที่ pending:")
print(" • Charge ล่าสุดที่รอ settlement (1-2 วัน)")
print(" • Refund ที่กำลังประมวลผล")
print(" • Transfer ที่กำลังดำเนินการ")
print(" • Transaction ที่มีข้อพิพาทค้างอยู่")
print("\nเงินทุนเหล่านี้จะกลายเป็น available หลัง settlement")
else:
print("ไม่มีเงินทุน pending ทั้งหมด balance พร้อมใช้งาน")

การตรวจสอบ Balance

การตรวจสอบแบบเรียลไทม์

class BalanceMonitor {
constructor(thresholds) {
this.thresholds = thresholds || {
low: 100000, // 1,000 THB
critical: 50000 // 500 THB
};
}

async checkBalance() {
const balance = await omise.balance.retrieve();

const status = {
current: balance.available,
status: this.getBalanceStatus(balance.available),
alerts: []
};

if (balance.available <= this.thresholds.critical) {
status.alerts.push({
level: 'critical',
message: `Critical: Balance at ${balance.available / 100} ${balance.currency.toUpperCase()}`
});
await this.notifyCriticalBalance(balance);
} else if (balance.available <= this.thresholds.low) {
status.alerts.push({
level: 'warning',
message: `Warning: Low balance at ${balance.available / 100} ${balance.currency.toUpperCase()}`
});
await this.notifyLowBalance(balance);
}

return status;
}

getBalanceStatus(available) {
if (available <= this.thresholds.critical) return 'critical';
if (available <= this.thresholds.low) return 'low';
return 'healthy';
}

async notifyCriticalBalance(balance) {
console.log('🚨 CRITICAL: Balance very low!');
// Send urgent notification
}

async notifyLowBalance(balance) {
console.log('⚠️ WARNING: Balance is low');
// Send warning notification
}
}

// Usage
const monitor = new BalanceMonitor({
low: 100000,
critical: 50000
});

setInterval(async () => {
const status = await monitor.checkBalance();
console.log('Balance Status:', status.status);
}, 3600000); // Check every hour

กรณีการใช้งานทั่วไป

1. การตรวจสอบ Balance ก่อนการดำเนินการ

class BalanceGuard:
def __init__(self, buffer_amount=0):
self.buffer_amount = buffer_amount

def can_perform_operation(self, operation_type, amount):
"""Check if operation can be performed"""
balance = omise.Balance.retrieve()

# Calculate required amount based on operation
if operation_type == 'transfer':
# Transfer + fee
required = amount + 2500 # 25 THB fee
elif operation_type == 'refund':
required = amount
else:
required = amount

# Add buffer
required += self.buffer_amount

if balance.available < required:
return {
'allowed': False,
'reason': 'insufficient_balance',
'available': balance.available,
'required': required,
'shortfall': required - balance.available
}

return {
'allowed': True,
'available': balance.available,
'required': required,
'remaining_after': balance.available - required
}

# Usage
guard = BalanceGuard(buffer_amount=10000) # Keep 100 THB buffer
check = guard.can_perform_operation('transfer', 100000)

if check['allowed']:
print(f"✓ สามารถดำเนินการได้ จะเหลือ {check['remaining_after']/100:.2f}")
else:
print(f"✗ ไม่สามารถดำเนินการได้ ขาด {check['shortfall']/100:.2f}")

2. รายงาน Balance รายวัน

class DailyBalanceReport
def generate_report
balance = Omise::Balance.retrieve

report = {
date: Date.today.to_s,
balance: {
total: balance.total,
available: balance.available,
pending: balance.total - balance.available,
currency: balance.currency
},
formatted: format_balance(balance),
status: assess_balance_health(balance),
recommendations: get_recommendations(balance)
}

print_report(report)
save_report(report)

report
end

def format_balance(balance)
{
total: "#{balance.total / 100.0} #{balance.currency.upcase}",
available: "#{balance.available / 100.0} #{balance.currency.upcase}",
pending: "#{(balance.total - balance.available) / 100.0} #{balance.currency.upcase}"
}
end

def assess_balance_health(balance)
case balance.available
when 0..50000
'critical'
when 50001..100000
'low'
when 100001..500000
'moderate'
else
'healthy'
end
end

def get_recommendations(balance)
recommendations = []

if balance.available < 100000
recommendations << "พิจารณาเพิ่มเงินทุน"
end

pending = balance.total - balance.available
if pending > balance.available * 2
recommendations << "Pending balance สูง - ตรวจสอบ settlement"
end

recommendations
end

def print_report(report)
puts "\n#{'='*50}"
puts "รายงาน BALANCE รายวัน - #{report[:date]}"
puts "="*50
puts "Total: #{report[:formatted][:total]}"
puts "Available: #{report[:formatted][:available]}"
puts "Pending: #{report[:formatted][:pending]}"
puts "Status: #{report[:status].upcase}"

unless report[:recommendations].empty?
puts "\nคำแนะนำ:"
report[:recommendations].each { |r| puts " • #{r}" }
end
puts "="*50
end

def save_report(report)
# Save to file or database
File.open('balance_reports.log', 'a') do |f|
f.puts JSON.generate(report)
end
end
end

แนวทางปฏิบัติที่ดีที่สุด

1. ตรวจสอบ Balance ก่อนการดำเนินการเสมอ

async function safeOperation(operationType, amount) {
const balance = await omise.balance.retrieve();

// Calculate required amount
let required = amount;
if (operationType === 'transfer') {
required += 2500; // Add transfer fee
}

// Check balance
if (balance.available < required) {
throw new Error(`Insufficient balance: need ${required}, have ${balance.available}`);
}

// Proceed with operation
console.log(`✓ Sufficient balance. Proceeding with ${operationType}`);
return true;
}

2. ตรวจสอบ Balance อย่างสม่ำเสมอ

import schedule
import time

def monitor_balance():
"""Scheduled balance monitoring"""
balance = omise.Balance.retrieve()

if balance.available < 50000:
send_alert(f"Critical: Balance at {balance.available/100:.2f}")
elif balance.available < 100000:
send_notification(f"Warning: Low balance at {balance.available/100:.2f}")

# Run every hour
schedule.every().hour.do(monitor_balance)

while True:
schedule.run_pending()
time.sleep(60)

3. รักษาจำนวนเงินสำรอง

function maintainBalanceBuffer($operationAmount, $bufferAmount = 10000) {
$balance = OmiseBalance::retrieve();

$required = $operationAmount + $bufferAmount;

if ($balance['available'] < $required) {
throw new Exception("Insufficient balance including buffer");
}

echo "✓ Sufficient balance with buffer maintained\n";
return true;
}

คำถามที่พบบ่อย

ความแตกต่างระหว่าง available balance และ total balance คืออะไร?

Available balance คือจำนวนเงินที่คุณสามารถใช้ได้ทันทีสำหรับ transfer และ refund Total balance รวมทั้ง available และ pending เงินทุน (charge ที่รอ settlement, refund ที่กำลังประมวลผล ฯลฯ)

Pending balance จะกลายเป็น available ใช้เวลานานแค่ไหน?

โดยทั่วไป 1-2 วันทำการ เวลาที่แน่นอนขึ้นอยู่กับตารางเวลา settlement และประเภทของ transaction

ฉันสามารถใช้ pending balance สำหรับ transfer ได้หรือไม่?

ไม่ได้ มีเพียง available balance เท่านั้นที่สามารถใช้สำหรับ transfer และ refund เงินทุน pending ต้อง settle ก่อน

ทำไม available balance ของฉันถึงน้อยกว่าที่คาดไว้?

สาเหตุทั่วไป:

  • Transfer ล่าสุดที่ถูกหักจาก balance
  • Refund ที่ประมวลผลแล้ว
  • Pending settlement ที่ยังไม่ cleared
  • ความต้องการสำรอง (ถ้ามี)
  • ข้อพิพาทที่กักเก็บเงินทุน

ฉันควรตรวจสอบ balance ของฉันบ่อยแค่ไหน?

สำหรับธุรกิจที่มี transaction ปริมาณสูง ให้ตรวจสอบก่อนการดำเนินการสำคัญแต่ละครั้ง ใช้การตรวจสอบอัตโนมัติเพื่อแจ้งเตือนเมื่อ balance ลดลงต่ำกว่าเกณฑ์

Balance สามารถเป็นลบได้หรือไม่?

ไม่ได้ การดำเนินการที่ต้องการมากกว่า available balance ของคุณจะล้มเหลว ตรวจสอบ balance ก่อนการดำเนินการเสมอ

Balance ของฉันอยู่ในสกุลเงินอะไร?

Balance ถูกติดตามตามสกุลเงิน บัญชีหลายสกุลเงินจะมี balance แยกกันสำหรับแต่ละสกุลเงิน (THB, USD ฯลฯ)

ฉันจะเพิ่มเงินทุนใน balance ของฉันได้อย่างไร?

Balance ของคุณจะเพิ่มขึ้นโดยอัตโนมัติเมื่อคุณได้รับ charge ที่สำเร็จจากลูกค้า จะลดลงเมื่อคุณทำ refund หรือสร้าง transfer

แหล่งข้อมูลที่เกี่ยวข้อง

  • Transaction History - ดูการเปลี่ยนแปลง balance ทั้งหมด
  • Reconciliation - จับคู่ Transaction กับ Settlement
  • Creating Transfers - โอนเงินทุนจาก Balance
  • Refunds - ทำความเข้าใจผลกระทบของ Refund ต่อ Balance
  • Webhooks - ตรวจสอบการเปลี่ยนแปลง balance แบบเรียลไทม์

ขั้นตอนถัดไป