ตารางการชำระเงินแบบซ้ำ ๆ
เรียนรู้วิธีการสร้างและจัดการตารางการชำระเงินแบบซ้ำ ๆ สำหรับการเรียกเก็บเงินจากการสมัครสมาชิก สมาชิก และการเรียกเก็บเงินโดยอัตโนมัติ ตารางจะทำให้กระบวนการเรียกเก็บเงินจากลูกค้าในช่วงเวลาปกติโดยอัตโนมัติ
ภาพรวม
Schedules API ช่วยให้คุณสามารถทำให้การชำระเงินแบบซ้ำ ๆ เป็นอัตโนมัติโดยไม่ต้องมีการแทรกแซงด้วยตนเอง ยอดเยี่ยมสำหรับ:
- บริการสมัครสมาชิก (รายเดือน รายปี)
- ค่าสมาชิก
- การชำระเงินแบบผ่อนส่วน
- รอบการเรียกเก็บเงินตามการใช้งาน
- การบริจาคแบบซ้ำ ๆ
- การต่ออายุใบอนุญาต
ประโยชน์หลัก
- การเรียกเก็บเงินแบบอัตโนมัติ - ตั้งค่าครั้งเดียว การเรียกเก็บเงินจะเกิดขึ้นโดยอัตโนมัติ
- ตารางที่ยืดหยุ่น - ทุกวัน ทุกสัปดาห์ ทุกเดือน หรือช่วงเวลาที่กำหนดเอง
- ตรรมชาติการลองใหม่ - การจัดการล้มเหลวของการชำระเงินแบบสร้างขึ้น
- การจัดการที่ง่าย - หยุด ดำเนินการต่อ หรือยกเลิกตาราง
- การติดตามที่ครอบคลุม - ตรวจสอบการชำระเงินตามตารางทั้งหมด
- การรักษาลูกค้า - ลดอัตราการออกจากการทำให้การต่ออายุเป็นไปอย่างราบรื่น
วิธีการทำงานของตาราง
- สร้างตาราง พร้อมรายละเอียดลูกค้าและการชำระเงิน
- ตารางสร้างการชำระเงินโดยอัตโนมัติในวันที่ที่ระบุ
- ระบบพยายามชำระเงินให้กับวิธีการชำระเงินเริ่มต้นของลูกค้า
- Webhooks แจ้งให้คุณทราบเกี่ยวกับการชำระเงินที่สำเร็จ/ล้มเหลว
- ตารางจะดำเนินต่อไปจนกว่าจะสิ้นสุดหรือยกเลิก
การสร้างตาราง
การสร้างตารางพื้นฐาน
curl https://api.omise.co/schedules \
-u skey_test_123: \
-d "every=1" \
-d "period=month" \
-d "start_date=2024-02-01" \
-d "end_date=2024-12-31" \
-d "charge[customer]=cust_test_123456" \
-d "charge[amount]=100000" \
-d "charge[currency]=THB" \
-d "charge[description]=Monthly Premium Subscription"
const omise = require('omise')({
secretKey: 'skey_test_123'
});
const schedule = await omise.schedules.create({
every: 1,
period: 'month',
start_date: '2024-02-01',
end_date: '2024-12-31',
charge: {
customer: 'cust_test_123456',
amount: 100000,
currency: 'THB',
description: 'Monthly Premium Subscription'
}
});
console.log('รหัสตาราง:', schedule.id);
console.log('สถานะ:', schedule.status);
console.log('การเกิดขึ้นครั้งต่อไป:', schedule.next_occurrences_on[0]);
ช่วงเวลาของตาราง
// การเรียกเก็บเงินรายเดือน
const monthly = await omise.schedules.create({
every: 1,
period: 'month',
start_date: '2024-02-01',
charge: { /* ... */ }
});
// การเรียกเก็บเงินรายสัปดาห์
const weekly = await omise.schedules.create({
every: 1,
period: 'week',
start_date: '2024-02-01',
charge: { /* ... */ }
});
// การเรียกเก็บเงินรายวัน
const daily = await omise.schedules.create({
every: 1,
period: 'day',
start_date: '2024-02-01',
charge: { /* ... */ }
});
// ทุกไตรมาส (ทุก 3 เดือน)
const quarterly = await omise.schedules.create({
every: 3,
period: 'month',
start_date: '2024-02-01',
charge: { /* ... */ }
});
// การเรียกเก็บเงินรายปี
const annual = await omise.schedules.create({
every: 1,
period: 'year',
start_date: '2024-02-01',
charge: { /* ... */ }
});
การจัดการตาราง
ดึงตาราง
const schedule = await omise.schedules.retrieve('schd_test_123456');
console.log('ตาราง:', schedule.in_words);
console.log('สถานะ:', schedule.status);
console.log('การชำระเงินครั้งต่อไป:', schedule.next_occurrences_on);
console.log('การชำระเงินที่เสร็จสมบูรณ์:', schedule.occurrences.total);
แสดงรายการตารางทั้งหมด
// แสดงรายการพร้อมตัวกรอง
const schedules = await omise.schedules.list({
limit: 20,
offset: 0,
order: 'reverse_chronological'
});
schedules.data.forEach(schedule => {
console.log(`${schedule.id} - ${schedule.status} - ${schedule.in_words}`);
});
// ตัวกรองตามสถานะ
const activeSchedules = schedules.data.filter(
s => s.status === 'active'
);
หยุดตาราง
// หยุดตารางชั่วคราว
await omise.schedules.destroy('schd_test_123456');
// ตรวจสอบการหยุด
const schedule = await omise.schedules.retrieve('schd_test_123456');
console.log('สถานะ:', schedule.status);
ดูการเกิดขึ้นของตาราง
const schedule = await omise.schedules.retrieve('schd_test_123456');
const occurrences = schedule.occurrences.data;
occurrences.forEach(occurrence => {
console.log('วันที่:', occurrence.schedule_date);
console.log('สถานะ:', occurrence.status);
console.log('การชำระเงิน:', occurrence.result?.id);
console.log('---');
});
การกำหนดค่าตารางขั้นสูง
คิดค่าธรรมเนียมในวันที่เฉพาะ
// คิดค่าธรรมเนียมในวันที่เฉพาะของเดือน
const schedule = await omise.schedules.create({
every: 1,
period: 'month',
on: {
days_of_month: [15] // คิดค่าธรรมเนียมในวันที่ 15 ของแต่ละเดือน
},
start_date: '2024-02-15',
charge: {
customer: 'cust_test_123456',
amount: 100000,
currency: 'THB'
}
});
// วันหลายวันต่อเดือน
const bimonthly = await omise.schedules.create({
every: 1,
period: 'month',
on: {
days_of_month: [1, 15] // คิดค่าธรรมเนียมในวันที่ 1 และ 15
},
start_date: '2024-02-01',
charge: { /* ... */ }
});
ตารางพร้อมข้อมูลเมตา
const schedule = await omise.schedules.create({
every: 1,
period: 'month',
start_date: '2024-02-01',
charge: {
customer: 'cust_test_123456',
amount: 100000,
currency: 'THB',
description: 'Premium Subscription',
metadata: {
subscription_id: 'sub_12345',
plan: 'premium',
user_id: '67890',
billing_cycle: 'monthly'
}
}
});
วิธีการที่ดีที่สุด
1. สร้างตารางอย่างปลอดภัย
// ตรวจสอบลูกค้าก่อนสร้างตาราง
async function createScheduleSafely(customerId, scheduleData) {
const customer = await omise.customers.retrieve(customerId);
if (!customer.default_card) {
throw new Error('ลูกค้าไม่มีวิธีการชำระเงิน');
}
// ตรวจสอบการหมดอายุของการ์ด
const card = customer.cards.data.find(
c => c.id === customer.default_card
);
const expiry = new Date(
card.expiration_year,
card.expiration_month - 1,
1
);
if (expiry <= new Date()) {
throw new Error('วิธีการชำระเงินหมดอายุแล้ว');
}
return await omise.schedules.create({
...scheduleData,
charge: {
...scheduleData.charge,
customer: customerId
}
});
}
2. การจัดการข้อผิดพลาด
class ScheduleErrorHandler {
async createScheduleWithRetry(scheduleData, maxRetries = 3) {
let lastError;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await omise.schedules.create(scheduleData);
} catch (error) {
lastError = error;
console.log(`ความพยายาม ${attempt} ล้มเหลว:`, error.message);
if (error.code === 'customer_not_found') {
throw error;
}
if (attempt < maxRetries) {
await this.delay(1000 * attempt);
}
}
}
throw lastError;
}
}
คำถามที่พบบ่อย
Q: การชำระเงินครั้งแรกเกิดขึ้นเมื่อใด?
A: การชำระเงินครั้งแรกเกิดขึ้นในวันที่ start_date ที่คุณระบุ ตรวจสอบว่าอย่างน้อยจะเป็นวันในอนาคต 1 วัน
Q: ฉันสามารถเปลี่ยนตารางหลังจากสร้างได้หรือไม่?
A: ไม่ ตารางไม่เปลี่ยนแปลง เพื่อเปลี่ยนพารามิเตอร์ คุณต้องลบตารางเก่าและสร้างตารางใหม่
Q: จะเกิดอะไรขึ้นหากการชำระเงินตามตารางล้มเหลว?
A: คุณจะได้รับการแจ้งเตือน Webhook ตารางจะถูกดำเนินต่อไปเพื่อพยายามชำระเงินในอนาคต ใช้งานตรรมชาติการลองใหม่ในแอปพลิเคชันของคุณ
Q: ฉันควรจัดการ กับการชำระเงินจากการสมัครสมาชิกที่ล้มเหลวอย่างไร?
A: ใช้งานตรรมชาติการลองใหม่ (พร้อมการถอยหลัง) แจ้งให้ลูกค้าทราบ และให้วิธีง่ายๆ ในการอัปเดตข้อมูลการชำระเงิน พิจารณารอบระยะเวลา
แหล่งข้อมูลที่เกี่ยวข้อง
- การจัดการลูกค้า
- วิธีการชำระเงินที่บันทึกไว้
- Schedules API Reference
- Charges API Reference
- Webhooks
- การทดสอบ