iT邦幫忙

2023 iThome 鐵人賽

DAY 20
0
WordPress

透過WordPress架設電商網站,並串接管理後台系列 第 20

«D20»整理 WooCommerce Webhook 資料

  • 分享至 

  • xImage
  •  

提醒個,WooCommerce Webhook 有一個 Secret,但我還不知道怎麼操作(過幾天研究完會更新),在正式公開網站之前,請務必確認您的後端程式碼可以判別此筆 Webhook 資料是從 WooCommerce 來的,不然隨便 POST 到你伺服器的資料你都會接收處理,可能會有受到攻擊的風險

嗨,取得資料之後要來整理哪些東西是要放上 Ragic 的啦!

這個是我從 webhook.site 取得的 Raw Data

{"id":41,"parent_id":0,"status":"on-hold","currency":"USD","version":"8.1.1","prices_include_tax":false,"date_created":"2023-09-26T18:25:27","date_modified":"2023-09-26T18:25:27","discount_total":"0.00","discount_tax":"0.00","shipping_total":"120.00","shipping_tax":"0.00","cart_tax":"0.00","total":"920.00","total_tax":"0.00","customer_id":1,"order_key":"wc_order_xxxxx","billing":{"first_name":"test","last_name":"1","company":"1","address_1":"1","address_2":"1","city":"1","state":"1","postcode":"1","country":"TW","email":"123456789@nccu.edu.tw","phone":"1"},"shipping":{"first_name":"test","last_name":"1","company":"1","address_1":"1","address_2":"1","city":"1","state":"1","postcode":"1","country":"TW","phone":""},"payment_method":"bacs","payment_method_title":"Direct bank transfer","transaction_id":"","customer_ip_address":"140.000.000.1","customer_user_agent":"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit\/605.1.15 (KHTML, like Gecko) Version\/16.5.1 Safari\/605.1.15","created_via":"checkout","customer_note":"","date_completed":null,"date_paid":null,"cart_hash":"x12u340972wjijjpajxkjasdweqcrl","number":"41","meta_data":[{"id":454,"key":"is_vat_exempt","value":"no"}],"line_items":[{"id":13,"name":"Test product","product_id":31,"variation_id":0,"quantity":1,"tax_class":"","subtotal":"800.00","subtotal_tax":"0.00","total":"800.00","total_tax":"0.00","taxes":[],"meta_data":[{"id":112,"key":"_reduced_stock","value":"1","display_key":"_reduced_stock","display_value":"1"}],"sku":"p-0001","price":800,"image":{"id":"30","src":"https:\/\/learn.gabriel.cyou\/wp-content\/uploads\/2023\/09\/photo-214fsa5-scaled.webp"},"parent_name":null}],"tax_lines":[],"shipping_lines":[{"id":14,"method_title":"Flat rate","method_id":"flat_rate","instance_id":"1","total":"120.00","total_tax":"0.00","taxes":[],"meta_data":[{"id":111,"key":"Items","value":"Test product × 1","display_key":"Items","display_value":"Test product × 1"}]}],"fee_lines":[],"coupon_lines":[],"refunds":[],"payment_url":"https:\/\/learn.gabriel.cyou\/checkout\/order-pay\/41\/?pay_for_order=true&key=wc_order_xxxxxx","is_editable":true,"needs_payment":false,"needs_processing":true,"date_created_gmt":"2023-09-26T10:25:27","date_modified_gmt":"2023-09-26T10:25:27","date_completed_gmt":null,"date_paid_gmt":null,"currency_symbol":"$","_links":{"self":[{"href":"https:\/\/learn.gabriel.cyou\/wp-json\/wc\/v3\/orders\/1"}],"collection":[{"href":"https:\/\/learn.gabriel.cyou\/wp-json\/wc\/v3\/orders"}],"customer":[{"href":"https:\/\/learn.gabriel.cyou\/wp-json\/wc\/v3\/customers\/1"}]}}

偷懶直接都給 ChatGPT

我需要的有 billing_first_name (我有在後台隱藏 last name 以符合台灣人使用習慣,所以我的 first_name label 是放「姓名」)

# 會用到 json 模組
import json

# 記得把取得的資料變成字串,然後用 loads 變成字典
data = json.loads('This_is_your_data_received')

# 提取所需字段
date_created = data.get('date_created')
discount_total = data.get('discount_total')
shipping_total = data.get('shipping_total')

# 因為 line_items 是一個列表,所以我們只取第一個項目的價格
line_item_price = data.get('line_items')[0].get('price')

billing = data.get('billing')
billing_first_name = billing.get('first_name')
billing_company = billing.get('company')
billing_address_1 = billing.get('address_1')
billing_city = billing.get('city')
billing_state = billing.get('state')
billing_country = billing.get('country')
billing_email = billing.get('email')
billing_phone = billing.get('phone')

sku = data.get('line_items')[0].get('sku')
customer_user_agent = data.get('customer_user_agent')
order_key = data.get('order_key')

# 如果需要,您可以 Print 或 return 這些值
print(date_created, discount_total, shipping_total, line_item_price, billing_first_name, billing_company, billing_address_1, billing_city, billing_state, billing_country, billing_email, billing_phone, sku, customer_user_agent, order_key)
print(
    f"Date Created: {date_created}\n"
    f"Discount Total: {discount_total}\n"
    f"Shipping Total: {shipping_total}\n"
    f"Line Item Price: {line_item_price}\n"
    f"Billing First Name: {billing_first_name}\n"
    f"Billing Company: {billing_company}\n"
    f"Billing Address 1: {billing_address_1}\n"
    f"Billing City: {billing_city}\n"
    f"Billing State: {billing_state}\n"
    f"Billing Country: {billing_country}\n"
    f"Billing Email: {billing_email}\n"
    f"Billing Phone: {billing_phone}\n"
    f"SKU: {sku}\n"
    f"Customer User Agent: {customer_user_agent}\n"
    f"Order Key: {order_key}\n"
)

接著很無腦的把資料填入要傳送到 Ragic 的那個 data

def new_order(date_created, discount_total, shipping_total, line_item_price, billing_first_name, billing_company, billing_address_1, billing_city, billing_state, billing_country, billing_email, billing_phone, sku, customer_user_agent, order_key, order_id):
    data = {
        表格編號: date_created,
        表格編號 2: discount_total,
        表格編號 3: shipping_total,
        表格編號 4: line_item_price,
        表格編號 5: billing_first_name,
        表格編號 6: billing_company,
        表格編號 7: billing_address_1,
        表格編號 8: billing_city,
        表格編號 9: billing_state,
        表格編號 10: billing_country,
        表格編號 11: billing_email,
        表格編號 12: billing_phone,
        表格編號 13: sku,
        表格編號 14: customer_user_agent,
        表格編號 15: order_key,
    }
    ENDPOINT = f'https://{ragic_BASE_URL}/formsx/x'  # 在這個表單新增資料
    response = requests.post(ENDPOINT, params={'api': '','v': 3}, json=data, headers={'Authorization': 'Basic '+ragic_API_KEY})
    return f'Success sent to Ragic {response.text}', 200

這個是 Webhook 參考影片 https://www.youtube.com/watch?v=YaoUvgDMrY4


上一篇
«D19»取得 WooCommerce Webhook 的資料
下一篇
«D21»(金流篇)串接 TapPay 金流—Direct Pay - iFrame
系列文
透過WordPress架設電商網站,並串接管理後台30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言