PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/veritrans-php/README.md

https://gitlab.com/myurd/vtweb-magento
Markdown | 410 lines | 332 code | 78 blank | 0 comment | 0 complexity | e94ac11ee2c33e3c2df6b7921c263034 MD5 | raw file
  1. Veritrans-PHP
  2. ===============
  3. [![Build Status](https://travis-ci.org/veritrans/veritrans-php.svg)](https://travis-ci.org/veritrans/veritrans-php)
  4. Veritrans :heart: PHP!
  5. This is the all new PHP client library for Veritrans 2.0. This is the official PHP wrapper for Veritrans Payment API. Visit [https://www.veritrans.co.id](https://www.veritrans.co.id) for more information about the product and see documentation at [http://docs.veritrans.co.id](http://docs.veritrans.co.id) for more technical details.
  6. ## Installation
  7. ### Composer Installation
  8. If you are using [Composer](https://getcomposer.org), add this require line to your `composer.json` file:
  9. ```json
  10. {
  11. "require": {
  12. "veritrans/veritrans-php": "dev-master"
  13. }
  14. }
  15. ```
  16. and run `composer install` on your terminal.
  17. ### Manual Instalation
  18. If you are not using Composer, you can clone or [download](https://github.com/veritrans/veritrans-php/archive/master.zip) this repository.
  19. ## How to Use
  20. ### General Settings
  21. #### Set Server Key
  22. ```php
  23. Veritrans_Config::$serverKey = '<your server key>';
  24. ```
  25. #### Set Client Key (VT-Direct)
  26. ```javascript
  27. Veritrans.client_key = "<your client key>";
  28. ```
  29. #### Set Environment
  30. ```php
  31. // Development Environment (the default)
  32. Veritrans_Config::$isProduction = false;
  33. // Production Environment
  34. Veritrans_Config::$isProduction = true;
  35. ```
  36. #### Set Sanitization
  37. ```php
  38. // Set sanitization off (default)
  39. Veritrans_Config::$isSanitized = false;
  40. // Set sanitization on
  41. Veritrans_Config::$isSanitized = true;
  42. ```
  43. ### VT-Web
  44. You can see some VT-Web examples [here](https://github.com/veritrans/veritrans-php/tree/master/examples/vt-web).
  45. #### Get Redirection URL of a Charge
  46. ```php
  47. $params = array(
  48. 'transaction_details' => array(
  49. 'order_id' => rand(),
  50. 'gross_amount' => 10000,
  51. ),
  52. 'vtweb' => array()
  53. );
  54. try {
  55. // Redirect to Veritrans VTWeb page
  56. header('Location: ' . Veritrans_Vtweb::getRedirectionUrl($params));
  57. }
  58. catch (Exception $e) {
  59. echo $e->getMessage();
  60. }
  61. ```
  62. #### Handle Notification Callback
  63. ```php
  64. $notif = new Veritrans_Notification();
  65. $transaction = $notif->transaction_status;
  66. $fraud = $notif->fraud_status;
  67. error_log("Order ID $notif->order_id: "."transaction status = $transaction, fraud staus = $fraud");
  68. if ($transaction == 'capture') {
  69. if ($fraud == 'challenge') {
  70. // TODO Set payment status in merchant's database to 'challenge'
  71. }
  72. else if ($fraud == 'accept') {
  73. // TODO Set payment status in merchant's database to 'success'
  74. }
  75. }
  76. else if ($transaction == 'cancel') {
  77. if ($fraud == 'challenge') {
  78. // TODO Set payment status in merchant's database to 'failure'
  79. }
  80. else if ($fraud == 'accept') {
  81. // TODO Set payment status in merchant's database to 'failure'
  82. }
  83. }
  84. else if ($transaction == 'deny') {
  85. // TODO Set payment status in merchant's database to 'failure'
  86. }
  87. }
  88. ```
  89. ### VT-Direct
  90. You can see some VT-Direct examples [here](https://github.com/veritrans/veritrans-php/tree/master/examples/vt-direct).
  91. #### Checkout Page
  92. ```html
  93. <html>
  94. <head>
  95. <title>Checkout</title>
  96. <link rel="stylesheet" href="jquery.fancybox.css">
  97. </head>
  98. <body>
  99. <script type="text/javascript" src="https://api.sandbox.veritrans.co.id/v2/assets/js/veritrans.js"></script>
  100. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  101. <script type="text/javascript" src="jquery.fancybox.pack.js"></script>
  102. <h1>Checkout</h1>
  103. <form action="checkout-process.php" method="POST" id="payment-form">
  104. <fieldset>
  105. <legend>Checkout</legend>
  106. <p>
  107. <label>Card Number</label>
  108. <input class="card-number" value="4111111111111111" size="20" type="text" autocomplete="off" />
  109. </p>
  110. <p>
  111. <label>Expiration (MM/YYYY)</label>
  112. <input class="card-expiry-month" value="12" placeholder="MM" size="2" type="text" />
  113. <span> / </span>
  114. <input class="card-expiry-year" value="2020" placeholder="YYYY" size="4" type="text" />
  115. </p>
  116. <p>
  117. <label>CVV</label>
  118. <input class="card-cvv" value="123" size="4" type="password" autocomplete="off" />
  119. </p>
  120. <p>
  121. <label>Save credit card</label>
  122. <input type="checkbox" name="save_cc" value="true">
  123. </p>
  124. <input id="token_id" name="token_id" type="hidden" />
  125. <button class="submit-button" type="submit">Submit Payment</button>
  126. </fieldset>
  127. </form>
  128. <!-- Javascript for token generation -->
  129. <script type="text/javascript">
  130. $(function () {
  131. // Sandbox URL
  132. Veritrans.url = "https://api.sandbox.veritrans.co.id/v2/token";
  133. // TODO: Change with your client key.
  134. Veritrans.client_key = "<your client key>";
  135. var card = function () {
  136. return {
  137. "card_number": $(".card-number").val(),
  138. "card_exp_month": $(".card-expiry-month").val(),
  139. "card_exp_year": $(".card-expiry-year").val(),
  140. "card_cvv": $(".card-cvv").val(),
  141. "secure": false,
  142. "gross_amount": 200000
  143. }
  144. };
  145. function callback(response) {
  146. console.log(response);
  147. if (response.redirect_url) {
  148. console.log("3D SECURE");
  149. // 3D Secure transaction, please open this popup
  150. openDialog(response.redirect_url);
  151. }
  152. else if (response.status_code == "200") {
  153. console.log("NOT 3-D SECURE");
  154. // Success 3-D Secure or success normal
  155. closeDialog();
  156. // Submit form
  157. $("#token_id").val(response.token_id);
  158. $("#payment-form").submit();
  159. }
  160. else {
  161. // Failed request token
  162. console.log(response.status_code);
  163. alert(response.status_message);
  164. }
  165. }
  166. function openDialog(url) {
  167. $.fancybox.open({
  168. href: url,
  169. type: "iframe",
  170. autoSize: false,
  171. width: 700,
  172. height: 500,
  173. closeBtn: false,
  174. modal: true
  175. });
  176. }
  177. function closeDialog() {
  178. $.fancybox.close();
  179. }
  180. $(".submit-button").click(function (event) {
  181. console.log("SUBMIT");
  182. event.preventDefault();
  183. $(this).attr("disabled", "disabled");
  184. Veritrans.token(card, callback);
  185. return false;
  186. });
  187. });
  188. </script>
  189. </body>
  190. </html>
  191. ```
  192. #### Checkout Process
  193. ##### 1. Create Transaction Details
  194. ```php
  195. $transaction_details = array(
  196. 'order_id' => time(),
  197. 'gross_amount' => 200000
  198. );
  199. ```
  200. ##### 2. Create Item Details, Billing Address, Shipping Address, and Customer Details (Optional)
  201. ```php
  202. // Populate items
  203. $items = array(
  204. array(
  205. 'id' => 'item1',
  206. 'price' => 100000,
  207. 'quantity' => 1,
  208. 'name' => 'Adidas f50'
  209. ),
  210. array(
  211. 'id' => 'item2',
  212. 'price' => 50000,
  213. 'quantity' => 2,
  214. 'name' => 'Nike N90'
  215. ));
  216. // Populate customer's billing address
  217. $billing_address = array(
  218. 'first_name' => "Andri",
  219. 'last_name' => "Setiawan",
  220. 'address' => "Karet Belakang 15A, Setiabudi.",
  221. 'city' => "Jakarta",
  222. 'postal_code' => "51161",
  223. 'phone' => "081322311801",
  224. 'country_code' => 'IDN'
  225. );
  226. // Populate customer's shipping address
  227. $shipping_address = array(
  228. 'first_name' => "John",
  229. 'last_name' => "Watson",
  230. 'address' => "Bakerstreet 221B.",
  231. 'city' => "Jakarta",
  232. 'postal_code' => "51162",
  233. 'phone' => "081322311801",
  234. 'country_code' => 'IDN'
  235. );
  236. // Populate customer's info
  237. $customer_details = array(
  238. 'first_name' => "Andri",
  239. 'last_name' => "Setiawan",
  240. 'email' => "payment-api@veritrans.co.id",
  241. 'phone' => "081322311801",
  242. 'billing_address' => $billing_address,
  243. 'shipping_address' => $shipping_address
  244. );
  245. ```
  246. ##### 3. Get Token ID from Checkout Page
  247. ```php
  248. // Token ID from checkout page
  249. $token_id = $_POST['token_id'];
  250. ```
  251. ##### 4. Create Transaction Data
  252. ```php
  253. // Transaction data to be sent
  254. $transaction_data = array(
  255. 'payment_type' => 'credit_card',
  256. 'credit_card' => array(
  257. 'token_id' => $token_id,
  258. 'bank' => 'bni',
  259. 'save_token_id' => isset($_POST['save_cc'])
  260. ),
  261. 'transaction_details' => $transaction_details,
  262. 'item_details' => $items,
  263. 'customer_details' => $customer_details
  264. );
  265. ```
  266. ##### 5. Charge
  267. ```php
  268. $response = Veritrans_VtDirect::charge($transaction_data);
  269. ```
  270. ##### 6. Handle Transaction Status
  271. ```php
  272. // Success
  273. if($response->transaction_status == 'capture') {
  274. echo "<p>Transaksi berhasil.</p>";
  275. echo "<p>Status transaksi untuk order id $response->order_id: " .
  276. "$response->transaction_status</p>";
  277. echo "<h3>Detail transaksi:</h3>";
  278. echo "<pre>";
  279. var_dump($response);
  280. echo "</pre>";
  281. }
  282. // Deny
  283. else if($response->transaction_status == 'deny') {
  284. echo "<p>Transaksi ditolak.</p>";
  285. echo "<p>Status transaksi untuk order id .$response->order_id: " .
  286. "$response->transaction_status</p>";
  287. echo "<h3>Detail transaksi:</h3>";
  288. echo "<pre>";
  289. var_dump($response);
  290. echo "</pre>";
  291. }
  292. // Challenge
  293. else if($response->transaction_status == 'challenge') {
  294. echo "<p>Transaksi challenge.</p>";
  295. echo "<p>Status transaksi untuk order id $response->order_id: " .
  296. "$response->transaction_status</p>";
  297. echo "<h3>Detail transaksi:</h3>";
  298. echo "<pre>";
  299. var_dump($response);
  300. echo "</pre>";
  301. }
  302. // Error
  303. else {
  304. echo "<p>Terjadi kesalahan pada data transaksi yang dikirim.</p>";
  305. echo "<p>Status message: [$response->status_code] " .
  306. "$response->status_message</p>";
  307. echo "<pre>";
  308. var_dump($response);
  309. echo "</pre>";
  310. }
  311. ```
  312. #### Process Transaction
  313. ##### Get a Transaction Status
  314. ```php
  315. $status = Veritrans_Transaction::status($orderId);
  316. var_dump($status);
  317. ```
  318. ##### Approve a Transaction
  319. ```php
  320. $approve = Veritrans_Transaction::approve($orderId);
  321. var_dump($approve);
  322. ```
  323. ##### Cancel a Transaction
  324. ```php
  325. $cancel = Veritrans_Transaction::cancel($orderId);
  326. var_dump($cancel);
  327. ```
  328. ## Contributing
  329. ### Developing e-commerce plug-ins
  330. There are several guides that must be taken care of when you develop new plugins.
  331. 1. __Handling currency other than IDR.__ Veritrans `v1` and `v2` currently accepts payments in Indonesian Rupiah only. As a corrolary, there is a validation on the server to check whether the item prices are in integer or not. As much as you are tempted to round-off the price, DO NOT do that! Always prepare when your system uses currencies other than IDR, convert them to IDR accordingly, and only round the price AFTER that.
  332. 2. Consider using the __auto-sanitization__ feature.