← Back to list

การเขียนโปรแกรม 2C2P การชำระเงินออนไลน์ ด้วย python

เพื่อเชื่อมต่อระบบ ชำระเงินออนไลน์ 2C2P ด้วย Python, คุณสามารถใช้ 2C2P Payment Gateway API ที่พวกเขาให้ไว้ ซึ่งโดยทั่วไปจะมีการทำงานหลัก ๆ…

Dr. Pathasu Doungmala · 2025-06-06 03:33 · 0 claps · 0.9 min read
#python #2c2p #ชำระเงินออนไลน์
Open on Medium ↗
Wiki topics: FIN · Fintech & Banking

การเขียนโปรแกรม 2C2P การชำระเงินออนไลน์ ด้วย python

เพื่อเชื่อมต่อระบบ ชำระเงินออนไลน์ 2C2P ด้วย Python, คุณสามารถใช้ 2C2P Payment Gateway API ที่พวกเขาให้ไว้ ซึ่งโดยทั่วไปจะมีการทำงานหลัก ๆ ดังนี้:

  • สร้างฟอร์ม/คำขอชำระเงิน (Payment Request)
  • เข้ารหัสข้อมูลตามที่ 2C2P กำหนด (AES encryption + HMAC)
  • ส่งข้อมูลไปยัง Payment Gateway ของ 2C2P
  • รับการแจ้งเตือนผลการชำระเงิน (Payment Notification)
  • ตรวจสอบและยืนยันข้อมูล

✅ สิ่งที่คุณต้องเตรียม

  • Merchant ID (จาก 2C2P)
  • Secret Key และ IV (สำหรับเข้ารหัส)
  • Sandbox หรือ Production URL จาก 2C2P
  • Python 3 และไลบรารี pycryptodome, flask (หากต้องการทำเว็บ)

ตัวอย่างโค้ดพื้นฐานสำหรับสร้าง Request ด้วย Python

ติดตั้งไลบรารี:

pip install pycryptodome flask

🔐 ฟังก์ชันเข้ารหัส AES และสร้าง HMAC:

import base64
import hashlib
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
# ฟังก์ชันเข้ารหัส
def encrypt_payload(payload, secret_key, iv):
    cipher = AES.new(bytes.fromhex(secret_key), AES.MODE_CBC, bytes.fromhex(iv))
    encrypted = cipher.encrypt(pad(payload.encode(), AES.block_size))
    return base64.b64encode(encrypted).decode()
# ฟังก์ชันถอดรหัส (ใช้ตอนรับข้อมูลจาก 2C2P)
def decrypt_payload(encrypted_payload, secret_key, iv):
    cipher = AES.new(bytes.fromhex(secret_key), AES.MODE_CBC, bytes.fromhex(iv))
    decrypted = unpad(cipher.decrypt(base64.b64decode(encrypted_payload)), AES.block_size)
    return decrypted.decode()

📦 สร้างฟอร์มการชำระเงิน (HTML + Flask):

from flask import Flask, render_template_string, request, redirect
app = Flask(__name__)
@app.route('/')
def index():
    payload = '''
    {
        "merchantID":"JT01",
        "invoiceNo":"INV123456",
        "description":"สินค้า A",
        "amount":"10000",
        "currencyCode":"764",
        "paymentChannel":"ALL",
        "frontendReturnUrl":"https://yourdomain.com/return"
    }
    '''
    secret_key = "YOUR_SECRET_KEY_IN_HEX"
    iv = "YOUR_IV_IN_HEX"
    encrypted_payload = encrypt_payload(payload, secret_key, iv)
    html_form = f"""
    <form method="POST" action="https://sandbox-pgw.2c2p.com/payment/4.1/">
        <input type="hidden" name="paymentRequest" value="{encrypted_payload}">
        <button type="submit">จ่ายเงินด้วย 2C2P</button>
    </form>
    """
    return render_template_string(html_form)
if __name__ == '__main__':
    app.run(debug=True)

🔁 รับข้อมูลแจ้งผลการชำระเงิน

2C2P จะส่งข้อมูลกลับมาทาง URL ที่คุณระบุ เช่น /return หรือ /callback. คุณต้องถอดรหัสข้อมูลเพื่อตรวจสอบสถานะ:

@app.route('/return', methods=['POST'])
def payment_return():
    encrypted_response = request.form.get('paymentResponse')
    decrypted_response = decrypt_payload(encrypted_response, secret_key, iv)
    print("Payment Response:", decrypted_response)
    return "ชำระเงินเรียบร้อย"

🧪 หมายเหตุ:

merchantID, secretKey, iv จะได้จาก 2C2P โดยตรง

  • ใช้ sandbox ก่อนเพื่อทดสอบ (https://sandbox-pgw.2c2p.com/payment/4.1/)
  • ระวังเรื่อง encoding, padding, และการเข้ารหัส AES-128 CBC

หมายเหตุท้าย: หากคุณชอบบทความนี้อย่าลืมคลิก❤ด้านล่างเพื่อแนะนำและถ้าคุณมีคำถามใด ๆ แสดงความคิดเห็นและฉันจะพยายามอย่างดีที่สุดที่จะตอบ คุณสามารถติดตามฉันบน facebook page (https://www.facebook.com/nextsoftwarehousethailand/) website : www.nextsoftwarethailand.com และสามารถส่งอีเมลถึงฉัน


메타데이터
post_id
6b67386f3d2c
slug
การเขียนโปรแกรม-2c2p-การชำระเงินออนไลน์-ด้วย-python-6b67386f3d2c
url
https://medium.com/@dr-pathasu-doung/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%80%E0%B8%82%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B9%82%E0%B8%9B%E0%B8%A3%E0%B9%81%E0%B8%81%E0%B8%A3%E0%B8%A1-2c2p-%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%8A%E0%B8%B3%E0%B8%A3%E0%B8%B0%E0%B9%80%E0%B8%87%E0%B8%B4%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-python-6b67386f3d2c
canonical_url
https://medium.com/@dr-pathasu-doung/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%80%E0%B8%82%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B9%82%E0%B8%9B%E0%B8%A3%E0%B9%81%E0%B8%81%E0%B8%A3%E0%B8%A1-2c2p-%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%8A%E0%B8%B3%E0%B8%A3%E0%B8%B0%E0%B9%80%E0%B8%87%E0%B8%B4%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-python-6b67386f3d2c
author_url
https://medium.com/@dr-pathasu-doung
status
ok
fetched_at
2026-07-19 14:03:49