Meldiron / appwrite-webhook-proxy

A simple HTTP server behaving as proxy between webhooks and Appwrite Functions.
30 stars 2 forks source link

Garbled output with new lines and quotes #7

Open mandolf0 opened 1 year ago

mandolf0 commented 1 year ago

Thank you for this Container.

However, I cannot wrap my head around parsing the output from the cloud function. The fn spits out the response in a format that I can't seem to tame. It must be the json_encode used in the Swoole curl.

I could not get this to be readable with Dart or NodeJS. Any tips?


  method: "POST",
  body: "{\n  \"id\": \"evt_3MN8vDGEmuv5fU7E1BraQQpl\",\n  \"object\": \"event\",\n  \"api_version\": \"2022-11-15\",\n  \"created\": 1672984616,\n  \"data\": {\n    \"object\": {\n      \"id\": \"ch_3MN8vDGEmuv5fU7E1XYFxObb\",\n      \"object\": \"charge\",\n      \"amount\": 2000,\n      \"amount_captured\": 2000,\n      \"amount_refunded\": 0,\n      \"application\": null,\n      \"application_fee\": null,\n      \"application_fee_amount\": null,\n      \"balance_transaction\": \"txn_3MN8vDGEmuv5fU7E1RNrwDj9\",\n      \"billing_details\": {\n        \"address\": {\n          \"city\": null,\n          \"country\": null,\n          \"line1\": null,\n          \"line2\": null,\n          \"postal_code\": null,\n          \"state\": null\n        },\n        \"email\": null,\n        \"name\": null,\n        \"phone\": null\n      },\n      \"calculated_statement_descriptor\": \"Stripe\",\n      \"captured\": true,\n      \"created\": 1672984615,\n      \"currency\": \"usd\",\n      \"customer\": null,\n      \"description\": \"(created by Stripe CLI)\",\n      \"destination\": null,\n      \"dispute\": null,\n      \"disputed\": false,\n      \"failure_balance_transaction\": null,\n      \"failure_code\": null,\n      \"failure_message\": null,\n      \"fraud_details\": {\n      },\n      \"invoice\": null,\n      \"livemode\": false,\n      \"metadata\": {\n      },\n      \"on_behalf_of\": null,\n      \"order\": null,\n      \"outcome\": {\n        \"network_status\": \"approved_by_network\",\n        \"reason\": null,\n        \"risk_level\": \"normal\",\n        \"risk_score\": 49,\n        \"seller_message\": \"Payment complete.\",\n        \"type\": \"authorized\"\n      },\n      \"paid\": true,\n      \"payment_intent\": \"pi_3MN8vDGEmuv5fU7E1WhGJsJW\",\n      \"payment_method\": \"pm_1MN8vDGEmuv5fU7EqkormNok\",\n      \"payment_method_details\": {\n        \"card\": {\n          \"brand\": \"visa\",\n          \"checks\": {\n            \"address_line1_check\": null,\n            \"address_postal_code_check\": null,\n            \"cvc_check\": null\n          },\n          \"country\": \"US\",\n          \"exp_month\": 1,\n          \"exp_year\": 2024,\n          \"fingerprint\": \"BXlQDKqvikDH65Cu\",\n          \"funding\": \"credit\",\n          \"installments\": null,\n          \"last4\": \"4242\",\n          \"mandate\": null,\n          \"network\": \"visa\",\n          \"three_d_secure\": null,\n          \"wallet\": null\n        },\n        \"type\": \"card\"\n      },\n      \"receipt_email\": null,\n      \"receipt_number\": null,\n      \"receipt_url\": \"https://pay.stripe.com/receipts/payment/CAcaFwoVYWNjdF8xTUo1WndHRW11djVmVTdFKKjw3p0GMgYgTbXnryA6LBYnAhPwd7LqD3iWSfaj_fnDVb3UOoiG24FMWJaqdVY1zupw_6g0TKNJdwf6\",\n      \"refunded\": false,\n      \"review\": null,\n      \"shipping\": {\n        \"address\": {\n          \"city\": \"San Francisco\",\n          \"country\": \"US\",\n          \"line1\": \"510 Townsend St\",\n          \"line2\": null,\n          \"postal_code\": \"94103\",\n          \"state\": \"CA\"\n        },\n        \"carrier\": null,\n        \"name\": \"Jenny Rosen\",\n        \"phone\": null,\n        \"tracking_number\": null\n      },\n      \"source\": null,\n      \"source_transfer\": null,\n      \"statement_descriptor\": null,\n      \"statement_descriptor_suffix\": null,\n      \"status\": \"succeeded\",\n      \"transfer_data\": null,\n      \"transfer_group\": null\n    }\n  },\n  \"livemode\": false,\n  \"pending_webhooks\": 1,\n  \"request\": {\n    \"id\": \"req_QZevuJq0Xj15Wv\",\n    \"idempotency_key\": \"7940b850-37a3-4b6c-a55d-aaac6a4d43ce\"\n  },\n  \"type\": \"charge.succeeded\"\n}",
  headers: {
    host: "val",
    "user-agent": "Stripe/1.0 (+https://stripe.com/docs/webhooks)",

  },
  params: {
    functionId: "63b7a841ccceb438b2b6",
  },
}```
mandolf0 commented 1 year ago

This is getting me halfway there in NodeJS.

var obj = req.payload;
const map = new Map(Object.entries(obj));  
for (const [key, value] of Object.entries(obj)) {
// console.log(`${key} ${value}`); 
if(key =="body") {
console.log(`${key} ${value}`); // 
}
}

And, btw, I'm sending a POST from the Appwrite fn with the proxy response to a NodeJS server outside of Appwrite. This way I can debug the function without reuploading anything.

Stripe webhook-> this proxy -> appwrite fn - > dev server.

  const event = JSON.parse(payload || '{}');
  console.log(`\nEvent is: ${event}`);

    //send the webhook data to the LAN dev server for debug
  request({
    url: "http://10.0.0.64:5000/webhook",
    method: "POST",
    json: true, 
    body: event
  }, function (error, response, body) {
    console.log(response);
  });