Thuta Learning
API Integration & Webhooks
IntermediateWeb Developmentintermediate

Webhook ဥပမာတစ်ခု — Payment ပြီးဆုံးခြင်း

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Webhook ဥပမာတစ်ခု — Payment ပြီးဆုံးခြင်း concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • Diagram ကို ဖတ်ပြီး request/response (သို့) event flow ဘယ်လိုစီးဆင်းသလဲ ခြေရာခံနိုင်ရန်
  • ကိုယ့် ကိုယ်ပိုင် API integration အတွက် ဘယ်လို အသုံးချသင့်သလဲ ရှင်းပြနိုင်ရန်

နားလည်ထားရမယ့် အချက်

ဥပမာတစ်ခု စဉ်းစားကြည့်ပါ — customer တစ်ယောက်က သင့် checkout page ပေါ်မှာ ငွေပေးချေမှု ပြီးဆုံးသွားပေမယ့် အဲဒီ charge ကို တကယ်လုပ်ဆောင်ပေးတာက သီးခြား payment provider တစ်ခုပါ။ Charge အောင်မြင်တဲ့ အချိန်မှာ provider ရဲ့ server က ချက်ချင်းသိပါတယ်၊ ဒါပေမဲ့ သင့် server က မသိပါဘူး။

Webhook model က ဒါကို ရှင်းရှင်းလင်းလင်း ဖြေရှင်းပေးပါတယ် — payment provider က event ကို သူ့ဘက်မှာ စောင့်ကြည့်နေပြီး၊ ငွေပေးချေမှု ပြီးဆုံးတဲ့ အခိုက်အတန့်မှာ ကြိုတင် register လုပ်ထားတဲ့ URL ဆီကို payment details ပါအောင် POST ကို ပို့လိုက်ပါတယ်။

Customer ငွေပေးချေသည်

Customer က သင့်ဆိုက်ပေါ်မှာ checkout ပြီးဆုံးပါတယ်၊ charge ကို payment provider က တကယ်လုပ်ဆောင်ပေးပါတယ်။

Provider က ပြီးဆုံးကြောင်း detect လုပ်သည်

Payment provider ရဲ့ system တွေကိုယ်တိုင်က charge အောင်မြင်ကြောင်း အတည်ပြုပါတယ် — ဒီအဆင့်ကို သင့် server မမြင်နိုင်ပါဘူး။

Provider က webhook ပို့သည်

Provider က ကြိုတင် register လုပ်ထားတဲ့ URL ဆီကို payment details ပါတဲ့ HTTP POST ကို ပို့လိုက်ပါတယ်။

သင့် server က order ကို update လုပ်သည်

သင့် handler က payload ကို ဖတ်ပြီး database ထဲမှာ order ကို paid အဖြစ် မှတ်သားပါတယ် — polling လုံးဝမလိုပါဘူး။

ဒါကို alternative တစ်ခုနဲ့ နှိုင်းယှဉ်ကြည့်ပါ — polling ဆိုတာ သင့် server က ထပ်ကာထပ်ကာ request ပို့နေရတာပါ။ Event မဖြစ်ခင် request တွေ ဖြုန်းတီးနေပြီး၊ ဖြစ်ပြီးတဲ့နောက်တောင် polling interval တစ်ခုစာအထိ စောင့်နေရဦးမယ်။

ဒါကြောင့် payment provider၊ chat platform၊ CI system တွေအားလုံးက ဒီလို event မျိုးအတွက် webhook ကို ပိုနှစ်သက်ကြတာပါ။ Trade-off ကတော့ webhook တစ်ခု လက်ခံရတာက verify လုပ်ဖို့နဲ့ delivery ကို မှန်ကန်စွာ handle လုပ်ဖို့ တာဝန်ရှိစေတယ်ဆိုတာပါပဲ။

text
PAYMENT WEBHOOK SEQUENCE
------------------------
-----
1. Customer pays on your checkout page
        |
        v
2. Payment Provider detects the charge completed
        |
        v
3. Payment Provider sends POST /webhook to Your Server
        |
        v
4. Your Server updates order status to "paid" in the DB

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

အောက်က code က အဆင့် လေးဆင့် အစဉ်လိုက်ကို model လုပ်ထားပါတယ် — payment ပြီးဆုံးကြောင်း claim လုပ်တဲ့ webhook payload တစ်ခု ရောက်လာပြီး သင့် handler က ဘာလုပ်မလဲ ဆုံးဖြတ်ပါတယ်။ Order ID နဲ့ amount ကို ထုတ်ယူပြီး status update အတိအကျကို ပြန်ပေးပါတယ်။

Function က ဘာမှ fetch မလုပ်ဘူး၊ payment ဖြစ်လား မဖြစ်လား ဘယ်သူ့ကိုမှ မမေးဘူးဆိုတာ သတိပြုပါ — ရရှိထားတဲ့ payload ကို ယုံပြီး တုံ့ပြန်တာပါ။ ဒါကပဲ efficiency အကျိုးအမြတ်တစ်ခုလုံးပါ — outbound request လုံးဝမလိုပါဘူး။

Function က ဘာမှမလုပ်ခင် event.type ကို စစ်တာကိုလည်း သတိပြုပါ။ Payload တိုင်းကို payment ပြီးဆုံးမှုလို့ မယူဆဘဲ ရှင်းလင်းတဲ့ အကြောင်းပြချက် string နဲ့ ဘေးကင်းစွာ ထွက်ခွာတတ်တဲ့ အလေ့အကျင့်ကို အခုကတည်းက တည်ဆောက်ထားသင့်ပါတယ်။

အတူတူ စမ်းရေးကြည့်မယ်

javascript
function handlePaymentWebhook(payload) {
  const event = JSON.parse(payload);
  if (event.type !== "payment.completed") {
    return { updated: false, reason: `Unhandled event type: ${event.type}` };
  }
  const { orderId, amount, currency } = event.data;
  return {
    updated: true,
    orderId,
    newStatus: "paid",
    message: `Order ${orderId} marked as paid (${amount} ${currency}) via webhook`
  };
}

const incoming = JSON.stringify({
  type: "payment.completed",
  data: { orderId: "ord_482", amount: 4200, currency: "USD" }
});

console.log(handlePaymentWebhook(incoming));
You should see
{
  updated: true,
  orderId: 'ord_482',
  newStatus: 'paid',
  message: 'Order ord_482 marked as paid (4200 USD) via webhook'
}

၅ မိနစ် စမ်းကြည့်

handlePaymentWebhook ကို 'payment.refunded' event type ကိုပါ ကိုင်တွယ်နိုင်အောင် တိုးချဲ့ပါ — order ကို refunded အဖြစ် မှတ်သားမယ့် status update ကို ပြန်ပေးပါ။

သတိလေးတစ်ချက်

event.type ကို အရင်မစစ်ဘဲ webhook payload က မျှော်လင့်ထားတဲ့ event type ပဲ ဖြစ်နေမယ်လို့ ယူဆလိုက်ခြင်း။

နောက်သင်ခန်းစာမှာ ဖော်ပြမယ့် signature checking လိုမျိုး အခြား verification မရှိဘဲ webhook ရောက်တာနဲ့ ငွေပေးချေမှု တကယ်အောင်မြင်ပြီလို့ အာမခံအဖြစ် သဘောထားခြင်း။

Stripe Docs — Webhooks overviewAPI Integration & Webhooks

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • event.type ကို အရင်မစစ်ဘဲ webhook payload က မျှော်လင့်ထားတဲ့ event type ပဲ ဖြစ်နေမယ်လို့ ယူဆလိုက်ခြင်း။
  • နောက်သင်ခန်းစာမှာ ဖော်ပြမယ့် signature checking လိုမျိုး အခြား verification မရှိဘဲ webhook ရောက်တာနဲ့ ငွေပေးချေမှု တကယ်အောင်မြင်ပြီလို့ အာမခံအဖြစ် သဘောထားခြင်း။
  • API Tutorial (apiguide) ကို မလေ့လာရသေးရင် ဒီ course ကို စမလိုက်ခင် အရင် ပြီးအောင် လေ့လာထားသင့်ပါတယ် — ဒီ course က REST/HTTP/Auth အခြေခံတွေကို ထပ်မသင်ဘဲ webhook, testing, reliability, integration architecture တို့ကိုသာ ဆက်လက် တည်ဆောက်ပါတယ်။

လေ့ကျင့်ခန်း

handlePaymentWebhook ကို 'payment.refunded' event type ကိုပါ ကိုင်တွယ်နိုင်အောင် တိုးချဲ့ပါ — order ကို refunded အဖြစ် မှတ်သားမယ့် status update ကို ပြန်ပေးပါ။

You'll know it worked when: { updated: true, orderId: 'ord_482', newStatus: 'paid', message: 'Order ord_482 marked as paid (4200 USD) via webhook' }

Webhook ဥပမာတစ်ခု — Payment ပြီးဆုံးခြင်း | Thuta Learning