|
| 1 | +package com.midtrans.snapbi; |
| 2 | + |
| 3 | +import org.json.JSONObject; |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class SnapBiDirectDebitPayment { |
| 8 | + |
| 9 | + static String externalId = UUID.randomUUID().toString(); |
| 10 | + static String clientId = "Zabcdefg-MIDTRANS-CLIENT-SNAP"; |
| 11 | + static String privateKey = "-----BEGIN PRIVATE KEY-----" |
| 12 | + +"ABcDeFGHijklMnOpQrsU/6osrLPS6TXpoqo0ozgfjDD1B1TzfkJZ0AF/5oLYEVCdsMufKLcaVKBZDC32ZVSQ3z7sa85rTOiYfm+Cje/S6FpvnC0ovknjshJhPXRBooitL2kF1o3yhuhak2V99Mvxyy79AnF1xuYLpfnlTYk5yhVuvnToBlflS3twp5NTivl143T/K38T42FAfl4uABNsxQHnvA7ZQApyNW5wZiOx41PyXWmf2Xp/YyqZ5x56Yui+ZLtqxZpZqyvBX7idjhBZnnUtg7d7IVDdRtXNn7XJVAgMBAAECggEAcu5amQOjUFBzNhnXrv+tNFQT0oL77CP83r63o7k5xnNzgLLBVw6x+uOvFqV6un4TlswUOSvoeYCNxHFlbxj7rfhaBUYyBEPV87j/LCADVr/VaSnB9cfefkyv4fnl+b/nyNT9kiduVlvcvGsNrlaf0braAJxEkmoFccpr/vE7fXwPfO8X7Y9dFvlo+HrzVBRYGcKpzKR7NQltV+mGaDc4tnAOHfZ4SOZveqGFudZ46bTlQ55OkRS1aRpj7GvOSETHpWQwiMv3jJdVfQTCUKOYpdkWiKa+xVzUbVrXVsFd2x1yDxcc7Aj+WcZWlQKBgQDMyifSua/xOtbGaqb7ukKWk1LalXB2Xmwcmg20PK2/UAjGsxCY8sHuBtDEeU5V6v8AT5M+g9z4bFwWBmOmc1kMVh/FZp1jPB783LNKSuK/9fcWjINEI6vAUoKvTApS2rP/1qLg/EokO/luPWVwKBgQCm+RN92x889jmZtfddeOLX3p8+Up+c58h8n0yOSLyWm3vZ8OtlfVWBG/wkLwopQAN8pcISVKTzdemPnjRRJ/GXQdhrXGOBbGOBOm/TpV2snJWEOrJs3oTBQtD64FPT41zzecdPG70YRjwTSe9hvolHVp/HIhTAP2q65e4uGrbZMwKBgQCPWZbEsAxTrN84puFaZghEhL3DmkXN5cyBvOiI2MyRfZKpwcwsL4455U/NYS/R5w60Sb8/cgt6mPlqZfas/qKAvIoqV7Xzhv0zfzgU54cGKC2k1kEElPEcRr+WKrmAW8pGzS+GbMlvEv2lRYXKlz8aNsWgETvffny1JOyNiR7VIwKBgGf6FTw7dsF0pzvkB81qWQCKfmnF8+U2pS+N15OHEgFLg52dHElP+i3zKlmx/UbguQsnAap+kVWWqMIph3S+RgCmHUoF92UCBRjVNgv7H1E9FRKJptCK5OqgXBjOFwWMwTZRMF8/68ow+nocgXhBuaT1+Xt86qUpGKKYSnDla9XnAoGAD5Tt/NGy5tZid9+NBXXU7yNSmJ5t6KLsN7VWlSq30ezJFuPSCE+rWkXyPfhmY/zMDbPmGUCGK+iE5vgefm6QgtaR38f4EfNl1z6o4KlDRUllJjNqdw+894gjd+Aj4f3FmgSZa4BlZTdQcHWh/abcdeFg1234=" |
| 13 | + +"-----END PRIVATE KEY-----"; |
| 14 | + static String clientSecret = "ABcdefghiSrLJPgKRXqdjaSAuj5WDAbeaXAX8Vn7CWGHuBCfFgABCDVqRLvNZf8BaqPGKaksMjrZDrZqzZEbaA1AYFwBewIWCqLZr4PuvuLBqfTmYIzAbCakHKejABCa"; |
| 15 | + static String partnerId = "partner-id"; |
| 16 | + static String merchantId = "M001234"; |
| 17 | + static String channelId = "12345"; |
| 18 | + public static void main(String[] args) throws Exception { |
| 19 | + |
| 20 | + SnapBiConfig.setSnapBiClientId(clientId); |
| 21 | + SnapBiConfig.setSnapBiPrivateKey(privateKey); |
| 22 | + SnapBiConfig.setSnapBiClientSecret(clientSecret); |
| 23 | + SnapBiConfig.setSnapBiPartnerId(partnerId); |
| 24 | + SnapBiConfig.setSnapBiChannelId(channelId); |
| 25 | + SnapBiConfig.setEnableLogging(true); |
| 26 | + |
| 27 | + /* |
| 28 | + * Example code for Direct Debit (gopay/ dana/ shopeepay) using Snap Bi, you can uncomment and run the code. |
| 29 | + * Below are example code to create the payment |
| 30 | + */ |
| 31 | + |
| 32 | + /* |
| 33 | + * Basic example |
| 34 | + * To change the payment method, you can change the value of the request body on the `payOptionDetails` |
| 35 | + */ |
| 36 | + JSONObject snapBiResponse1 = SnapBi.directDebit() |
| 37 | + .withBody(createDirectDebitRequestBody()) |
| 38 | + .createPayment(externalId); |
| 39 | + |
| 40 | + /* |
| 41 | + * Example of using existing access token to create payment. You can uncomment and run the code |
| 42 | + * To change the payment method, you can change the value of the request body on the `payOptionDetails` |
| 43 | + */ |
| 44 | + JSONObject snapBiResponse2 = SnapBi.directDebit() |
| 45 | + .withBody(createDirectDebitRequestBody()) |
| 46 | + .withAccessToken("") |
| 47 | + .createPayment(externalId); |
| 48 | + |
| 49 | + /* |
| 50 | + * Example of using additional header on access token and when doing transaction header. |
| 51 | + * To change the payment method, you can change the value of the request body on the `payOptionDetails` |
| 52 | + */ |
| 53 | + JSONObject snapBiResponse3 = SnapBi.directDebit() |
| 54 | + .withBody(createDirectDebitRequestBody()) |
| 55 | + .withAccessTokenHeader(createAdditionalHeader()) |
| 56 | + .withTransactionHeader(createAdditionalHeader()) |
| 57 | + .createPayment(externalId); |
| 58 | + |
| 59 | + System.out.println("Snap Bi response3 : " + snapBiResponse3); |
| 60 | + } |
| 61 | + public static Map<String, Object> createDirectDebitRequestBody() { |
| 62 | + // Create the top-level map |
| 63 | + Map<String, Object> requestBody = new HashMap<>(); |
| 64 | + |
| 65 | + // Add partnerReferenceNo and other fields |
| 66 | + requestBody.put("partnerReferenceNo", externalId); |
| 67 | + requestBody.put("chargeToken", ""); |
| 68 | + requestBody.put("merchantId", merchantId); |
| 69 | + requestBody.put("validUpTo", "2030-07-20T20:34:15.452305Z"); |
| 70 | + |
| 71 | + // Create and add urlParam map |
| 72 | + Map<String, String> urlParam = new HashMap<>(); |
| 73 | + urlParam.put("url", "https://midtrans-test.com/api/notification"); |
| 74 | + urlParam.put("type", "PAY_RETURN"); |
| 75 | + urlParam.put("isDeeplink", "N"); |
| 76 | + requestBody.put("urlParam", urlParam); |
| 77 | + |
| 78 | + // Create and add payOptionDetails list |
| 79 | + List<Map<String, Object>> payOptionDetails = new ArrayList<>(); |
| 80 | + Map<String, Object> payOptionDetail = new HashMap<>(); |
| 81 | + payOptionDetail.put("payMethod", "GOPAY"); |
| 82 | + payOptionDetail.put("payOption", "GOPAY_WALLET"); |
| 83 | + |
| 84 | + // Create and add transAmount map |
| 85 | + Map<String, String> transAmount = new HashMap<>(); |
| 86 | + transAmount.put("value", "1500"); |
| 87 | + transAmount.put("currency", "IDR"); |
| 88 | + payOptionDetail.put("transAmount", transAmount); |
| 89 | + |
| 90 | + payOptionDetails.add(payOptionDetail); |
| 91 | + requestBody.put("payOptionDetails", payOptionDetails); |
| 92 | + |
| 93 | + // Create and add additionalInfo map |
| 94 | + Map<String, Object> additionalInfo = new HashMap<>(); |
| 95 | + |
| 96 | + // Create and add customerDetails map |
| 97 | + Map<String, Object> customerDetails = new HashMap<>(); |
| 98 | + customerDetails.put("firstName", "Merchant"); |
| 99 | + customerDetails.put("lastName", "Operation"); |
| 100 | + customerDetails.put("email", "merchant-ops@midtrans.com"); |
| 101 | + customerDetails.put("phone", "+6281932358123"); |
| 102 | + |
| 103 | + // Create and add billingAddress map |
| 104 | + Map<String, String> billingAddress = new HashMap<>(); |
| 105 | + billingAddress.put("firstName", "Merchant"); |
| 106 | + billingAddress.put("lastName", "Operation"); |
| 107 | + billingAddress.put("phone", "+6281932358123"); |
| 108 | + billingAddress.put("address", "Pasaraya Blok M"); |
| 109 | + billingAddress.put("city", "Jakarta"); |
| 110 | + billingAddress.put("postalCode", "12160"); |
| 111 | + billingAddress.put("countryCode", "IDN"); |
| 112 | + customerDetails.put("billingAddress", billingAddress); |
| 113 | + |
| 114 | + // Create and add shippingAddress map |
| 115 | + Map<String, String> shippingAddress = new HashMap<>(); |
| 116 | + shippingAddress.put("firstName", "Merchant"); |
| 117 | + shippingAddress.put("lastName", "Operation"); |
| 118 | + shippingAddress.put("phone", "+6281932358123"); |
| 119 | + shippingAddress.put("address", "Pasaraya Blok M"); |
| 120 | + shippingAddress.put("city", "Jakarta"); |
| 121 | + shippingAddress.put("postalCode", "12160"); |
| 122 | + shippingAddress.put("countryCode", "IDN"); |
| 123 | + customerDetails.put("shippingAddress", shippingAddress); |
| 124 | + |
| 125 | + additionalInfo.put("customerDetails", customerDetails); |
| 126 | + |
| 127 | + // Create and add items list |
| 128 | + List<Map<String, Object>> items = new ArrayList<>(); |
| 129 | + Map<String, Object> item = new HashMap<>(); |
| 130 | + item.put("id", "8143fc4f-ec05-4c55-92fb-620c212f401e"); |
| 131 | + |
| 132 | + // Create and add price map |
| 133 | + Map<String, String> price = new HashMap<>(); |
| 134 | + price.put("value", "1500.00"); |
| 135 | + price.put("currency", "IDR"); |
| 136 | + item.put("price", price); |
| 137 | + |
| 138 | + item.put("quantity", 1); |
| 139 | + item.put("name", "test item name"); |
| 140 | + item.put("brand", "test item brand"); |
| 141 | + item.put("category", "test item category"); |
| 142 | + item.put("merchantName", "Merchant Operation"); |
| 143 | + |
| 144 | + items.add(item); |
| 145 | + additionalInfo.put("items", items); |
| 146 | + |
| 147 | + requestBody.put("additionalInfo", additionalInfo); |
| 148 | + |
| 149 | + return requestBody; |
| 150 | + } |
| 151 | + public static Map<String, String > createAdditionalHeader(){ |
| 152 | + Map<String, String> headers = new HashMap<>(); |
| 153 | + headers.put("X-Device-id", "device id"); |
| 154 | + headers.put("debug-id", "debug id"); |
| 155 | + return headers; |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments