Coder Social home page Coder Social logo

rizdaprasetya / midtrans-mobile-merchant-server--php-sample- Goto Github PK

View Code? Open in Web Editor NEW
16.0 3.0 29.0 22 KB

Sample for Midtrans mobile backend implementation written in PHP |

Home Page: https://mobile-docs.midtrans.com

PHP 100.00%
midtrans payment payment-gateway api mobile

midtrans-mobile-merchant-server--php-sample-'s Introduction

Simple Merchant Server Implementation Reference for Mobile SDK (PHP version).

Also available in other Language Implementation:

Description

This is a example mobile SDK server for Midtrans's iOS and Android SDK, as an implementation reference to use the mobile sdk. Please read more in Documentation of Midtrans mobile SDK.

Purpose

The main idea why this server implementation needed is: To securely add HTTP Authorization Header from server side. This auth header is generated from server key (from your Midtrans account), this server key is secret, and should only be kept in server side, not client side (mobile app can be easily reverse engineered to extract any secret).

Additionally, it allows you to tweak JSON request parameter as needed from server side.

Endpoints

There is only one endpoint that are required to use Midtrans mobile SDK:

POST /charge/

This endpoint will proxy (forward) client request to Midtrans Snap API 'https://app.midtrans.com/snap/v1/transactions' (or 'https://app.sandbox.midtrans.com/snap/v1/transactions' for sandbox) with HTTP Authorization Header generated based on your Midtrans Server Key.

The response of API will be printed/returned to client as is. Example response that will be printed

{
    "token": "413ae932-471d-4c41-bfb4-e558cc271dcc",
    "redirect_url": "https://app.sandbox.midtrans.com/snap/v2/vtweb/413ae932-471d-4c41-bfb4-e558cc271dcc"
}

Usage

Edit file charge/index.php, insert your Midtrans Account Server Key to '<server key>'. Upload these to your host, and make sure the url <url where you host this>/charge/index.php can be accessed from the mobile app.

Set <url where you host this>/charge/index.php as merchant base url in mobile SDK. (refer to Midtrans mobile SDK doc)

Advanced Tips: You can also configure your HTTP server to route <url where you host this>/charge/ url to /charge/index.php file. So the merchant base url can be just configured as <url where you host this>/(without charge/index.php, because SDK will automatically convert it to <url where you host this>/charge/ then your server will route to /charge/index.php/).

Testing

You can mock client's request by executing this CURL command to the /charge/index.php endpoint:

curl -X POST -d '{  
   "transaction_details":{  
      "order_id":"mobile-12345",
      "gross_amount":280000
   },
   "item_details":[  
      {  
         "id":"A01",
         "price":280000,
         "quantity":1,
         "name":"Mie Ayam Komplit"
      }
   ],
   "customer_details":[  
      {  
         "email":"[email protected]",
         "first_name":"Budi",
         "last_name":"Khannedy",
         "phone":"628112341234"
      }
   ]
}' "https://<your url>/charge/index.php/"

Note: dont forget to change "http://<your url>/charge/index.php/" to your url where you hosted the /charge/index.php.

You can also import that curl command to Postman.

Notes

This is just for very basic implementation reference, in production, you should implement your backend more securely.

Troubleshooting

When request sent to the url https://<your url>/charge/index.php fail, try to change the url to https://<your url>/charge/

Get help

midtrans-mobile-merchant-server--php-sample-'s People

Contributors

rizdaprasetya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

midtrans-mobile-merchant-server--php-sample-'s Issues

production mode

kenapa masih masuk ke mode sandbox ya padahal sudah di set ke $is_production sudah diset ke true?

MidtransSDK: Error Get Payment Option

Halo mas, kenapa ya setiap saya request transaksi ke url ini http://pos-teknokrat.epizy.com/midtrans.php/charge/ tidak mendapatkan transaction response dari midtrans. Justru mendapatkan response error seperti ini:
<html><body><script type="text/javascript" src="/aes.js"></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("98dc1cf960e406255703a20ad47404e1");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://pos-teknokrat.epizy.com/midtrans.php/charge/?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

Padahal merchant base server sudah saya sesuaikan dengan yg ada di dokumentasi dan mengganti server key punya saya dan setiap saya integrasikan Midtrans ke android selalu mendapatkan pesan Error Get Payment Option.

[Sandbox] Error base URL setup

Error base URL setup using /charge/index.php, showing error :

Content-Type: application/json
Server: - Web acceleration by http://www.unixy.net/varnish
X-Cacheable: YES
Accept-Ranges: bytes
Date: Mon, 06 Aug 2018 03:13:04 GMT
X-Varnish: 1704966374 1704966368
Via: 1.1 varnish
Connection: keep-alive
Vary: Accept-Encoding,User-Agent, User-Agent
age: 0
X-Cache: HIT
X-Cache-Hits: 1
{
    "error_messages": [
        "transaction_details.gross_amount is required",
        "transaction_details.gross_amount is not a number",
        "transaction_details.order_id is required"
    ]
}

The correct base url uses /charge only.

Server Side Midtrans mobile integration getting Error

wrong path, make sure it's /charge

`<?php
// Set your server key (Note: Server key for sandbox and production mode are different)
$server_key = 'SB-Mid-server-7xaCHZufo--NTBcJ0T3SXB9a';
// Set true for production, set false for sandbox
$is_production = false;

$api_url = $is_production ?
'https://app.midtrans.com/snap/v1/transactions' :
'https://app.sandbox.midtrans.com/snap/v1/transactions';

// Check if request doesn't contains /charge in the url/path, display 404
if( !strpos($_SERVER['REQUEST_URI'], '/charge') ) {
http_response_code(404);
echo "wrong path, make sure it's /charge"; exit();
}
// Check if method is not HTTP POST, display 404
if( $_SERVER['REQUEST_METHOD'] !== 'POST'){
http_response_code(404);
echo "Page not found or wrong HTTP request method is used"; exit();
}

// get the HTTP POST body of the request
$request_body = file_get_contents('php://input');
// set response's content type as JSON
header('Content-Type: application/json');
// call charge API using request body passed by mobile SDK
$charge_result = chargeAPI($api_url, $server_key, $request_body);
// set the response http status code
http_response_code($charge_result['http_code']);
// then print out the response body
echo $charge_result['body'];

/**

  • call charge API using Curl
  • @param string $api_url
  • @param string $server_key
  • @param string $request_body
    */
    function chargeAPI($api_url, $server_key, $request_body){
    $ch = curl_init();
    $curl_options = array(
    CURLOPT_URL => $api_url,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_HEADER => 0,
    // Add header to the request, including Authorization generated from server key
    CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Accept: application/json',
    'Authorization: Basic ' . base64_encode($server_key . ':')
    ),
    CURLOPT_POSTFIELDS => $request_body
    );
    curl_setopt_array($ch, $curl_options);
    $result = array(
    'body' => curl_exec($ch),
    'http_code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
    );
    return $result;
    }`

Screen Shot 2020-11-13 at 1 12 23 AM

Please help? what happened to my implementation?

working?

script ini saya coba bolak balik ngk jalan

transaction_details.gross_amount is required","transaction_details.gross_amount is not a number","transaction_details.order_id is required

I have problem with android integration, when i using sample in postman it's work, but when i using it from android it sho this error, can anybody tell this solution, thankyou

D/OkHttp: --> POST https://pokkiproject.com/charge
Content-Type: application/json
Content-Length: 271
Accept: application/json
D/OkHttp: {"customer_details":{"customer_identifier":"[email protected]","email":"[email protected]","first_name":"user fullname","phone":"085310102020"},"item_details":[],"transaction_details":{"currency":"IDR","gross_amount":28000.0,"order_id":"1617873344071"},"user_id":"[email protected]"}
--> END POST (271-byte body)
D/OkHttp: <-- 400 https://pokkiproject.com/charge/ (1586ms)
date: Thu, 08 Apr 2021 09:17:41 GMT
server: Apache
vary: Accept-Encoding
content-type: application/json
D/OkHttp: {"error_messages":["transaction_details.gross_amount is required","transaction_details.gross_amount is not a number","transaction_details.order_id is required"]}
<-- END HTTP (161-byte body)
E/PaymentMethodsActivity: checkout>error:Failed to retrieve response from server

404 not found ketika request

Hi Selamat Malam,

Sya ingin mengimplementasi ke android dan mendapatkan masalah berikut

saya sudah impementasi charge di sisi server (https://github.com/rizdaprasetya/midtrans-mobile-merchant-server--php-sample-)
-folder berupa charge --> index.php
saya setting MERCHANT_BASE_CHECKOUT_URL = https://serversaya.com/
ketika saya run mendapatkan Log berikut

05-08 01:24:52.591 24396-24396/com.tingtong.android I/MidtransSDK: Found 1 user addresses.
05-08 01:24:52.625 24396-24569/com.tingtong.android D/OkHttp: --> POST https://serversaya.com/charge
05-08 01:24:52.625 24396-24569/com.tingtong.android D/OkHttp: Content-Type: application/json
05-08 01:24:52.625 24396-24569/com.tingtong.android D/OkHttp: Content-Length: 637
05-08 01:24:52.625 24396-24569/com.tingtong.android D/OkHttp: Accept: application/json
05-08 01:24:52.626 24396-24569/com.tingtong.android D/OkHttp: {"customer_details":{"billing_address":{"address":"uvuvuv","city":"vyuv","country_code":"IDN","first_name":"uvuv","phone":"0606338","postal_code":"85086"},"email":"[email protected]","first_name":"uvuv","phone":"0606338","shipping_address":{"address":"uvuvuv","city":"vyuv","country_code":"IDN","first_name":"uvuv","phone":"0606338","postal_code":"85086"}},"credit_card":{"authentication":"3ds","bank":"bca","save_card":false,"secure":true},"item_details":[{"id":"1","name":"Trekking Shoes","price":20000,"quantity":1}],"transaction_details":{"gross_amount":20000,"order_id":"1588875892331"},"user_id":"cfe21a33-dc97-41db-8b1b-2af90707a560"}
05-08 01:24:52.626 24396-24569/com.tingtong.android D/OkHttp: --> END POST (637-byte body)
05-08 01:24:52.754 24396-24540/com.tingtong.android D/OpenGLRenderer: endAllStagingAnimators on 0x55c0097ba0 (RippleDrawable) with handle 0x55c079ad10
05-08 01:24:52.971 24396-24569/com.tingtong.android D/OkHttp: <-- 404 https://serversaya.com/charge/ (344ms)
05-08 01:24:52.971 24396-24569/com.tingtong.android D/OkHttp: date: Thu, 07 May 2020 18:24:52 GMT
05-08 01:24:52.971 24396-24569/com.tingtong.android D/OkHttp: content-type: text/html; charset=UTF-8
05-08 01:24:52.971 24396-24569/com.tingtong.android D/OkHttp: vary: Accept-Encoding
05-08 01:24:52.972 24396-24569/com.tingtong.android D/OkHttp: wrong path, make sure it's /charge
05-08 01:24:52.972 24396-24569/com.tingtong.android D/OkHttp: <-- END HTTP (36-byte body)

kenapa 404 not found ya?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.