Skip to content

Commit a56c980

Browse files
authored
Merge pull request #102 from Midtrans/snapbi-docs
Snapbi docs
2 parents bbe195d + 5af74ef commit a56c980

File tree

9 files changed

+1320
-509
lines changed

9 files changed

+1320
-509
lines changed

README.md

Lines changed: 403 additions & 1 deletion
Large diffs are not rendered by default.

SnapBi/SnapBi.php

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
namespace SnapBi;
44

5-
use Midtrans\Config;
6-
75
/**
8-
* API methods to get transaction status, approve and cancel transactions
6+
* Provide Snap-Bi functionalities (create transaction, refund, cancel, get status)
97
*/
108
class SnapBi
119
{
1210

1311
const ACCESS_TOKEN = '/v1.0/access-token/b2b';
1412
const PAYMENT_HOST_TO_HOST = '/v1.0/debit/payment-host-to-host';
1513
const CREATE_VA = '/v1.0/transfer-va/create-va';
16-
const STATUS = '/v1.0/debit/status';
17-
const REFUND = '/v1.0/debit/refund';
18-
const CANCEL = '/v1.0/debit/cancel';
14+
const DEBIT_STATUS = '/v1.0/debit/status';
15+
const DEBIT_REFUND = '/v1.0/debit/refund';
16+
const DEBIT_CANCEL = '/v1.0/debit/cancel';
17+
const VA_STATUS = '/v1.0/transfer-va/status';
18+
const VA_CANCEL = '/v1.0/transfer-va/delete-va';
1919
private $apiPath;
20+
private $paymentMethod;
2021
private $accessTokenHeader = [];
2122
private $transactionHeader = [];
2223
private $accessToken;
@@ -30,54 +31,68 @@ class SnapBi
3031
private $debugId;
3132
private $timeStamp;
3233

33-
public function __construct($apiUrl)
34+
public function __construct($paymentMethod)
3435
{
35-
$this->apiPath = $apiUrl;
36+
$this->paymentMethod = $paymentMethod;
3637
$this->timeStamp = date("c");
3738
}
3839

40+
/**
41+
* this method chain is used to start Direct Debit (Gopay, Shopeepay, Dana) related transaction
42+
*/
43+
3944
public static function directDebit()
4045
{
41-
$apiPath = self::PAYMENT_HOST_TO_HOST;
42-
return new self($apiPath);
46+
return new self("directDebit");
4347
}
4448

49+
/**
50+
* this method chain is used to start VA(Bank Transfer) related transaction
51+
*/
4552
public static function va()
4653
{
47-
$apiPath = self::CREATE_VA;
48-
return new self($apiPath);
49-
}
50-
51-
public static function transaction()
52-
{
53-
$apiPath = "";
54-
return new self($apiPath);
54+
return new self("va");
5555
}
5656

57+
/**
58+
* this method chain is used to add additional header during access token request
59+
*/
5760
public function withAccessTokenHeader(array $headers)
5861
{
5962
$this->accessTokenHeader = array_merge($this->accessTokenHeader, $headers);
6063
return $this;
6164
}
6265

66+
/**
67+
* this method chain is used to add additional header during transaction process (create payment/ get status/ refund/ cancel)
68+
*/
6369
public function withTransactionHeader(array $headers)
6470
{
6571
$this->transactionHeader = array_merge($this->transactionHeader, $headers);
6672
return $this;
6773
}
6874

75+
/**
76+
* this method chain is used to supply access token that you already have, and want to re-use
77+
*/
6978
public function withAccessToken($accessToken)
7079
{
7180
$this->accessToken = $accessToken;
7281
return $this;
7382
}
7483

84+
/**
85+
* this method chain is used to supply the request body/ payload
86+
*/
7587
public function withBody(array $body)
7688
{
7789
$this->body = $body;
7890
return $this;
7991
}
8092

93+
/**
94+
* These method chains below are config related method chain that can be used as an option
95+
*/
8196
public function withPrivateKey($privateKey)
8297
{
8398
SnapBiConfig::$snapBiPrivateKey = $privateKey;
@@ -120,29 +135,46 @@ public function withDebuglId($debugId)
120135
return $this;
121136
}
122137

138+
/**
139+
* these method chain is used to execute create payment
140+
*/
123141
public function createPayment($externalId)
124142
{
143+
$this->apiPath = $this->setupCreatePaymentApiPath($this->paymentMethod);
125144
return $this->createConnection($externalId);
126145
}
127146

147+
/**
148+
* these method chain is used to cancel the transaction
149+
*/
128150
public function cancel($externalId)
129151
{
130-
$this->apiPath = self::CANCEL;
152+
$this->apiPath = $this->setupCancelApiPath($this->paymentMethod);
131153
return $this->createConnection($externalId);
132154
}
133155

156+
/**
157+
* these method chain is used to refund the transaction
158+
*/
134159
public function refund($externalId)
135160
{
136-
$this->apiPath = self::REFUND;
161+
$this->apiPath = $this->setupRefundApiPath($this->paymentMethod);
137162
return $this->createConnection($externalId);
138163
}
139164

165+
/**
166+
* these method chain is used to get the status of the transaction
167+
*/
140168
public function getStatus($externalId)
141169
{
142-
$this->apiPath = self::STATUS;
170+
$this->apiPath = $this->setupGetStatusApiPath($this->paymentMethod);
143171
return $this->createConnection($externalId);
144172
}
145173

174+
175+
/**
176+
* these method chain is used to get the access token
177+
*/
146178
public function getAccessToken()
147179
{
148180
$snapBiAccessTokenHeader = $this->buildAccessTokenHeader($this->timeStamp);
@@ -152,11 +184,11 @@ public function getAccessToken()
152184
return SnapBiApiRequestor::remoteCall(SnapBiConfig::getSnapBiTransactionBaseUrl() . self::ACCESS_TOKEN, $snapBiAccessTokenHeader, $openApiPayload);
153185
}
154186

155-
public function createConnection($externalId = null)
187+
private function createConnection($externalId = null)
156188
{
157189
// Attempt to get the access token if it's not already set
158190
if (!$this->accessToken) {
159-
$access_token_response = $this->getAccessToken($this->timeStamp);
191+
$access_token_response = $this->getAccessToken();
160192

161193
// If getting the access token failed, return the response from getAccessToken
162194
if (!isset($access_token_response->accessToken)) {
@@ -247,4 +279,38 @@ private function buildAccessTokenHeader($timeStamp)
247279
}
248280
return $snapBiAccessTokenHeader;
249281
}
282+
283+
private function setupCreatePaymentApiPath($paymentMethod)
284+
{
285+
switch ($paymentMethod) {
286+
case "va":
287+
return self::CREATE_VA;
288+
default:
289+
return self::PAYMENT_HOST_TO_HOST;
290+
}
291+
}
292+
private function setupRefundApiPath($paymentMethod)
293+
{
294+
return self::DEBIT_REFUND;
295+
}
296+
297+
private function setupCancelApiPath($paymentMethod)
298+
{
299+
switch ($paymentMethod) {
300+
case "va":
301+
return self::VA_CANCEL;
302+
default:
303+
return self::DEBIT_CANCEL;
304+
}
305+
}
306+
307+
private function setupGetStatusApiPath($paymentMethod)
308+
{
309+
switch ($paymentMethod) {
310+
case "va":
311+
return self::VA_STATUS;
312+
default:
313+
return self::DEBIT_STATUS;
314+
}
315+
}
250316
}

SnapBi/SnapBiConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class SnapBiConfig
1313
public static $snapBiClientSecret;
1414
public static $snapBiPartnerId;
1515
public static $snapBiChannelId;
16+
public static $enableLogging = false;
1617

1718
const SNAP_BI_SANDBOX_BASE_URL = 'https://merchants.sbx.midtrans.com';
1819
const SNAP_BI_PRODUCTION_BASE_URL = 'https://merchants.midtrans.com';

examples/snap-bi/snap-bi-cancel.php

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
// This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
3+
4+
namespace Midtrans;
5+
6+
use SnapBi\SnapBi;
7+
use SnapBi\SnapBiConfig;
8+
9+
require_once dirname(__FILE__) . '/../../Midtrans.php';
10+
/**
11+
* SETUP YOUR CREDENTIALS HERE
12+
*/
13+
14+
$client_id = "Zabcdefg-MIDTRANS-CLIENT-SNAP";
15+
16+
//make sure to add 3 newline "\n" to your private key as shown below
17+
$private_key = "-----BEGIN PRIVATE KEY-----\nABCDEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7Zk6kJjqamLddaN1lK03XJW3vi5zOSA7V+5eSiYeM9tCOGouJewN/Py58wgvRh7OMAMm1IbSZpAbcZbBa1=\n-----END PRIVATE KEY-----\n";
18+
$client_secret = "ABcdefghiSrLJPgKRXqdjaSAuj5WDAbeaXAX8Vn7CWGHuBCfFgABCDVqRLvNZf8BaqPGKaksMjrZDrZqzZEbaA1AYFwBewIWCqLZr4PuvuLBqfTmYIzAbCakHKejABCa";
19+
$partner_id = "partner-id";
20+
$merchant_id = "M001234";
21+
$channel_id = "12345";
22+
$va_merchant_id = "G012345677";
23+
24+
$external_id = "uzi-order-testing" . uniqid();
25+
26+
$directDebitCancelByReferenceBody = array(
27+
"originalReferenceNo" => "A1202409071547203VpKvjM8MrID"
28+
);
29+
$directDebitCancelByExternalIdBody = array(
30+
"originalExternalId" => "uzi-order-testing66dc75ab3b96c"
31+
);
32+
33+
$vaCancelBody = array(
34+
"partnerServiceId" => " 5818",
35+
"customerNo" => "628014506680",
36+
"virtualAccountNo" => " 5818628014506680",
37+
"trxId" => "uzi-order-testing66dc76754bf1c",
38+
"additionalInfo" => array(
39+
"merchantId" => $va_merchant_id
40+
)
41+
);
42+
43+
44+
$snapBiResponse = null;
45+
SnapBiConfig::$snapBiClientId = $client_id;
46+
SnapBiConfig::$snapBiPrivateKey = $private_key;
47+
SnapBiConfig::$snapBiClientSecret = $client_secret;
48+
SnapBiConfig::$snapBiPartnerId = $partner_id;
49+
SnapBiConfig::$snapBiChannelId = $partner_id;
50+
SnapBiConfig::$snapBiChannelId = $channel_id;
51+
52+
try {
53+
54+
/**
55+
* Example code for SnapBI, you can uncomment and run the code.
56+
* To cancel transaction you can use externalId or referenceNo.
57+
* The difference is based on the request body/ payload.
58+
* you can refer to the variable $directDebitCancelByReferenceBody or $directDebitCancelByExternalIdBody (for direct debit) or $vaCancelBody (for va) to see the value.
59+
*
60+
* Below are example code to cancel the transaction.
61+
*/
62+
63+
/**
64+
* Example code for Direct Debit cancel using externalId
65+
*/
66+
67+
/**
68+
* Basic implementation of Direct Debit to cancel transaction
69+
*/
70+
$snapBiResponse = SnapBi::directDebit()
71+
->withBody($directDebitCancelByExternalIdBody)
72+
->cancel($external_id);
73+
74+
/**
75+
* Example code of Direct Debit to cancel transaction using your existing access token
76+
*/
77+
$snapBiResponse = SnapBi::directDebit()
78+
->withAccessToken("")
79+
->withBody($directDebitCancelByExternalIdBody)
80+
->cancel($external_id);
81+
82+
/**
83+
* Example code of Direct Debit to cancel transaction by adding or overriding the accessTokenHeader and TranasctionHeader
84+
*/
85+
$snapBiResponse = SnapBi::directDebit()
86+
->withBody($directDebitCancelByExternalIdBody)
87+
->withAccessTokenHeader([
88+
"CHANNEL-ID" => "12345"
89+
])
90+
->withTransactionHeader([
91+
"CHANNEL-ID" => "12345"
92+
])
93+
->cancel($external_id);
94+
95+
/**
96+
* Example code for Direct Debit to cancel using referenceNo
97+
*/
98+
99+
/**
100+
* Basic implementation of Direct Debit to cancel transaction
101+
*/
102+
$snapBiResponse = SnapBi::directDebit()
103+
->withBody($directDebitCancelByReferenceBody)
104+
->cancel($external_id);
105+
106+
/**
107+
* Example code of Direct Debit to cancel transaction using your existing access token
108+
*/
109+
$snapBiResponse = SnapBi::directDebit()
110+
->withAccessToken("")
111+
->withBody($directDebitCancelByReferenceBody)
112+
->cancel($external_id);
113+
114+
/**
115+
* Example code of Direct Debit to cancel transaction by adding or overriding the accessTokenHeader and TransactionHeader
116+
*/
117+
$snapBiResponse = SnapBi::directDebit()
118+
->withBody($directDebitCancelByReferenceBody)
119+
->withAccessTokenHeader([
120+
"CHANNEL-ID" => "12345"
121+
])
122+
->withTransactionHeader([
123+
"CHANNEL-ID" => "12345"
124+
])
125+
->cancel($external_id);
126+
127+
/**
128+
* Example code for VA (Bank Transfer) to cancel transaction
129+
*/
130+
131+
/**
132+
* Basic implementation of VA (Bank Transfer) to cancel transaction
133+
*/
134+
$snapBiResponse = SnapBi::va()
135+
->withBody($vaCancelBody)
136+
->cancel($external_id);
137+
138+
/**
139+
* Example code of VA (Bank Transfer) to cancel transaction using your existing access token
140+
*/
141+
$snapBiResponse = SnapBi::va()
142+
->withAccessToken("")
143+
->withBody($vaCancelBody)
144+
->cancel($external_id);
145+
146+
/**
147+
* Example code of VA (Bank Transfer) to cancel transaction by adding or overriding the accessTokenHeader and TransactionHeader
148+
*/
149+
$snapBiResponse = SnapBi::va()
150+
->withBody($vaCancelBody)
151+
->withAccessTokenHeader([
152+
"CHANNEL-ID" => "12345"
153+
])
154+
->withTransactionHeader([
155+
"CHANNEL-ID" => "12345"
156+
])
157+
->cancel($external_id);
158+
159+
} catch (\Exception $e) {
160+
echo $e->getMessage();
161+
}
162+
echo "snap bi response = " . print_r($snapBiResponse, true), PHP_EOL;
163+
164+
function generateRandomNumber()
165+
{
166+
$prefix = "6280"; // Fixed prefix
167+
$randomDigits = mt_rand(100000000, 999999999); // Generate 9 random digits
168+
return $prefix . $randomDigits;
169+
}
170+

0 commit comments

Comments
 (0)