-2

I have a code that retrieve json result from snaptoken when the pay-button is clicked and the transaction process ended.

    <script src="https://app.sandbox.midtrans.com/snap/snap.js"> </script>
    <script type="text/javascript">
    document.getElementById('pay-button').onclick = function() {
        // SnapToken acquired from previous step
        snap.pay('<?php echo $snapToken ?>', {
            // Optional
            onSuccess: function(result) {
                /* You may add your own js here, this is just example */
                document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
                console.log(JSON.stringify(result, null, 2));
            },
            // Optional
            onPending: function(result) {
                /* You may add your own js here, this is just example */
                document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
                console.log(JSON.stringify(result, null, 2));
            },
            // Optional
            onError: function(result) {
                /* You may add your own js here, this is just example */
                document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
                console.log(JSON.stringify(result, null, 2));
            }
        });
    };
    </script>

the code is from midtrans docs and the output looks like this

{
  "Status_code": "201",
  "Status_message": "Success, Transaction Is Found",
  "Transaction_id": "6ce0790e-F214-455c-8f3c-77269b23b8cb",
  "Order_id": "1836559779",
  "Gross_amount": "270000.00",
  "Payment_type": "Gopay",
  "Transaction_time": "2022-07-15 15:04:52",
  "Transaction_status": "Pending",
  "Fraud_status": "Accept",
  "Finish_redirect_url": "Http://Example.Com?Order_id=1836559779&Status_code=201&Transaction_status=Pending"
}

its there a way to store the invoice payment into database with mysql?

i tried to change it into object json using parse() although it still gets an error

onPending: function(result) {
                /* You may add your own js here, this is just example */
                var stringjson = document.getElementById('result-json').innerHTML += JSON.stringify(
                    result, null, 2);
                var objectjson = document.getElementById('result-json').innerHTML = JSON.parse(
                    stringjson);
                console.log(objectjson);
            },
  • Does this answer your question? [Storing Data in MySQL as JSON](https://stackoverflow.com/questions/3564024/storing-data-in-mysql-as-json) – angel.bonev Jul 15 '22 at 09:04

1 Answers1

2

Depending on the version of MySQL you use, you might be able to store it directly to the database:

As of MySQL 5.7.8, MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents. The JSON data type provides these advantages over storing JSON-format strings in a string column:

Automatic validation of JSON documents stored in JSON columns. Invalid documents produce an error.

Optimized storage format. JSON documents stored in JSON columns are converted to an internal format that permits quick read access to document elements. When the server later must read a JSON value stored in this binary format, the value need not be parsed from a text representation. The binary format is structured to enable the server to look up subobjects or nested values directly by key or array index without reading all values before or after them in the document.

Source: https://dev.mysql.com/doc/refman/5.7/en/json.html

If you are using an older version, you could still store it as a text field, but this would not be very efficient.

muhrahim95
  • 121
  • 4