การเก็บรวบรวมข้อมูลบัตร
ใช้ Omise.js เพื่อเก็บรวบรวมรายละเอียดบัตรอย่างปลอดภัยโดยตรงจากเว็บไซต์ของคุณ โดยเก็บข้อมูลที่ละเอียดอ่อนออกจากเซิร์ฟเวอร์ของคุณ
ภาพรวม
Omise.js เป็นไลบรารี JavaScript ฝั่งไคลเอนต์ที่ช่วยให้สามารถทำ tokenization บัตรอย่างปลอดภัยได้โดยตรงจากเว็บไซต์ของคุณ วิธีการนี้ช่วยให้มั่นใจว่าข้อมูลการชำระเงินที่ละเอียดอ่อนจะไม่สัมผัสเซิร์ฟเวอร์ของคุณ ลดภาระการปฏิบัติตาม PCI ของคุณอย่างมาก
วิธีการทำงานของ Tokenization
ประโยชน์ หลัก:
- ข้อมูลบัตรไปตรงยังเซิร์ฟเวอร์ที่ได้รับการรับรอง PCI ของ Omise
- เซิร์ฟเวอร์ของคุณรับเพียง token ใช้ครั้งเดียว
- ลดข้อกำหนดการปฏิบัติตาม PCI
- เพิ่มความปลอดภัยให้กับลูกค้าของคุณ
ขั้นตอนการใช้งาน
ขั้นตอนที่ 1: รวมไลบรารี Omise.js
เพิ่มสคริปต์ Omise.js ก่อนแท็กปิด </body>:
<script src="https://cdn.omise.co/omise.js"></script>
Omise.js ต้องใช้ HTTPS บนหน้าเช็คเอาต์ของคุณ ใช้ TLS 1.2 หรือสูงกว่าเพื่อความปลอดภัยที่เหมาะสม
ขั้นตอนที่ 2: สร้างฟอร์มการชำระเงิน
สร้างฟอร์ม HTML ของคุณด้วยฟิลด์ป้อนข้อมูลบัตร:
<form id="payment-form">
<div class="form-group">
<label for="card-name">ชื่อผู้ถือบัตร</label>
<input type="text" id="card-name" placeholder="สมชาย ใจดี" required />
</div>
<div class="form-group">
<label for="card-number">หมายเลขบัตร</label>
<input type="text" id="card-number" placeholder="4242 4242 4242 4242" required />
</div>
<div class="form-row">
<div class="form-group">
<label for="expiry-month">เดือนหมดอายุ</label>
<input type="text" id="expiry-month" placeholder="12" maxlength="2" required />
</div>
<div class="form-group">
<label for="expiry-year">ปีหมดอายุ</label>
<input type="text" id="expiry-year" placeholder="2027" maxlength="4" required />
</div>
<div class="form-group">
<label for="cvv">CVV</label>
<input type="text" id="cvv" placeholder="123" maxlength="4" required />
</div>
</div>
<!-- ที่อยู่สำหรับเรียกเก็บเงินเป็นตัวเลือก (ช่วยเพิ่มอัตราการอนุมัติ) -->
<div class="form-group">
<label for="postal-code">รหัสไปรษณีย์</label>
<input type="text" id="postal-code" placeholder="10110" />
</div>
<button type="submit" id="pay-button">ชำระเงินตอนนี้</button>
<div id="error-message" style="color: red;"></div>
</form>
ขั้นตอนที่ 3: ตั้งค่า Public Key ของคุณ
เริ่มต้น Omise.js ด้วย public key ของคุณ:
Omise.setPublicKey("pkey_test_YOUR_PUBLIC_KEY");
ใช้ test keys (pkey_test_...) ระหว่างการพัฒนาและ live keys (pkey_...) ในการผลิต อย่าใช้ live keys ในสภาพแวดล้อมทดสอบ!
ขั้นตอนที่ 4: จัดการการส่งฟอร์ม
สร้าง token เมื่อส่งฟอร์ม:
document.getElementById('payment-form').addEventListener('submit', function(e) {
e.preventDefault();
// ปิดการใช้งานปุ่มส่งเพื่อป้องกันการส่งซ้ำ
const payButton = document.getElementById('pay-button');
payButton.disabled = true;
payButton.textContent = 'กำลังประมวลผล...';
// สร้าง card token
Omise.createToken("card", {
name: document.getElementById('card-name').value,
number: document.getElementById('card-number').value,
expiration_month: parseInt(document.getElementById('expiry-month').value),
expiration_year: parseInt(document.getElementById('expiry-year').value),
security_code: document.getElementById('cvv').value,
postal_code: document.getElementById('postal-code').value
}, function(statusCode, response) {
if (statusCode === 200) {
// สำเร็จ! สร้าง Token แล้ว
handleToken(response.id);
} else {
// เกิดข้อผิดพลาด
handleError(response.message);
payButton.disabled = false;
payButton.textContent = 'ชำระเงินตอนนี้';
}
});
});
function handleToken(token) {
// ส่ง token ไปยังเซิร์ฟเวอร์ของคุณ
fetch('/checkout', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// รวม CSRF token เพื่อความปลอดภัย (ขึ้นอยู่กับเฟรมเวิร์ก)
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]')?.content
},
body: JSON.stringify({
token: token,
amount: 10025, // จำนวนเงินในหน่วยที่เล็กที่สุด (เช่น สตางค์)
currency: 'thb'
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
// เปลี่ยนเส้นทางไปยังหน้าความสำเร็จ
window.location.href = '/payment-success';
} else {
handleError(data.error);
}
})
.catch(error => {
handleError('เกิดข้อผิดพลาดของเครือข่าย');
});
}
function handleError(message) {
document.getElementById('error-message').textContent = message;
const payButton = document.getElementById('pay-button');
payButton.disabled = false;
payButton.textContent = 'ชำระเงินตอนนี้';
}
ขั้นตอนที่ 5: ประมวลผลการชำระเงินบนเซิร์ฟเวอร์
รับ token บนเซิร์ฟเวอร์ของคุณและสร้างการเรียกเก็บเงิน:
- Node.js
- PHP
- Python
const express = require('express');
const omise = require('omise')({
secretKey: 'skey_test_YOUR_SECRET_KEY'
});
app.post('/checkout', async (req, res) => {
try {
const { token, amount, currency } = req.body;
const charge = await omise.charges.create({
amount: amount,
currency: currency,
card: token
});
if (charge.status === 'successful') {
res.json({ success: true, chargeId: charge.id });
} else {
res.json({ success: false, error: charge.failure_message });
}
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
});
<?php
require_once 'vendor/autoload.php';
define('OMISE_SECRET_KEY', 'skey_test_YOUR_SECRET_KEY');
$token = $_POST['token'];
$amount = $_POST['amount'];
$currency = $_POST['currency'];
try {
$charge = OmiseCharge::create(array(
'amount' => $amount,
'currency' => $currency,
'card' => $token
));
if ($charge['status'] == 'successful') {
echo json_encode(['success' => true, 'chargeId' => $charge['id']]);
} else {
echo json_encode(['success' => false, 'error' => $charge['failure_message']]);
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
?>
from flask import Flask, request, jsonify
import omise
omise.api_secret = 'skey_test_YOUR_SECRET_KEY'
@app.route('/checkout', methods=['POST'])
def checkout():
try:
data = request.get_json()
token = data['token']
amount = data['amount']
currency = data['currency']
charge = omise.Charge.create(
amount=amount,
currency=currency,
card=token
)
if charge.status == 'successful':
return jsonify({'success': True, 'chargeId': charge.id})
else:
return jsonify({'success': False, 'error': charge.failure_message})
except Exception as e:
return jsonify({'success': False, 'error': str(e)}), 500
แนวทาง ปฏิบัติที่ดีที่สุดด้านความปลอดภัย
1. อย่าเก็บข้อมูลบัตรบนเซิร์ฟเวอร์ของคุณ
อย่า บันทึก เก็บ หรือส่งข้อมูลบัตรดิบผ่านเซิร์ฟเวอร์ของคุณ ใช้ tokens เสมอ
// ❌ แย่: ส่งข้อมูลบัตรไปยังเซิร์ฟเวอร์ของคุณ
fetch('/checkout', {
body: JSON.stringify({
cardNumber: '4242424242424242', // อย่าทำอย่างนี้!
cvv: '123' // อย่าทำอย่างนี้!
})
});
// ✅ ดี: ส่งเฉพาะ token
fetch('/checkout', {
body: JSON.stringify({
token: 'tokn_test_...' // นี่ปลอดภัย!
})
});
2. ใช้ HTTPS ทุกที่
- ต้องใช้ TLS 1.2 ขั้นต่ำ
- ใช้กับทั้งไซต์ ไม่ใช่แค่เช็คเอาต์
- ใช้ Let's Encrypt สำหรับใบรับรองฟรี
3. ใช้ CSP Headers
<meta http-equiv="Content-Security-Policy"
content="script-src 'self' https://cdn.omise.co;">
4. ปิด Analytics บนเช็คเอาต์
ป้องกันการจับภาพข้อมูลบัตรโดยไม่ตั้งใจ:
// ปิด Google Analytics บนหน้าเช็คเอาต์
window['ga-disable-UA-XXXXXX-Y'] = true;
5. ใช้การป้องกัน CSRF
รวม CSRF tokens ในคำขอการชำระเงินเสมอเพื่อป้องกันการโจมตี cross-site request forgery:
// ฝั่งไคลเอนต์: รวม CSRF token ในคำขอ
fetch('/checkout', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').content
},
body: JSON.stringify({ token: tokenId })
});