การชำระเงินแบบเนทีฟ

การผสานรวมเนทีฟกำหนดให้คุณสร้าง RESTful API ที่ Google สามารถเรียกใช้ เพื่อสร้างและจัดการเซสชันการชำระเงิน

ขั้นตอนโดยรวมมีดังนี้

  1. สร้างเซสชันการชำระเงิน: ผู้ใช้และตัวแทน (ไม่บังคับ) จะอยู่ในลูป เพิ่มสินค้าลงในเซสชัน
  2. ส่งต่อให้ UI ของ Google: เมื่อผู้ใช้ตัดสินใจที่จะชำระเงิน เอเจนต์ (หากมีส่วนร่วม) จะส่งต่อการควบคุมไปยัง UI ของ Google (ส่งข้อมูลเซสชันการชำระเงิน)
  3. การชำระเงินด้วยตนเอง: ตอนนี้ผู้ใช้จะโต้ตอบกับ UI ของ Google เท่านั้นเพื่อกรอก รายละเอียดที่ละเอียดอ่อนเกี่ยวกับการดำเนินการตามคำสั่งซื้อและการชำระเงิน แล้วส่งคำสั่งซื้อ เอเจนต์ไม่มีส่วนเกี่ยวข้องในส่วนนี้เพื่อให้มั่นใจถึงความแน่นอน
  4. การทำธุรกรรมให้เสร็จสมบูรณ์และการคืนสินค้า: UI ของ Google จะแสดงหน้า "ขอขอบคุณ" เพื่อยืนยัน คำสั่งซื้อ หรืออาจเปลี่ยนเส้นทางผู้ใช้กลับไปยังตัวแทน ซึ่งอาจได้รับการแจ้งเตือนเกี่ยวกับการซื้อที่เสร็จสมบูรณ์แล้ว

ขั้นตอนต่อไปนี้จะอธิบายปลายทาง API ที่คุณจะต้องติดตั้งใช้งานและ ลักษณะการทำงานของปลายทางเหล่านั้น

1. สร้างเซสชันการชำระเงิน

ปลายทางนี้อนุญาตให้สร้างเซสชันการชำระเงินที่มีผลิตภัณฑ์ ที่ผู้ใช้สนใจซื้อ

  • ปลายทาง: POST /checkout-sessions
  • ทริกเกอร์: ผู้ใช้คลิก "ซื้อ" ในผลิตภัณฑ์

คำขอ: Google จะส่งรายการโฆษณาและสกุลเงิน

{
  "line_items": [
    {
      "item": {
        "id": "product_12345", // Must match Product Feed ID
        "title": "Running Shoes"
      },
      "quantity": 1
    }
  ],
  "currency": "USD"
}

การตอบกลับ: คุณจะแสดงเซสชันที่เริ่มต้นพร้อมยอดรวม ภาษี (ประมาณการเบื้องต้น ) และความสามารถในการชำระเงิน คุณต้องใส่ลิงก์ไปยังข้อกำหนดในการให้บริการและนโยบายความเป็นส่วนตัว ซึ่งจะลิงก์ไว้ใต้ปุ่มชำระเงิน

{
  "ucp": {
    "version": "2026-01-11",
    "capabilities": [
      {
        "name": "dev.ucp.shopping.checkout",
        "version": "2026-01-11"
      },
      {
        "name": "dev.ucp.shopping.fulfillment",
        "version": "2026-01-11"
      }
    ]
  },
  "id": "gid://merchant.example.com/Checkout/session_abc123",
  "status": "incomplete",
  "line_items": [
    {
      "id": "line_1",
      "item": {
        "id": "product_12345",
        "title": "Running Shoes",
        "price": 10000
      },
      "quantity": 1,
      "base_amount": 10000,
      "subtotal": 10000,
      "total": 10000
    }
  ],
  "totals": [
    { "type": "subtotal", "amount": 10000 }, // in cents
    { "type": "tax", "amount": 0 },
    { "type": "total", "amount": 10000 }
  ],
  "payment": {
    "handlers": [
      {
        "id": "gpay",
        "name": "com.google.pay",
        "config": {
           "api_version": 2,
           "api_version_minor": 0,
           "merchant_info": {
             "merchant_id": "12345678901234567890",
             "merchant_name": "Example Merchant"
           },
           "allowed_payment_methods": [
             {
               "type": "CARD",
               "parameters": {
                 "allowed_auth_methods": ["PAN_ONLY", "CRYPTOGRAM_3DS"],
                 "allowed_card_networks": ["VISA", "MASTERCARD"]
               },
               "tokenization_specification": {
                 "type": "PAYMENT_GATEWAY",
                 "parameters": {
                   "gateway": "stripe",
                   "gateway_merchant_id": "exampleGatewayMerchantId"
                 }
               }
             }
           ]
        }
      }
    ]
  },
  "links": [
    { "type": "privacy_policy", "url": "https://m.com/privacy" },
    { "type": "terms_of_service", "url": "https://m.com/terms" }
  ]
}

2. รับเซสชันการชำระเงิน

ปลายทางนี้ช่วยให้ดึงข้อมูลเซสชันการชำระเงินได้

  • ปลายทาง: GET /checkout-sessions/{id}

คำขอ: Google จะส่งรหัสของเซสชันการชำระเงิน หากคุณใช้รหัสสากล (เช่น gid://merchant.example.com/Checkout/session_abc123) โปรดทราบว่ารหัส ในเส้นทางคำขอจะเป็นเพียงคอมโพเนนต์สุดท้ายของรหัสนี้ (เช่น session_abc123)

การตอบกลับ: คุณจะแสดงผลออบเจ็กต์การชำระเงินแบบเต็ม

3. อัปเดตเซสชันการชำระเงิน

ปลายทางนี้อนุญาตให้อัปเดตเซสชันการชำระเงิน เมื่ออัปเดตที่อยู่สำหรับจัดส่ง แล้ว ระบบจะต้องคำนวณใหม่และแสดงตัวเลือกภาษีและการจัดส่ง

  • ปลายทาง: PUT /checkout-sessions/{id}
  • ทริกเกอร์: ผู้ใช้เลือกหรือเปลี่ยนที่อยู่จัดส่ง อัปเดต ตัวเลือกการจัดส่ง หรืออัปเดตข้อมูลการชำระเงิน

คำขอ: Google จะส่งออบเจ็กต์การชำระเงินทั้งหมดพร้อมข้อมูลที่อัปเดต

{
  "id": "gid://merchant.example.com/Checkout/session_abc123",
  "buyer": {
      "first_name": "John",
      "last_name": "Doe",
      "email": "john@example.com"
  },
  "fulfillment": {
     "methods": [
       {
         "type": "shipping",
         "destinations": [
           {
             "id": "dest_1",
             "postal_code": "94043",
             "country": "US",
             "address_locality": "Mountain View",
             "address_region": "CA"
           }
         ],
         "selected_destination_id": "dest_1"
       }
     ]
  },
  "payment": {
    "selected_instrument_id": "pi_gpay_5678",
    "instruments": [
      {
        "id": "pi_gpay_5678",
        "handler_id": "gpay",
        "type": "card",
        "brand": "mastercard",
        "last_digits": "5678",
        "rich_text_description": "Google Pay •••• 5678"
      }
    ]
  }
  // ... other fields (line_items, currency, etc.)
}

การตอบกลับ: คุณคำนวณภาษีและตัวเลือกการจัดส่งใหม่ตามที่จำเป็น และ ส่งคืนออบเจ็กต์การชำระเงินทั้งหมด

{
  "id": "gid://merchant.example.com/Checkout/session_abc123",
  "totals": [
     { "type": "subtotal", "amount": 10000 },
     { "type": "shipping", "display_text": "Ground Shipping", "amount": 500 },
     { "type": "tax", "amount": 850 },
     { "type": "total", "amount": 11350 }
  ],

  // ... other fields (line_items, currency, etc.)

  "fulfillment": {
      "methods": [
          {
            "id": "method_shipping",
            "type": "shipping",
            "line_item_ids": ["line_1"],
            "selected_destination_id": "dest_1",
            "destinations": [
               {
                 "id": "dest_1",
                 "postal_code": "94043",
                 "country": "US",
                 "address_locality": "Mountain View",
                 "address_region": "CA"
               }
            ],
            "groups": [
              {
                "id": "group_1",
                "line_item_ids": ["line_1"],
                "selected_option_id": "ship_ground",
                "options": [
                  {
                    "id": "ship_ground",
                    "title": "Ground (3-5 days)",
                    "total": 500
                  },
                  {
                    "id": "ship_express",
                    "title": "Express (1-2 days)",
                    "total": 1500
                  }
                ]
              }
            ]
          }
      ]
  }
}

4. เซสชันการชำระเงินให้เสร็จสมบูรณ์

ปลายทางนี้ช่วยให้คุณทำเซสชันการชำระเงินให้เสร็จสมบูรณ์และสั่งซื้อได้ โดยควรแสดงเซสชันการชำระเงินที่เสร็จสมบูรณ์และมีข้อมูลคำสั่งซื้อ การประมวลผลการชำระเงินควรเริ่มหลังจากได้รับสายนี้

  • ปลายทาง: POST /checkout-sessions/{id}/complete
  • ทริกเกอร์: ผู้ใช้คลิก "สั่งซื้อ"

คำขอ: Google จะส่งโทเค็นการชำระเงิน (Blob ที่เข้ารหัส) จากผู้ให้บริการ (เช่น Google Pay) และสัญญาณความเสี่ยง เกี่ยวกับผู้ซื้อเพื่อให้คุณตรวจจับการประพฤติมิชอบได้ด้วยตนเอง

{
  "payment_data": {
    "id": "pi_gpay_5678",
    "handler_id": "gpay",
    "type": "card",
    "brand": "mastercard",
    "last_digits": "5678",
    "billing_address": {
      "postal_code": "94043",
      "address_country": "US"
    },
    "credential": {
       "type": "PAYMENT_GATEWAY",
       "token": "{\"signature\":\"...\",\"protocolVersion\":\"ECv2\"...}"
    }
  }
}

การตอบกลับ: คุณจะแสดงออบเจ็กต์การชำระเงินแบบเต็มซึ่งระบุว่าคำสั่งซื้อ เสร็จสมบูรณ์แล้ว รวมถึงรหัสคำสั่งซื้อและ URL เพอร์มาลิงก์ไปยังคำสั่งซื้อ

{
  "ucp": {
      "version": "2026-01-11",
      "capabilities": [...]
  },
  "id": "gid://merchant.example.com/Checkout/session_abc123",
  "status": "completed",

  // ... other fields (line_items, currency, etc.)

  "order_id": "gid://merchant.example.com/Order/789",
  "order_permalink_url": "https://merchant.example.com/orders/789"
}

5. ยกเลิกเซสชันการชำระเงิน

ปลายทางนี้จะยกเลิกเซสชันการชำระเงิน

  • ปลายทาง: POST /checkout-sessions/{id}/cancel

คำขอ: Google จะส่งรหัสของเซสชันการชำระเงิน

การตอบกลับ: คุณส่งคืนออบเจ็กต์การชำระเงินทั้งหมดพร้อมสถานะที่อัปเดตเป็น canceled