Skip to content

Commit acd8588

Browse files
committed
Add Midtrans Transaction
1 parent 8503a62 commit acd8588

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+9618
-2
lines changed

css/style.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ body {
243243
cursor: pointer;
244244
}
245245

246+
.form-container .checkout-button.disabled {
247+
background-color: #999;
248+
cursor: not-allowed;
249+
}
250+
246251
/* Hero Section */
247252
.hero {
248253
min-height: 100vh;

index.html

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@
2626
></script>
2727

2828
<!-- App -->
29-
<script src="src/app.js"></script>
29+
<script src="src/app.js" async></script>
30+
31+
<!-- Midtrans -->
32+
<script
33+
type="text/javascript"
34+
src="https://app.sandbox.midtrans.com/snap/snap.js"
35+
data-client-key="SB-Mid-client-G7a45yoT_5uVLHPy"
36+
></script>
3037
</head>
3138
<body>
3239
<!-- Navbar Start -->
@@ -95,6 +102,16 @@ <h4 x-show="$store.cart.items.length">
95102

96103
<div class="form-container" x-show="$store.cart.items.length">
97104
<form action="" id="checkoutForm">
105+
<input
106+
type="hidden"
107+
name="items"
108+
x-model="JSON.stringify($store.cart.items)"
109+
/>
110+
<input
111+
type="hidden"
112+
name="total"
113+
x-model="JSON.stringify($store.cart.total)"
114+
/>
98115
<h5>Customer Detail</h5>
99116

100117
<label for="name">
@@ -116,7 +133,7 @@ <h5>Customer Detail</h5>
116133
</label>
117134

118135
<button
119-
class="checkout-button"
136+
class="checkout-button disabled"
120137
type="submit"
121138
id="checkout-button"
122139
value="checkout"

php/midtrans-php/.drone.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
image: bradrydzewski/php:5.5
2+
script:
3+
- echo "Hello World"
4+
notify:
5+
email:
6+
recipients:
7+
- radityo.shaddiqa@veritrans.co.id

php/midtrans-php/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
composer.lock
2+
composer.phar
3+
vendor/
4+
.idea
5+
.DS_STORE

php/midtrans-php/.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
php:
3+
- 7.4
4+
- 7.2
5+
- 7.0
6+
- 5.6
7+
# - 5.5 # PHP version too old
8+
# - 5.4 # PHP version too old
9+
# - hhvm # HHVM officially drop PHP support
10+
11+
before_script:
12+
- composer install

php/midtrans-php/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Midtrans
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

php/midtrans-php/Midtrans.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Check PHP version.
4+
*/
5+
if (version_compare(PHP_VERSION, '5.4', '<')) {
6+
throw new Exception('PHP version >= 5.4 required');
7+
}
8+
9+
// Check PHP Curl & json decode capabilities.
10+
if (!function_exists('curl_init') || !function_exists('curl_exec')) {
11+
throw new Exception('Midtrans needs the CURL PHP extension.');
12+
}
13+
if (!function_exists('json_decode')) {
14+
throw new Exception('Midtrans needs the JSON PHP extension.');
15+
}
16+
17+
// Configurations
18+
require_once 'Midtrans/Config.php';
19+
20+
// Midtrans API Resources
21+
require_once 'Midtrans/Transaction.php';
22+
23+
// Plumbing
24+
require_once 'Midtrans/ApiRequestor.php';
25+
require_once 'Midtrans/Notification.php';
26+
require_once 'Midtrans/CoreApi.php';
27+
require_once 'Midtrans/Snap.php';
28+
require_once 'SnapBi/SnapBi.php';
29+
require_once 'SnapBi/SnapBiApiRequestor.php';
30+
require_once 'SnapBi/SnapBiConfig.php';
31+
32+
// Sanitization
33+
require_once 'Midtrans/Sanitizer.php';
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
3+
namespace Midtrans;
4+
5+
use Exception;
6+
/**
7+
* Send request to Midtrans API
8+
* Better don't use this class directly, please use CoreApi, Snap, and Transaction instead
9+
*/
10+
11+
class ApiRequestor
12+
{
13+
14+
/**
15+
* Send GET request
16+
*
17+
* @param string $url
18+
* @param string $server_key
19+
* @param mixed[] $data_hash
20+
* @return mixed
21+
* @throws Exception
22+
*/
23+
public static function get($url, $server_key, $data_hash)
24+
{
25+
return self::remoteCall($url, $server_key, $data_hash, 'GET');
26+
}
27+
28+
/**
29+
* Send POST request
30+
*
31+
* @param string $url
32+
* @param string $server_key
33+
* @param mixed[] $data_hash
34+
* @return mixed
35+
* @throws Exception
36+
*/
37+
public static function post($url, $server_key, $data_hash)
38+
{
39+
return self::remoteCall($url, $server_key, $data_hash, 'POST');
40+
}
41+
42+
/**
43+
* Send PATCH request
44+
*
45+
* @param string $url
46+
* @param string $server_key
47+
* @param mixed[] $data_hash
48+
* @return mixed
49+
* @throws Exception
50+
*/
51+
public static function patch($url, $server_key, $data_hash)
52+
{
53+
return self::remoteCall($url, $server_key, $data_hash, 'PATCH');
54+
}
55+
56+
/**
57+
* Actually send request to API server
58+
*
59+
* @param string $url
60+
* @param string $server_key
61+
* @param mixed[] $data_hash
62+
* @param bool $post
63+
* @return mixed
64+
* @throws Exception
65+
*/
66+
public static function remoteCall($url, $server_key, $data_hash, $method)
67+
{
68+
$ch = curl_init();
69+
70+
if (!$server_key) {
71+
throw new Exception(
72+
'The ServerKey/ClientKey is null, You need to set the server-key from Config. Please double-check Config and ServerKey key. ' .
73+
'You can check from the Midtrans Dashboard. ' .
74+
'See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys ' .
75+
'for the details or contact support at support@midtrans.com if you have any questions.'
76+
);
77+
} else {
78+
if ($server_key == "") {
79+
throw new Exception(
80+
'The ServerKey/ClientKey is invalid, as it is an empty string. Please double-check your ServerKey key. ' .
81+
'You can check from the Midtrans Dashboard. ' .
82+
'See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys ' .
83+
'for the details or contact support at support@midtrans.com if you have any questions.'
84+
);
85+
} elseif (preg_match('/\s/',$server_key)) {
86+
throw new Exception(
87+
'The ServerKey/ClientKey is contains white-space. Please double-check your API key. Please double-check your ServerKey key. ' .
88+
'You can check from the Midtrans Dashboard. ' .
89+
'See https://docs.midtrans.com/en/midtrans-account/overview?id=retrieving-api-access-keys ' .
90+
'for the details or contact support at support@midtrans.com if you have any questions.'
91+
);
92+
}
93+
}
94+
95+
96+
$curl_options = array(
97+
CURLOPT_URL => $url,
98+
CURLOPT_HTTPHEADER => array(
99+
'Content-Type: application/json',
100+
'Accept: application/json',
101+
'User-Agent: midtrans-php-v2.6.1',
102+
'Authorization: Basic ' . base64_encode($server_key . ':')
103+
),
104+
CURLOPT_RETURNTRANSFER => 1
105+
);
106+
107+
// Set append notification to header
108+
if (Config::$appendNotifUrl) Config::$curlOptions[CURLOPT_HTTPHEADER][] = 'X-Append-Notification: ' . Config::$appendNotifUrl;
109+
// Set override notification to header
110+
if (Config::$overrideNotifUrl) Config::$curlOptions[CURLOPT_HTTPHEADER][] = 'X-Override-Notification: ' . Config::$overrideNotifUrl;
111+
// Set payment idempotency-key to header
112+
if (Config::$paymentIdempotencyKey) Config::$curlOptions[CURLOPT_HTTPHEADER][] = 'Idempotency-Key: ' . Config::$paymentIdempotencyKey;
113+
114+
// merging with Config::$curlOptions
115+
if (count(Config::$curlOptions)) {
116+
// We need to combine headers manually, because it's array and it will no be merged
117+
if (Config::$curlOptions[CURLOPT_HTTPHEADER]) {
118+
$mergedHeaders = array_merge($curl_options[CURLOPT_HTTPHEADER], Config::$curlOptions[CURLOPT_HTTPHEADER]);
119+
$headerOptions = array(CURLOPT_HTTPHEADER => $mergedHeaders);
120+
} else {
121+
$mergedHeaders = array();
122+
$headerOptions = array(CURLOPT_HTTPHEADER => $mergedHeaders);
123+
}
124+
125+
$curl_options = array_replace_recursive($curl_options, Config::$curlOptions, $headerOptions);
126+
}
127+
128+
if ($method != 'GET') {
129+
130+
if ($data_hash) {
131+
$body = json_encode($data_hash);
132+
$curl_options[CURLOPT_POSTFIELDS] = $body;
133+
} else {
134+
$curl_options[CURLOPT_POSTFIELDS] = '';
135+
}
136+
137+
if ($method == 'POST') {
138+
$curl_options[CURLOPT_POST] = 1;
139+
} elseif ($method == 'PATCH') {
140+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
141+
}
142+
}
143+
144+
curl_setopt_array($ch, $curl_options);
145+
146+
// For testing purpose
147+
if (class_exists('\Midtrans\MT_Tests') && MT_Tests::$stubHttp) {
148+
$result = self::processStubed($curl_options, $url, $server_key, $data_hash, $method);
149+
} else {
150+
$result = curl_exec($ch);
151+
// curl_close($ch);
152+
}
153+
154+
155+
if ($result === false) {
156+
throw new Exception('CURL Error: ' . curl_error($ch), curl_errno($ch));
157+
} else {
158+
try {
159+
$result_array = json_decode($result);
160+
} catch (Exception $e) {
161+
throw new Exception("API Request Error unable to json_decode API response: ".$result . ' | Request url: '.$url);
162+
}
163+
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
164+
if (isset($result_array->status_code) && $result_array->status_code >= 401 && $result_array->status_code != 407) {
165+
throw new Exception('Midtrans API is returning API error. HTTP status code: ' . $result_array->status_code . ' API response: ' . $result, $result_array->status_code);
166+
} elseif ($httpCode >= 400) {
167+
throw new Exception('Midtrans API is returning API error. HTTP status code: ' . $httpCode . ' API response: ' . $result, $httpCode);
168+
} else {
169+
return $result_array;
170+
}
171+
}
172+
}
173+
174+
private static function processStubed($curl, $url, $server_key, $data_hash, $method)
175+
{
176+
MT_Tests::$lastHttpRequest = array(
177+
"url" => $url,
178+
"server_key" => $server_key,
179+
"data_hash" => $data_hash,
180+
$method => $method,
181+
"curl" => $curl
182+
);
183+
184+
return MT_Tests::$stubHttpResponse;
185+
}
186+
}

0 commit comments

Comments
 (0)