วิธีการชำระเงินที่บันทึกไว้
เรียนรู้วิธีการบันทึกและจัดการวิธีการชำระเงินอย่างปลอดภัยสำหรับการเรียกเก็บเงินแบบวนซ้ำ ประสบการณ์การจ่ายแบบคลิกเดียว และบริการสมาชิกซื้อ คู่มือนี้ครอบคลุมการเก็บบัตร การจัดการ และ การเรียกเก็บเงินจากวิธีการชำระเงินที่บันทึกไว้
ภาพรวม
วิธีการชำระเงินที่บันทึกไว้ช่วยให้คุณสามารถเรียกเก็บเงินจากลูกค้าได้โดยไม่ต้องเก็บรายละเอียดการชำระเงินสำหรับแต่ละรายการ นี่เป็นสิ่งจำเป็นสำหรับ:
- การเรียกเก็บเงินสมาชิกซื้อแบบวนซ้ำ
- ประสบการณ์การจ่ายแบบคลิกเดียว
- บริการการต่ออายุอัตโนมัติ
- แพลตฟอร์มสมาชิกซื้อ
- การเรียกเก็บเงินตามการใช้งาน
ประโยชน์หลัก
- ปรับปรุงการแปลง - ลดแรงเสียดทานด้วยการชำระเงินแบบคลิกเดียว
- การเก็บรักษากลุ่มลูกค้าที่ดีขึ้น - การต่ออายุสมาชิกซื้อที่ราบรื่น
- ลดขอบเขต PCI - Tokens แทนข้อมูลบัตรดิบ
- ตัวเลือกการชำระเงินหลายรูปแบบ - รองรับบัตรที่บันทึกไว้หลายใบ
- ความปลอดภัยที่เพิ่มขึ้น - 3D Secure ในการใช้ครั้งแรก การเรียกเก็บเงินครั้งต่อไปเร็วขึ้น
ความปลอดภัยและการปฏิบัติตามข้อกำหนด
Omise จัดการการเก็บบัตรอย่างปลอดภัย:
- บัตรถูก tokenized และเข้ารหัส
- โครงสร้างพื้นฐาน PCI DSS ระดับ 1 ที่ปฏิบัติตามข้อกำหนด
- ไม่มีข้อมูลบัตรดิบแตะเซิร์ฟเวอร์ของคุณ
- ระบบเก็บรักษาบัตรที่ปลอดภัย
- การปฏิบัติตามข้อกำหนดการป้องกันข้อมูล
การบันทึกวิธีการชำระเงิน
บันทึกบัตรพร้อมการสร้างลูกค้า
const omise = require('omise')({
secretKey: 'skey_test_123'
});
// Create customer with card
const customer = await omise.customers.create({
email: 'john@example.com',
description: 'John Doe',
card: 'tokn_test_123456' // Token from client-side
});
console.log('Customer ID:', customer.id);
console.log('Default Card:', customer.default_card);
import omise
omise.api_secret = 'skey_test_123'
# Create customer with card
customer = omise.Customer.create(
email='john@example.com',
description='John Doe',
card='tokn_test_123456'
)
print(f'Customer ID: {customer.id}')
print(f'Default Card: {customer.default_card}')
<?php
$omise = new Omise(['secretKey' => 'skey_test_123']);
$customer = $omise['customers']->create([
'email' => 'john@example.com',
'description' => 'John Doe',
'card' => 'tokn_test_123456'
]);
echo "Customer ID: " . $customer['id'] . "\n";
echo "Default Card: " . $customer['default_card'];
curl https://api.omise.co/customers \
-u skey_test_123: \
-d "email=john@example.com" \
-d "description=John Doe" \
-d "card=tokn_test_123456"
เพิ่มบัตรให้กับลูกค้าที่มีอยู่
// Add additional card
await omise.customers.update('cust_test_123456', {
card: 'tokn_test_new_card'
});
// Retrieve updated customer
const customer = await omise.customers.retrieve('cust_test_123456');
console.log(`Total cards: ${customer.cards.total}`);
console.log('Cards:', customer.cards.data.map(c =>
`${c.brand} •••• ${c.last_digits}`
));
# Add additional card
customer = omise.Customer.retrieve('cust_test_123456')
customer.update(card='tokn_test_new_card')
# Retrieve updated customer
customer.reload()
print(f'Total cards: {customer.cards.total}')
for card in customer.cards.data:
print(f'{card.brand} •••• {card.last_digits}')
curl https://api.omise.co/customers/cust_test_123456 \
-u skey_test_123: \
-d "card=tokn_test_new_card"
ขั้นตอนการบันทึกบัตรฝั่งไคลเอนต์
// Frontend: Create token
async function saveCard() {
const cardData = {
name: document.getElementById('name').value,
number: document.getElementById('number').value,
expiration_month: document.getElementById('month').value,
expiration_year: document.getElementById('year').value,
security_code: document.getElementById('cvv').value
};
// Create token
const token = await Omise.createToken('card', cardData);
// Send token to backend
const response = await fetch('/api/save-card', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
token: token.id,
customerId: 'cust_test_123456'
})
});
return response.json();
}
// Backend: Save card to customer
app.post('/api/save-card', async (req, res) => {
try {
const { token, customerId } = req.body;
const customer = await omise.customers.update(customerId, {
card: token
});
res.json({
success: true,
cardId: customer.cards.data[0].id
});
} catch (error) {
res.status(400).json({
success: false,
error: error.message
});
}
});
การจัดการวิธีการชำระเงิน
รายการบัตรที่บันทึกไว้
const customer = await omise.customers.retrieve('cust_test_123456');
customer.cards.data.forEach(card => {
console.log(`Card ID: ${card.id}`);
console.log(`Brand: ${card.brand}`);
console.log(`Last 4: ${card.last_digits}`);
console.log(`Expires: ${card.expiration_month}/${card.expiration_year}`);
console.log(`Default: ${card.id === customer.default_card}`);
console.log('---');
});
customer = omise.Customer.retrieve('cust_test_123456')
for card in customer.cards.data:
print(f'Card ID: {card.id}')
print(f'Brand: {card.brand}')
print(f'Last 4: {card.last_digits}')
print(f'Expires: {card.expiration_month}/{card.expiration_year}')
print(f'Default: {card.id == customer.default_card}')
print('---')
ตั้งค่าบัตรเริ่มต้น
// Change default card
await omise.customers.update('cust_test_123456', {
default_card: 'card_test_789012'
});
// Verify change
const customer = await omise.customers.retrieve('cust_test_123456');
console.log('New default:', customer.default_card);
# Change default card
customer = omise.Customer.retrieve('cust_test_123456')
customer.update(default_card='card_test_789012')
# Verify change
customer.reload()
print(f'New default: {customer.default_card}')
curl https://api.omise.co/customers/cust_test_123456 \
-u skey_test_123: \
-d "default_card=card_test_789012"
นำบัตรออก
// Delete specific card
await omise.customers.destroyCard('cust_test_123456', 'card_test_789012');
// Verify removal
const customer = await omise.customers.retrieve('cust_test_123456');
console.log(`Remaining cards: ${customer.cards.total}`);
# Delete specific card
customer = omise.Customer.retrieve('cust_test_123456')
customer.destroy_card('card_test_789012')
# Verify removal
customer.reload()
print(f'Remaining cards: {customer.cards.total}')
curl https://api.omise.co/customers/cust_test_123456/cards/card_test_789012 \
-u skey_test_123: \
-X DELETE
ดึงข้อมูลบัตรเฉพาะ
// Get card details
const customer = await omise.customers.retrieve('cust_test_123456');
const card = customer.cards.data.find(c => c.id === 'card_test_789012');
console.log({
id: card.id,
brand: card.brand,
lastDigits: card.last_digits,
expiry: `${card.expiration_month}/${card.expiration_year}`,
name: card.name,
fingerprint: card.fingerprint
});
curl https://api.omise.co/customers/cust_test_123456/cards/card_test_789012 \
-u skey_test_123:
การเรียกเก็บเงินจากวิธีการชำระเงินที่บันทึกไว้
เรียกเก็บเงินจากบัตรเริ่มต้น
// Charge customer's default card
const charge = await omise.charges.create({
customer: 'cust_test_123456',
amount: 100000, // 1,000.00 THB
currency: 'THB',
description: 'Monthly subscription - January 2024'
});
console.log('Charge status:', charge.status);
console.log('Card used:', charge.card.last_digits);
# Charge customer's default card
charge = omise.Charge.create(
customer='cust_test_123456',
amount=100000,
currency='THB',
description='Monthly subscription - January 2024'
)
print(f'Charge status: {charge.status}')
print(f'Card used: {charge.card.last_digits}')
เรียกเก็บเงินจากบัตรเฉพาะ
// Charge a specific saved card
const charge = await omise.charges.create({
customer: 'cust_test_123456',
card: 'card_test_789012',
amount: 100000,
currency: 'THB',
description: 'Purchase with backup card'
});
# Charge a specific saved card
charge = omise.Charge.create(
customer='cust_test_123456',
card='card_test_789012',
amount=100000,
currency='THB',
description='Purchase with backup card'
)
การเรียกเก็บเงินโดยไม่ใช้ CVV
// Saved cards don't require CVV for subsequent charges
const charge = await omise.charges.create({
customer: 'cust_test_123456',
amount: 50000,
currency: 'THB',
description: 'Auto-renewal payment'
});
// First charge may require 3D Secure
if (charge.authorize_uri) {
console.log('3DS required:', charge.authorize_uri);
// Redirect user for authentication
}
// Subsequent charges typically don't require 3DS
console.log('Charge successful:', charge.status === 'successful');
การเรียกเก็บเงินแบบสำรอง
async function chargeWithFallback(customerId, amount, currency) {
const customer = await omise.customers.retrieve(customerId);
const cards = customer.cards.data;
// Try primary card first
try {
return await omise.charges.create({
customer: customerId,
card: customer.default_card,
amount,
currency,
description: 'Payment attempt - primary card'
});
} catch (primaryError) {
console.log('Primary card failed:', primaryError.message);
// Try backup cards
for (const card of cards) {
if (card.id === customer.default_card) continue;
try {
return await omise.charges.create({
customer: customerId,
card: card.id,
amount,
currency,
description: 'Payment attempt - backup card'
});
} catch (backupError) {
console.log(`Backup card ${card.last_digits} failed`);
}
}
throw new Error('All payment methods failed');
}
}