การดู Transaction History
Transaction history ให้บันทึกที่สมบูรณ์ของกิจกรรมทั้งหมดที่ส่งผลต่อ balance ในบัญชี Omise ของคุณ คู่มือนี้ครอบคลุมการดู กรอง ค้นหา และส่งออกข้อมูล transaction
ภาพรวม
Transaction history ประกอบด้วย:
- Charge: การชำระเงินจากลูกค้าที่ได้รับ
- Refund: เงินที่คืนให้ลูกค้า
- Transfer: การจ่ายเงินให้ผู้รับ
- Fee: ค่าธรรมเนียมแพลตฟอร์มและ transaction
- Adjustment: การแก้ไข balance ด้วยตนเอง
- Dispute: Transaction ที่เกี่ยวข้องกับ chargeback
คุณสมบัติหลัก
- ประวัติที่สมบูรณ์: Transaction ทั้งหมดที่ส่งผลต่อ Balance
- การกรองขั้นสูง: ก รองตามประเภท วันที่ จำนวนเงิน สถานะ
- ความสามารถในการค้นหา: ค้นหา transaction เฉพาะอย่างรวดเร็ว
- Pagination: จัดการ transaction ปริมาณมาก
- ตัวเลือกการส่งออก: ดาวน์โหลดข้อมูลสำหรับรายงาน
- การอัปเดตแบบเรียลไทม์: เห็น transaction เมื่อเกิดขึ้น
การดึง Transaction
แสดงรายการ Transaction ทั้งหมด
- Node.js
- Python
- Ruby
- PHP
const omise = require('omise')({
secretKey: 'skey_test_123456789',
});
// Get recent transactions
async function getTransactions(limit = 20) {
try {
const transactions = await omise.transactions.list({
limit: limit,
order: 'reverse_chronological'
});
console.log(`Found ${transactions.total} transactions`);
console.log(`Showing ${transactions.data.length} of ${transactions.total}`);
transactions.data.forEach(txn => {
console.log(`${txn.id}: ${txn.type} - ${txn.amount / 100} ${txn.currency.toUpperCase()}`);
});
return transactions;
} catch (error) {
console.error('Failed to retrieve transactions:', error.message);
throw error;
}
}
// Get transactions with pagination
async function getPaginatedTransactions(page = 1, perPage = 20) {
const offset = (page - 1) * perPage;
const transactions = await omise.transactions.list({
limit: perPage,
offset: offset,
order: 'reverse_chronological'
});
return {
data: transactions.data,
pagination: {
page: page,
per_page: perPage,
total: transactions.total,
total_pages: Math.ceil(transactions.total / perPage),
has_more: transactions.data.length === perPage
}
};
}
// Get transactions by date range
async function getTransactionsByDateRange(startDate, endDate) {
const from = new Date(startDate).toISOString();
const to = new Date(endDate).toISOString();
const transactions = await omise.transactions.list({
from: from,
to: to,
order: 'reverse_chronological'
});
console.log(`Transactions from ${startDate} to ${endDate}:`);
console.log(`Total: ${transactions.total}`);
return transactions;
}
// Example usage
getTransactions(10);
getPaginatedTransactions(1, 20).then(result => {
console.log(`Page ${result.pagination.page} of ${result.pagination.total_pages}`);
});
import omise
from datetime import datetime, timedelta
omise.api_secret = 'skey_test_123456789'
def get_transactions(limit=20):
"""Get recent transactions"""
try:
transactions = omise.Transaction.retrieve(
limit=limit,
order='reverse_chronological'
)
print(f"Found {transactions.total} transactions")
print(f"Showing {len(transactions.data)} of {transactions.total}")
for txn in transactions.data:
print(f"{txn.id}: {txn.type} - {txn.amount / 100} {txn.currency.upper()}")
return transactions
except omise.errors.BaseError as e:
print(f"Failed to retrieve transactions: {str(e)}")
raise
def get_paginated_transactions(page=1, per_page=20):
"""Get transactions with pagination"""
offset = (page - 1) * per_page
transactions = omise.Transaction.retrieve(
limit=per_page,
offset=offset,
order='reverse_chronological'
)
return {
'data': transactions.data,
'pagination': {
'page': page,
'per_page': per_page,
'total': transactions.total,
'total_pages': (transactions.total + per_page - 1) // per_page,
'has_more': len(transactions.data) == per_page
}
}
# Example usage
get_transactions(10)
result = get_paginated_transactions(1, 20)
print(f"Page {result['pagination']['page']} of {result['pagination']['total_pages']}")
require 'omise'
require 'date'
Omise.api_key = 'skey_test_123456789'
def get_transactions(limit = 20)
begin
transactions = Omise::Transaction.retrieve(
limit: limit,
order: 'reverse_chronological'
)
puts "Found #{transactions.total} transactions"
puts "Showing #{transactions.data.length} of #{transactions.total}"
transactions.data.each do |txn|
puts "#{txn.id}: #{txn.type} - #{txn.amount / 100.0} #{txn.currency.upcase}"
end
transactions
rescue Omise::Error => e
puts "Failed to retrieve transactions: #{e.message}"
raise
end
end
# Example usage
transactions = get_transactions(10)
<?php
require_once 'vendor/autoload.php';
define('OMISE_SECRET_KEY', 'skey_test_123456789');
function getTransactions($limit = 20) {
try {
$transactions = OmiseTransaction::retrieve([
'limit' => $limit,
'order' => 'reverse_chronological'
]);
echo "Found {$transactions['total']} transactions\n";
echo "Showing " . count($transactions['data']) . " of {$transactions['total']}\n";
foreach ($transactions['data'] as $txn) {
echo "{$txn['id']}: {$txn['type']} - " . ($txn['amount'] / 100) . " " . strtoupper($txn['currency']) . "\n";
}
return $transactions;
} catch (Exception $e) {
echo "Failed to retrieve transactions: {$e->getMessage()}\n";
throw $e;
}
}
getTransactions(10);
?>
API Response
{
"object": "list",
"data": [
{
"object": "transaction",
"id": "trxn_test_5xyz789abc",
"type": "credit",
"amount": 100000,
"currency": "thb",
"created": "2024-01-15T10:30:00Z",
"source": "chrg_test_123456789"
}
],
"limit": 20,
"offset": 0,
"total": 150,
"location": "/transactions",
"order": "chronological",
"from": "2024-01-01T00:00:00Z",
"to": "2024-01-31T23:59:59Z"
}
การกรองและการค้นหา
การกรองขั้นสูง
class TransactionFilter {
constructor() {
this.filters = {};
}
async byDateRange(startDate, endDate) {
this.filters.from = new Date(startDate).toISOString();
this.filters.to = new Date(endDate).toISOString();
return this;
}
async byType(type) {
this.filters.type = type; // 'credit' or 'debit'
return this;
}
async byAmountRange(minAmount, maxAmount) {
this.filters.minAmount = minAmount;
this.filters.maxAmount = maxAmount;
return this;
}
async execute() {
const transactions = await omise.transactions.list({
from: this.filters.from,
to: this.filters.to,
limit: 100,
order: 'reverse_chronological'
});
let filtered = transactions.data;
if (this.filters.type) {
filtered = filtered.filter(txn => txn.type === this.filters.type);
}
if (this.filters.minAmount || this.filters.maxAmount) {
filtered = filtered.filter(txn => {
if (this.filters.minAmount && txn.amount < this.filters.minAmount) return false;
if (this.filters.maxAmount && txn.amount > this.filters.maxAmount) return false;
return true;
});
}
return filtered;
}
}
// Usage
const filter = new TransactionFilter();
const results = await filter
.byDateRange('2024-01-01', '2024-01-31')
.byType('credit')
.byAmountRange(100000, 1000000)
.execute();
console.log(`Found ${results.length} matching transactions`);
การส่งออก Transaction
ส่งออกเป็น CSV
import csv
from datetime import datetime, timedelta
def export_transactions_csv(start_date, end_date, filename='transactions.csv'):
"""Export transactions to CSV"""
# Get transactions
transactions = omise.Transaction.retrieve(
created={
'gte': start_date.isoformat(),
'lte': end_date.isoformat()
},
limit=1000,
order='chronological'
)
# Write to CSV
with open(filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
# Write header
writer.writerow([
'Transaction ID',
'Date',
'Type',
'Amount',
'Currency',
'Source',
'Description'
])
# Write data
for txn in transactions.data:
writer.writerow([
txn.id,
datetime.fromtimestamp(txn.created).strftime('%Y-%m-%d %H:%M:%S'),
txn.type,
txn.amount / 100,
txn.currency.upper(),
txn.source if hasattr(txn, 'source') else 'N/A',
f"{txn.type.capitalize()} transaction"
])
print(f"Exported {len(transactions.data)} transactions to {filename}")
return filename
# Usage
start = datetime.now() - timedelta(days=30)
end = datetime.now()
export_transactions_csv(start, end, 'monthly_transactions.csv')