-2

I have prepared a payment method, but it sends data in a JSON object in POST. I want to receive it and send it to the Firebase database. Not all data, but only a certain amount.

Transaction Processed Callback

It's an URL for an endpoint in your web app, on which you'd receive callbacks as a notification after performing any payment process from your customer side, you would receive a JSON object in POST request which contains a value by which you can know about your payments such as the status of the transaction (success/declined), the order ID related to this transition, the transaction ID and much other information related to your transaction, here's a sample request similar to the one you should receive on your transaction processed callback endpoint.

{
  "obj": {
    "id": 2556706,
    "pending": false,
    "amount_cents": 100,
    "success": true,
    "is_auth": false,
    "is_capture": false,
    "is_standalois_capturene_payment": true,
    "is_voided": false,
    "is_refunded": false,
    "is_3d_secure": true,
    "integration_id": 6741,
    "profile_id": 4214,
    "has_parent_transaction": false,
    "order": {
      "id": 4778239,
      "created_at": "2020-03-25T18:36:05.494685",
      "delivery_needed": true,
      "merchant": {
        "id": 4214,
        "created_at": "2019-09-22T18:32:56.764441",
        "phones": [
          "01032347111"
        ],
        "company_emails": [
          "fnjum@temp-link.net"
        ],
        "company_name": "Accept Payments",
        "state": "",
        "country": "EGY",
        "city": "",
        "postal_code": "",
        "street": ""
      },
      "collector": {
        "id": 115,
        "created_at": "2019-06-29T00:48:26.910433",
        "phones": [],
        "company_emails": [],
        "company_name": "logix - test",
        "state": "Heliopolis",
        "country": "egypt",
        "city": "cairo",
        "postal_code": "123456",
        "street": "Marghany"
      },
      "amount_cents": 2000,
      "shipping_data": {
        "id": 2558893,
        "first_name": "abdulrahman",
        "last_name": "Khalifa",
        "street": "Wadi el Nile",
        "building": "5",
        "floor": "11",
        "apartment": "1565162",
        "city": "Cairo",
        "state": "Cairo",
        "country": "EG",
        "email": "abdulrahman@weaccept.co",
        "phone_number": "01011994353",
        "postal_code": "",
        "extra_description": " ",
        "shipping_method": "UNK",
        "order_id": 4778239,
        "order": 4778239
      },
      "shipping_details": {
        "id": 1401,
        "cash_on_delivery_amount": 0,
        "cash_on_delivery_type": "Cash",
        "latitude": null,
        "longitude": null,
        "is_same_day": 0,
        "number_of_packages": 1,
        "weight": 1,
        "weight_unit": "Kilogram",
        "length": 1,
        "width": 1,
        "height": 1,
        "delivery_type": "PUD",
        "return_type": null,
        "order_id": 4778239,
        "notes": "im so tired",
        "order": 4778239
      },
      "currency": "EGP",
      "is_payment_locked": false,
      "is_return": false,
      "is_cancel": false,
      "is_returned": false,
      "is_canceled": false,
      "merchant_order_id": null,
      "wallet_notification": null,
      "paid_amount_cents": 100,
      "notify_user_with_email": false,
      "items": [],
      "order_url": "https://accept.paymobsolutions.com/i/nYWD",
      "commission_fees": 0,
      "delivery_fees_cents": 0,
      "delivery_vat_cents": 0,
      "payment_method": "tbc",
      "merchant_staff_tag": null,
      "api_source": "OTHER",
      "pickup_data": null,
      "delivery_status": []
    },
    "created_at": "2020-03-25T18:39:44.719228",
    "transaction_processed_callback_responses": [],
    "currency": "EGP",
    "source_data": {
      "pan": "2346",
      "type": "card",
      "sub_type": "MasterCard"
    },
    "api_source": "IFRAME",
    "terminal_id": null,
    "is_void": false,
    "is_refund": false,
    "data": {
      "acq_response_code": "00",
      "avs_acq_response_code": "Unsupported",
      "klass": "VPCPayment",
      "receipt_no": "008603626261",
      "order_info": "claudette09@exa.com",
      "message": "Approved",
      "gateway_integration_pk": 6741,
      "batch_no": "20200325",
      "card_num": null,
      "secure_hash": "832F4673452F9538CCD57D6B07B74183A0EEB1BEF7CA58704E31B244E8366549",
      "avs_result_code": "Unsupported",
      "card_type": "MC",
      "merchant": "TEST999999EGP",
      "created_at": "2020-03-25T16:40:37.127504",
      "merchant_txn_ref": "6741_572e773a5a0f55ff8de91876075d023e",
      "authorize_id": "626261",
      "currency": "EGP",
      "amount": "100",
      "transaction_no": "2090026774",
      "txn_response_code": "0",
      "command": "pay"
    },
    "is_hidden": false,
    "payment_key_claims": {
      "lock_order_when_paid": true,
      "integration_id": 6741,
      "billing_data": {
        "email": "claudette09@exa.com",
        "building": "8028",
        "apartment": "803",
        "street": "Ethan Land",
        "country": "CR",
        "state": "Utah",
        "last_name": "Nicolas",
        "first_name": "Clifford",
        "postal_code": "01898",
        "extra_description": "NA",
        "phone_number": "+86(8)9135210487",
        "floor": "42",
        "city": "Jaskolskiburgh"
      },
      "order_id": 4778239,
      "user_id": 4705,
      "pmk_ip": "197.57.37.135",
      "exp": 1585157836,
      "currency": "EGP",
      "amount_cents": 100
    },
    "error_occured": false,
    "is_live": false,
    "other_endpoint_reference": null,
    "refunded_amount_cents": 0,
    "source_id": -1,
    "is_captured": false,
    "captured_amount": 0,
    "merchant_staff_tag": null,
    "owner": 4705,
    "parent_transaction": null
  },
  "type": "TRANSACTION"
}
TRiG
  • 10,148
  • 7
  • 57
  • 107
  • to convert to JSON : json_encode($array); to decode JSON and return PHP array : json_decode($recievedJson,true); – AJ Zack May 09 '23 at 21:29
  • 1
    please remove your JSON example or make sure that your emails in it and all other data are CLEARLY fake emails (e.g. "abc@example.com") and fake names (e.g. "Darth Vader")...instead of what I'm seeing which looks like potentially real emails and names even if they are not...your "merchant" says "TEST999999EGP", but that's the only thing that looks like test data. – Kevin Nelson May 09 '23 at 22:52

2 Answers2

0

I faced something similar a few days ago.

Try this:

<?php

if ($_SERVER['REQUEST_METHOD'] === "POST") {
    $data = "";
    $fd = fopen("php://input", 'r');

    while(($chunk = fread($fd)))
        $data .= $chunk;

    $json = json_decode($data, true);
    
    if (is_null($json))
        die("Error parsing JSON request");
    
    print_r($json);
} else {
    echo "Method not supported";
}

Hope this can help you!

Saul Uribe
  • 744
  • 4
  • 10
  • 2
    `$data = file_get_contents('php://input')` is simpler than using `fopen()` and `fread()`. – TRiG May 09 '23 at 22:10
0

PHP has inbuilt support for when the Content-Type of a POST request is application/x-www-urlencoded or multipart/form-data. (These are used by browsers submitting HTML forms: multipart/form-data may include uploaded files; application/x-www-urlencoded is simpler and more efficient and cannot include uploaded files.) PHP will automatically parse the POST data supplied and put it into the $_POST and $_FILES superglobals.

For any other input type, you need to to the work yourself, though in the case of JSON it should be simple enough.

  1. Get the data: $data = file_get_contents('php://input');
  2. Parse the data: $arr = json_decode($data, true);

Putting that together, we get:

$arr = [];
if ($_SERVER['REQUEST_METHOD'] === "POST") {
    $data = file_get_contents('php://input');
    $arr = json_decode($data, true);
}
TRiG
  • 10,148
  • 7
  • 57
  • 107