-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from omarxp/feature/revamp-midtrans
revamp midtrans
- Loading branch information
Showing
59 changed files
with
2,267 additions
and
2,275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Check PHP version. | ||
*/ | ||
if (version_compare(PHP_VERSION, '5.4', '<')) { | ||
throw new Exception('PHP version >= 5.4 required'); | ||
} | ||
|
||
// Check PHP Curl & json decode capabilities. | ||
if (!function_exists('curl_init') || !function_exists('curl_exec')) { | ||
throw new Exception('Midtrans needs the CURL PHP extension.'); | ||
} | ||
if (!function_exists('json_decode')) { | ||
throw new Exception('Midtrans needs the JSON PHP extension.'); | ||
} | ||
|
||
// Configurations | ||
require_once 'Midtrans/Config.php'; | ||
|
||
// Midtrans API Resources | ||
require_once 'Midtrans/Transaction.php'; | ||
|
||
// Plumbing | ||
require_once 'Midtrans/ApiRequestor.php'; | ||
require_once 'Midtrans/SnapApiRequestor.php'; | ||
require_once 'Midtrans/Notification.php'; | ||
require_once 'Midtrans/CoreApi.php'; | ||
require_once 'Midtrans/Snap.php'; | ||
|
||
// Sanitization | ||
require_once 'Midtrans/Sanitizer.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace Midtrans; | ||
|
||
/** | ||
* Send request to Midtrans API | ||
* Better don't use this class directly, use CoreApi, Transaction | ||
*/ | ||
|
||
class ApiRequestor | ||
{ | ||
|
||
/** | ||
* Send GET request | ||
* | ||
* @param string $url | ||
* @param string $server_key | ||
* @param mixed[] $data_hash | ||
*/ | ||
public static function get($url, $server_key, $data_hash) | ||
{ | ||
return self::remoteCall($url, $server_key, $data_hash, false); | ||
} | ||
|
||
/** | ||
* Send POST request | ||
* | ||
* @param string $url | ||
* @param string $server_key | ||
* @param mixed[] $data_hash | ||
*/ | ||
public static function post($url, $server_key, $data_hash) | ||
{ | ||
return self::remoteCall($url, $server_key, $data_hash, true); | ||
} | ||
|
||
/** | ||
* Actually send request to API server | ||
* | ||
* @param string $url | ||
* @param string $server_key | ||
* @param mixed[] $data_hash | ||
* @param bool $post | ||
*/ | ||
public static function remoteCall($url, $server_key, $data_hash, $post = true) | ||
{ | ||
$ch = curl_init(); | ||
|
||
$curl_options = array( | ||
CURLOPT_URL => $url, | ||
CURLOPT_HTTPHEADER => array( | ||
'Content-Type: application/json', | ||
'Accept: application/json', | ||
'Authorization: Basic ' . base64_encode($server_key . ':') | ||
), | ||
CURLOPT_RETURNTRANSFER => 1 | ||
); | ||
|
||
// merging with Config::$curlOptions | ||
if (count(Config::$curlOptions)) { | ||
// We need to combine headers manually, because it's array and it will no be merged | ||
if (Config::$curlOptions[CURLOPT_HTTPHEADER]) { | ||
$mergedHeders = array_merge($curl_options[CURLOPT_HTTPHEADER], Config::$curlOptions[CURLOPT_HTTPHEADER]); | ||
$headerOptions = array( CURLOPT_HTTPHEADER => $mergedHeders ); | ||
} else { | ||
$mergedHeders = array(); | ||
} | ||
|
||
$curl_options = array_replace_recursive($curl_options, Config::$curlOptions, $headerOptions); | ||
} | ||
|
||
if ($post) { | ||
$curl_options[CURLOPT_POST] = 1; | ||
|
||
if ($data_hash) { | ||
$body = json_encode($data_hash); | ||
$curl_options[CURLOPT_POSTFIELDS] = $body; | ||
} else { | ||
$curl_options[CURLOPT_POSTFIELDS] = ''; | ||
} | ||
} | ||
|
||
curl_setopt_array($ch, $curl_options); | ||
|
||
// For testing purpose | ||
if (class_exists('\Midtrans\VT_Tests') && VT_Tests::$stubHttp) { | ||
$result = self::processStubed($curl_options, $url, $server_key, $data_hash, $post); | ||
} else { | ||
$result = curl_exec($ch); | ||
// curl_close($ch); | ||
} | ||
|
||
|
||
if ($result === false) { | ||
throw new \Exception('CURL Error: ' . curl_error($ch), curl_errno($ch)); | ||
} else { | ||
try { | ||
$result_array = json_decode($result); | ||
} catch (\Exception $e) { | ||
throw new \Exception("API Request Error unable to json_decode API response: ".$result . ' | Request url: '.$url); | ||
} | ||
if (!in_array($result_array->status_code, array(200, 201, 202, 407))) { | ||
$message = 'Midtrans Error (' . $result_array->status_code . '): ' | ||
. $result_array->status_message; | ||
if (isset($result_array->validation_messages)) { | ||
$message .= '. Validation Messages (' . implode(", ", $result_array->validation_messages) . ')'; | ||
} | ||
if (isset($result_array->error_messages)) { | ||
$message .= '. Error Messages (' . implode(", ", $result_array->error_messages) . ')'; | ||
} | ||
throw new \Exception($message, $result_array->status_code); | ||
} else { | ||
return $result_array; | ||
} | ||
} | ||
} | ||
|
||
private static function processStubed($curl, $url, $server_key, $data_hash, $post) | ||
{ | ||
VT_Tests::$lastHttpRequest = array( | ||
"url" => $url, | ||
"server_key" => $server_key, | ||
"data_hash" => $data_hash, | ||
"post" => $post, | ||
"curl" => $curl | ||
); | ||
|
||
return VT_Tests::$stubHttpResponse; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Midtrans; | ||
|
||
/** | ||
* Midtrans Configuration | ||
*/ | ||
class Config | ||
{ | ||
|
||
/** | ||
* Your merchant's server key | ||
* | ||
* @static | ||
*/ | ||
public static $serverKey; | ||
/** | ||
* Your merchant's client key | ||
* | ||
* @static | ||
*/ | ||
public static $clientKey; | ||
/** | ||
* True for production | ||
* false for sandbox mode | ||
* | ||
* @static | ||
*/ | ||
public static $isProduction = false; | ||
/** | ||
* Set it true to enable 3D Secure by default | ||
* | ||
* @static | ||
*/ | ||
public static $is3ds = false; | ||
/** | ||
* Enable request params sanitizer (validate and modify charge request params). | ||
* See Midtrans_Sanitizer for more details | ||
* | ||
* @static | ||
*/ | ||
public static $isSanitized = false; | ||
/** | ||
* Default options for every request | ||
* | ||
* @static | ||
*/ | ||
public static $curlOptions = array(); | ||
|
||
const SANDBOX_BASE_URL = 'https://api.sandbox.midtrans.com/v2'; | ||
const PRODUCTION_BASE_URL = 'https://api.midtrans.com/v2'; | ||
const SNAP_SANDBOX_BASE_URL = 'https://app.sandbox.midtrans.com/snap/v1'; | ||
const SNAP_PRODUCTION_BASE_URL = 'https://app.midtrans.com/snap/v1'; | ||
|
||
/** | ||
* Get baseUrl | ||
* | ||
* @return string Midtrans API URL, depends on $isProduction | ||
*/ | ||
public static function getBaseUrl() | ||
{ | ||
return Config::$isProduction ? | ||
Config::PRODUCTION_BASE_URL : Config::SANDBOX_BASE_URL; | ||
} | ||
|
||
/** | ||
* Get snapBaseUrl | ||
* | ||
* @return string Snap API URL, depends on $isProduction | ||
*/ | ||
public static function getSnapBaseUrl() | ||
{ | ||
return Config::$isProduction ? | ||
Config::SNAP_PRODUCTION_BASE_URL : Config::SNAP_SANDBOX_BASE_URL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Midtrans; | ||
|
||
/** | ||
* Provide charge and capture functions for Core API | ||
*/ | ||
class CoreApi | ||
{ | ||
/** | ||
* Create transaction. | ||
* | ||
* @param mixed[] $params Transaction options | ||
*/ | ||
public static function charge($params) | ||
{ | ||
$payloads = array( | ||
'payment_type' => 'credit_card' | ||
); | ||
|
||
if (array_key_exists('item_details', $params)) { | ||
$gross_amount = 0; | ||
foreach ($params['item_details'] as $item) { | ||
$gross_amount += $item['quantity'] * $item['price']; | ||
} | ||
$payloads['transaction_details']['gross_amount'] = $gross_amount; | ||
} | ||
|
||
$payloads = array_replace_recursive($payloads, $params); | ||
|
||
if (Config::$isSanitized) { | ||
Sanitizer::jsonRequest($payloads); | ||
} | ||
|
||
$result = ApiRequestor::post( | ||
Config::getBaseUrl() . '/charge', | ||
Config::$serverKey, | ||
$payloads | ||
); | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Capture pre-authorized transaction | ||
* | ||
* @param string $param Order ID or transaction ID, that you want to capture | ||
*/ | ||
public static function capture($param) | ||
{ | ||
$payloads = array( | ||
'transaction_id' => $param, | ||
); | ||
|
||
$result = ApiRequestor::post( | ||
Config::getBaseUrl() . '/capture', | ||
Config::$serverKey, | ||
$payloads | ||
); | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Midtrans; | ||
|
||
/** | ||
* Read raw post input and parse as JSON. Provide getters for fields in notification object | ||
* | ||
* Example: | ||
* | ||
* ```php | ||
* | ||
* namespace Midtrans; | ||
* | ||
* $notif = new Notification(); | ||
* echo $notif->order_id; | ||
* echo $notif->transaction_status; | ||
* ``` | ||
*/ | ||
class Notification | ||
{ | ||
private $response; | ||
|
||
public function __construct($input_source = "php://input") | ||
{ | ||
$raw_notification = json_decode(file_get_contents($input_source), true); | ||
$status_response = Transaction::status($raw_notification['transaction_id']); | ||
$this->response = $status_response; | ||
} | ||
|
||
public function __get($name) | ||
{ | ||
if (array_key_exists($name, $this->response)) { | ||
return $this->response->$name; | ||
} | ||
} | ||
} |
Oops, something went wrong.