Skip to content

Commit

Permalink
Merge pull request #56 from Andrenzo17/master
Browse files Browse the repository at this point in the history
Add gopay tokenization & api subscription
  • Loading branch information
Andrenzo17 authored Aug 20, 2021
2 parents f4713de + dc886bf commit 222db12
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 21 deletions.
37 changes: 28 additions & 9 deletions Midtrans/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ApiRequestor
*/
public static function get($url, $server_key, $data_hash)
{
return self::remoteCall($url, $server_key, $data_hash, false);
return self::remoteCall($url, $server_key, $data_hash, 'GET');
}

/**
Expand All @@ -36,7 +36,21 @@ public static function get($url, $server_key, $data_hash)
*/
public static function post($url, $server_key, $data_hash)
{
return self::remoteCall($url, $server_key, $data_hash, true);
return self::remoteCall($url, $server_key, $data_hash, 'POST');
}

/**
* Send PATCH request
*
* @param string $url
* @param string $server_key
* @param mixed[] $data_hash
* @return mixed
* @throws Exception
*/
public static function patch($url, $server_key, $data_hash)
{
return self::remoteCall($url, $server_key, $data_hash, 'PATCH');
}

/**
Expand All @@ -49,7 +63,7 @@ public static function post($url, $server_key, $data_hash)
* @return mixed
* @throws Exception
*/
public static function remoteCall($url, $server_key, $data_hash, $post = true)
public static function remoteCall($url, $server_key, $data_hash, $method)
{
$ch = curl_init();

Expand Down Expand Up @@ -84,7 +98,7 @@ public static function remoteCall($url, $server_key, $data_hash, $post = true)
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Accept: application/json',
'User-Agent: midtrans-php-v2.4.4',
'User-Agent: midtrans-php-v2.5.0',
'Authorization: Basic ' . base64_encode($server_key . ':')
),
CURLOPT_RETURNTRANSFER => 1
Expand All @@ -111,22 +125,27 @@ public static function remoteCall($url, $server_key, $data_hash, $post = true)
$curl_options = array_replace_recursive($curl_options, Config::$curlOptions, $headerOptions);
}

if ($post) {
$curl_options[CURLOPT_POST] = 1;
if ($method != 'GET') {

if ($data_hash) {
$body = json_encode($data_hash);
$curl_options[CURLOPT_POSTFIELDS] = $body;
} else {
$curl_options[CURLOPT_POSTFIELDS] = '';
}

if ($method == 'POST') {
$curl_options[CURLOPT_POST] = 1;
} elseif ($method == 'PATCH') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
}
}

curl_setopt_array($ch, $curl_options);

// For testing purpose
if (class_exists('\Midtrans\MT_Tests') && MT_Tests::$stubHttp) {
$result = self::processStubed($curl_options, $url, $server_key, $data_hash, $post);
$result = self::processStubed($curl_options, $url, $server_key, $data_hash, $method);
} else {
$result = curl_exec($ch);
// curl_close($ch);
Expand All @@ -152,13 +171,13 @@ public static function remoteCall($url, $server_key, $data_hash, $post = true)
}
}

private static function processStubed($curl, $url, $server_key, $data_hash, $post)
private static function processStubed($curl, $url, $server_key, $data_hash, $method)
{
MT_Tests::$lastHttpRequest = array(
"url" => $url,
"server_key" => $server_key,
"data_hash" => $data_hash,
"post" => $post,
$method => $method,
"curl" => $curl
);

Expand Down
4 changes: 2 additions & 2 deletions Midtrans/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class Config
*/
public static $curlOptions = array();

const SANDBOX_BASE_URL = 'https://api.sandbox.midtrans.com/v2';
const PRODUCTION_BASE_URL = 'https://api.midtrans.com/v2';
const SANDBOX_BASE_URL = 'https://api.sandbox.midtrans.com';
const PRODUCTION_BASE_URL = 'https://api.midtrans.com';
const SNAP_SANDBOX_BASE_URL = 'https://app.sandbox.midtrans.com/snap/v1';
const SNAP_PRODUCTION_BASE_URL = 'https://app.midtrans.com/snap/v1';

Expand Down
145 changes: 137 additions & 8 deletions Midtrans/CoreApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Midtrans;

use Exception;

/**
* Provide charge and capture functions for Core API
*/
Expand Down Expand Up @@ -36,7 +37,7 @@ public static function charge($params)
}

return ApiRequestor::post(
Config::getBaseUrl() . '/charge',
Config::getBaseUrl() . '/v2/charge',
Config::$serverKey,
$payloads
);
Expand All @@ -56,14 +57,14 @@ public static function capture($param)
);

return ApiRequestor::post(
Config::getBaseUrl() . '/capture',
Config::getBaseUrl() . '/v2/capture',
Config::$serverKey,
$payloads
);
}

/**
* Do `/card/register` API request to Core API
* Do `/v2/card/register` API request to Core API
*
* @param $cardNumber
* @param $expMoth
Expand All @@ -79,14 +80,14 @@ public static function cardRegister($cardNumber, $expMoth, $expYear)
. "&client_key=" . Config::$clientKey;

return ApiRequestor::get(
Config::getBaseUrl() . $path,
Config::getBaseUrl() . "/v2" . $path,
Config::$clientKey,
false
);
}

/**
* Do `/token` API request to Core API
* Do `/v2/token` API request to Core API
*
* @param $cardNumber
* @param $expMoth
Expand All @@ -104,14 +105,14 @@ public static function cardToken($cardNumber, $expMoth, $expYear, $cvv)
. "&client_key=" . Config::$clientKey;

return ApiRequestor::get(
Config::getBaseUrl() . $path,
Config::getBaseUrl() . "/v2" . $path,
Config::$clientKey,
false
);
}

/**
* Do `/point_inquiry/<tokenId>` API request to Core API
* Do `/v2/point_inquiry/<tokenId>` API request to Core API
*
* @param string tokenId - tokenId of credit card (more params detail refer to: https://api-docs.midtrans.com)
* @return mixed
Expand All @@ -120,9 +121,137 @@ public static function cardToken($cardNumber, $expMoth, $expYear, $cvv)
public static function cardPointInquiry($tokenId)
{
return ApiRequestor::get(
Config::getBaseUrl() . '/point_inquiry/' . $tokenId,
Config::getBaseUrl() . '/v2/point_inquiry/' . $tokenId,
Config::$serverKey,
false
);
}

/**
* Create `/v2/pay/account` API request to Core API
*
* @param string create pay account request (more params detail refer to: https://api-docs.midtrans.com/#create-pay-account)
* @return mixed
* @throws Exception
*/
public static function linkPaymentAccount($param)
{
return ApiRequestor::post(
Config::getBaseUrl() . '/v2/pay/account',
Config::$serverKey,
$param
);
}

/**
* Do `/v2/pay/account/<accountId>` API request to Core API
*
* @param string accountId (more params detail refer to: https://api-docs.midtrans.com/#get-pay-account)
* @return mixed
* @throws Exception
*/
public static function getPaymentAccount($accountId)
{
return ApiRequestor::get(
Config::getBaseUrl() . '/v2/pay/account/' . $accountId,
Config::$serverKey,
false
);
}

/**
* Unbind `/v2/pay/account/<accountId>/unbind` API request to Core API
*
* @param string accountId (more params detail refer to: https://api-docs.midtrans.com/#unbind-pay-account)
* @return mixed
* @throws Exception
*/
public static function unlinkPaymentAccount($accountId)
{
return ApiRequestor::post(
Config::getBaseUrl() . '/v2/pay/account/' . $accountId . '/unbind',
Config::$serverKey,
false
);
}

/**
* Create `/v1/subscription` API request to Core API
*
* @param string create subscription request (more params detail refer to: https://api-docs.midtrans.com/#create-subscription)
* @return mixed
* @throws Exception
*/
public static function createSubscription($param)
{
return ApiRequestor::post(
Config::getBaseUrl() . '/v1/subscriptions',
Config::$serverKey,
$param
);
}

/**
* Do `/v1/subscription/{subscription_id}` API request to Core API
*
* @param string get subscription request (more params detail refer to: https://api-docs.midtrans.com/#get-subscription)
* @return mixed
* @throws Exception
*/
public static function getSubscription($SubscriptionId)
{
return ApiRequestor::get(
Config::getBaseUrl() . '/v1/subscriptions/' . $SubscriptionId,
Config::$serverKey,
false
);
}

/**
* Do disable `/v1/subscription/{subscription_id}/disable` API request to Core API
*
* @param string disable subscription request (more params detail refer to: https://api-docs.midtrans.com/#disable-subscription)
* @return mixed
* @throws Exception
*/
public static function disableSubscription($SubscriptionId)
{
return ApiRequestor::post(
Config::getBaseUrl() . '/v1/subscriptions/' . $SubscriptionId . '/disable',
Config::$serverKey,
false
);
}

/**
* Do enable `/v1/subscription/{subscription_id}/enable` API request to Core API
*
* @param string enable subscription request (more params detail refer to: https://api-docs.midtrans.com/#enable-subscription)
* @return mixed
* @throws Exception
*/
public static function enableSubscription($SubscriptionId)
{
return ApiRequestor::post(
Config::getBaseUrl() . '/v1/subscriptions/' . $SubscriptionId . '/enable',
Config::$serverKey,
false
);
}

/**
* Do update subscription `/v1/subscription/{subscription_id}` API request to Core API
*
* @param string update subscription request (more params detail refer to: https://api-docs.midtrans.com/#update-subscription)
* @return mixed
* @throws Exception
*/
public static function updateSubscription($SubscriptionId, $param)
{
return ApiRequestor::patch(
Config::getBaseUrl() . '/v1/subscriptions/' . $SubscriptionId,
Config::$serverKey,
$param
);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "midtrans/midtrans-php",
"description": "PHP Wrapper for Midtrans Payment API.",
"homepage": "https://midtrans.com",
"version": "2.4.4",
"version": "2.5.0",
"type": "library",
"license":"MIT",
"authors": [
Expand Down
49 changes: 49 additions & 0 deletions examples/core-api/tokenization-process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Midtrans;

require_once dirname(__FILE__) . '/../../Midtrans.php';
//Set Your server key
Config::$serverKey = "<your server key>";

// define variables and set to empty values
$number = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = ($_POST["number"]);
}

// required
$params = array(
"payment_type" => "gopay",
"gopay_partner" => array(
"phone_number" => $number,
"redirect_url" => "https://www.google.com"
)
);

$response = CoreApi::linkPaymentAccount($params);
?>

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<h2>Simple Gopay Tokenization</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Phone number: <input type="text" name="number">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>


<?php
echo "<h2>Result get pay account:</h2>";
echo json_encode($response, JSON_UNESCAPED_SLASHES);
echo "<br>";
?>


</body>
</html>
Loading

0 comments on commit 222db12

Please sign in to comment.