├── README.md ├── app ├── Exceptions │ └── VeritransException.php ├── Http │ ├── Controllers │ │ ├── SnapController.php │ │ ├── TransactionController.php │ │ ├── VtdirectController.php │ │ └── VtwebController.php │ └── routes.php └── Veritrans │ ├── Midtrans.php │ ├── Veritrans.php │ └── data │ └── cacert.pem ├── public ├── css │ └── jquery.fancybox.css └── js │ └── jquery.fancybox.pack.js └── resources └── views ├── checkout.blade.php ├── snap_checkout.blade.php └── transaction.blade.php /README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------- 3 | 4 | > **:warning::warning: ANNOUNCEMENT :warning::warning:** 5 | 6 | **Please use the updated and composer compatible library: [Midtrans PHP](https://github.com/Midtrans/midtrans-php). 7 | For better more modern composer compatibility.** 8 | 9 | This repo still be here for archive and compatibility purpose. But it's always recommended to use the newer version [Midtrans PHP](https://github.com/Midtrans/midtrans-php). 10 | 11 | > **:speaker: END OF ANNOUNCEMENT :speaker:** 12 | 13 | --------------------- 14 | 15 | 16 | 17 | Veritrans Laravel library 18 | ======================================================= 19 | Veritrans :heart: Laravel! 20 | 21 | This is the all new Laravel client library for Veritrans 2.0. 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. 22 | 23 | ### Requirements 24 | The following plugin is tested under following environment: 25 | * PHP v5.4.x or greater 26 | * Laravel 5 27 | 28 | ## Installation 29 | * Download the library and extract the .zip 30 | * Merge all the files. 31 | * install from composer (Soon :) ) 32 | 33 | ## Using Veritrans Library 34 | 35 | ### Use Veritrans Class 36 | ###### Add this following line in your controller 37 | ```php 38 | //before 39 | namespace App\Http\Controllers; 40 | use Illuminate\Http\Request; 41 | use App\Http\Requests; 42 | use App\Http\Controllers\Controller; 43 | 44 | class YourController extends Controller 45 | { 46 | ... 47 | 48 | // after 49 | namespace App\Http\Controllers; 50 | use Illuminate\Http\Request; 51 | use App\Http\Requests; 52 | use App\Http\Controllers\Controller; 53 | 54 | use App\Veritrans\Veritrans; 55 | 56 | class YourController extends Controller 57 | { 58 | ... 59 | ``` 60 | ###### Add/Edit this following line in your __construct() function 61 | ```php 62 | //set $isproduction to true for prodution environment 63 | //before 64 | class YourController extends Controller 65 | { 66 | 67 | ... 68 | 69 | // after 70 | use App\Veritrans\Veritrans; 71 | class YourController extends Controller 72 | { 73 | public function __construct(){ 74 | Veritrans::$serverKey = 'your-server-key'; 75 | Veritrans::$isProduction = false; 76 | } 77 | ... 78 | ``` 79 | ### SNAP 80 | You can see how to get snap token by reading the controller [here](https://github.com/harrypujianto/Veritrans-Laravel5/blob/master/app/Http/Controllers/SnapController.php). 81 | 82 | #### Get Snap Token 83 | ```php 84 | public function token() 85 | { 86 | error_log('masuk ke snap token adri ajax'); 87 | $midtrans = new Midtrans; 88 | $transaction_details = array( 89 | 'order_id' => uniqid(), 90 | 'gross_amount' => 200000 91 | ); 92 | // Populate items 93 | $items = [ 94 | array( 95 | 'id' => 'item1', 96 | 'price' => 100000, 97 | 'quantity' => 1, 98 | 'name' => 'Adidas f50' 99 | ), 100 | array( 101 | 'id' => 'item2', 102 | 'price' => 50000, 103 | 'quantity' => 2, 104 | 'name' => 'Nike N90' 105 | ) 106 | ]; 107 | // Populate customer's billing address 108 | $billing_address = array( 109 | 'first_name' => "Andri", 110 | 'last_name' => "Setiawan", 111 | 'address' => "Karet Belakang 15A, Setiabudi.", 112 | 'city' => "Jakarta", 113 | 'postal_code' => "51161", 114 | 'phone' => "081322311801", 115 | 'country_code' => 'IDN' 116 | ); 117 | // Populate customer's shipping address 118 | $shipping_address = array( 119 | 'first_name' => "John", 120 | 'last_name' => "Watson", 121 | 'address' => "Bakerstreet 221B.", 122 | 'city' => "Jakarta", 123 | 'postal_code' => "51162", 124 | 'phone' => "081322311801", 125 | 'country_code'=> 'IDN' 126 | ); 127 | // Populate customer's Info 128 | $customer_details = array( 129 | 'first_name' => "Andri", 130 | 'last_name' => "Setiawan", 131 | 'email' => "andrisetiawan@asdasd.com", 132 | 'phone' => "081322311801", 133 | 'billing_address' => $billing_address, 134 | 'shipping_address'=> $shipping_address 135 | ); 136 | // Data yang akan dikirim untuk request redirect_url. 137 | $transaction_data = array( 138 | 'transaction_details'=> $transaction_details, 139 | 'item_details' => $items, 140 | 'customer_details' => $customer_details 141 | ); 142 | 143 | try 144 | { 145 | $snap_token = $midtrans->getSnapToken($transaction_data); 146 | //return redirect($vtweb_url); 147 | echo $snap_token; 148 | } 149 | catch (Exception $e) 150 | { 151 | return $e->getMessage; 152 | } 153 | } 154 | ``` 155 | 156 | #### SNAP UI 157 | In this section you could see the code, how to get snap token with ajax and open the snap pop up on the page. Please refer [here](https://github.com/harrypujianto/Veritrans-Laravel5/blob/master/resources/views/snap_checkout.blade.php) 158 | 159 | For sandbox use https://app.sandbox.midtrans.com/snap/snap.js 160 | For production use https://app.midtrans.com/snap/snap.js 161 | ``` 162 | 163 |
Transaksi berhasil.
"; 608 | echo "Status transaksi untuk order id $response->order_id: " . 609 | "$response->transaction_status
"; 610 | 611 | echo ""; 613 | var_dump($response); 614 | echo ""; 615 | } 616 | // Deny 617 | else if($response->transaction_status == 'deny') { 618 | echo "
Transaksi ditolak.
"; 619 | echo "Status transaksi untuk order id .$response->order_id: " . 620 | "$response->transaction_status
"; 621 | 622 | echo ""; 624 | var_dump($response); 625 | echo ""; 626 | } 627 | // Challenge 628 | else if($response->transaction_status == 'challenge') { 629 | echo "
Transaksi challenge.
"; 630 | echo "Status transaksi untuk order id $response->order_id: " . 631 | "$response->transaction_status
"; 632 | 633 | echo ""; 635 | var_dump($response); 636 | echo ""; 637 | } 638 | // Error 639 | else { 640 | echo "
Terjadi kesalahan pada data transaksi yang dikirim.
"; 641 | echo "Status message: [$response->status_code] " . 642 | "$response->status_message
"; 643 | 644 | echo ""; 645 | var_dump($response); 646 | echo ""; 647 | } 648 | ``` 649 | 650 | #### Process Transaction 651 | More details can be found [here](https://github.com/harrypujianto/Veritrans-Laravel5/blob/master/app/Http/Controllers/TransactionController.php) 652 | 653 | Don't forget to create new veritrans object 654 | ```php 655 | //creating new veritrans object 656 | $vt = new Veritrans; 657 | ``` 658 | ##### Get a Transaction Status 659 | ```php 660 | $status = $vt->status($order_id); 661 | var_dump($status); 662 | ``` 663 | ##### Approve a Transaction 664 | ```php 665 | $approve = $vt->approve($order_id); 666 | var_dump($approve); 667 | ``` 668 | ##### Cancel a Transaction 669 | ```php 670 | $cancel = $vt->cancel($order_id); 671 | var_dump($cancel); 672 | ``` 673 | -------------------------------------------------------------------------------- /app/Exceptions/VeritransException.php: -------------------------------------------------------------------------------- 1 | uniqid(), 31 | 'gross_amount' => 200000 32 | ); 33 | 34 | // Populate items 35 | $items = [ 36 | array( 37 | 'id' => 'item1', 38 | 'price' => 100000, 39 | 'quantity' => 1, 40 | 'name' => 'Adidas f50' 41 | ), 42 | array( 43 | 'id' => 'item2', 44 | 'price' => 50000, 45 | 'quantity' => 2, 46 | 'name' => 'Nike N90' 47 | ) 48 | ]; 49 | 50 | // Populate customer's billing address 51 | $billing_address = array( 52 | 'first_name' => "Andri", 53 | 'last_name' => "Setiawan", 54 | 'address' => "Karet Belakang 15A, Setiabudi.", 55 | 'city' => "Jakarta", 56 | 'postal_code' => "51161", 57 | 'phone' => "081322311801", 58 | 'country_code' => 'IDN' 59 | ); 60 | 61 | // Populate customer's shipping address 62 | $shipping_address = array( 63 | 'first_name' => "John", 64 | 'last_name' => "Watson", 65 | 'address' => "Bakerstreet 221B.", 66 | 'city' => "Jakarta", 67 | 'postal_code' => "51162", 68 | 'phone' => "081322311801", 69 | 'country_code' => 'IDN' 70 | ); 71 | 72 | // Populate customer's Info 73 | $customer_details = array( 74 | 'first_name' => "Andri", 75 | 'last_name' => "Setiawan", 76 | 'email' => "andrisetiawan@asdasd.com", 77 | 'phone' => "081322311801", 78 | 'billing_address' => $billing_address, 79 | 'shipping_address'=> $shipping_address 80 | ); 81 | 82 | // Data yang akan dikirim untuk request redirect_url. 83 | $credit_card['secure'] = true; 84 | //ser save_card true to enable oneclick or 2click 85 | //$credit_card['save_card'] = true; 86 | 87 | $time = time(); 88 | $custom_expiry = array( 89 | 'start_time' => date("Y-m-d H:i:s O",$time), 90 | 'unit' => 'hour', 91 | 'duration' => 2 92 | ); 93 | 94 | $transaction_data = array( 95 | 'transaction_details'=> $transaction_details, 96 | 'item_details' => $items, 97 | 'customer_details' => $customer_details, 98 | 'credit_card' => $credit_card, 99 | 'expiry' => $custom_expiry 100 | ); 101 | 102 | try 103 | { 104 | $snap_token = $midtrans->getSnapToken($transaction_data); 105 | //return redirect($vtweb_url); 106 | echo $snap_token; 107 | } 108 | catch (Exception $e) 109 | { 110 | return $e->getMessage; 111 | } 112 | } 113 | 114 | public function finish(Request $request) 115 | { 116 | $result = $request->input('result_data'); 117 | $result = json_decode($result); 118 | echo $result->status_message . '
'; 120 | var_dump($result); 121 | echo '' ; 122 | } 123 | 124 | public function notification() 125 | { 126 | $midtrans = new Midtrans; 127 | echo 'test notification handler'; 128 | $json_result = file_get_contents('php://input'); 129 | $result = json_decode($json_result); 130 | 131 | if($result){ 132 | $notif = $midtrans->status($result->order_id); 133 | } 134 | 135 | error_log(print_r($result,TRUE)); 136 | 137 | /* 138 | $transaction = $notif->transaction_status; 139 | $type = $notif->payment_type; 140 | $order_id = $notif->order_id; 141 | $fraud = $notif->fraud_status; 142 | 143 | if ($transaction == 'capture') { 144 | // For credit card transaction, we need to check whether transaction is challenge by FDS or not 145 | if ($type == 'credit_card'){ 146 | if($fraud == 'challenge'){ 147 | // TODO set payment status in merchant's database to 'Challenge by FDS' 148 | // TODO merchant should decide whether this transaction is authorized or not in MAP 149 | echo "Transaction order_id: " . $order_id ." is challenged by FDS"; 150 | } 151 | else { 152 | // TODO set payment status in merchant's database to 'Success' 153 | echo "Transaction order_id: " . $order_id ." successfully captured using " . $type; 154 | } 155 | } 156 | } 157 | else if ($transaction == 'settlement'){ 158 | // TODO set payment status in merchant's database to 'Settlement' 159 | echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type; 160 | } 161 | else if($transaction == 'pending'){ 162 | // TODO set payment status in merchant's database to 'Pending' 163 | echo "Waiting customer to finish transaction order_id: " . $order_id . " using " . $type; 164 | } 165 | else if ($transaction == 'deny') { 166 | // TODO set payment status in merchant's database to 'Denied' 167 | echo "Payment using " . $type . " for transaction order_id: " . $order_id . " is denied."; 168 | }*/ 169 | 170 | } 171 | } -------------------------------------------------------------------------------- /app/Http/Controllers/TransactionController.php: -------------------------------------------------------------------------------- 1 | '; 15 | Veritrans::$isProduction = false; 16 | } 17 | 18 | public function transaction() 19 | { 20 | return view('transaction'); 21 | } 22 | 23 | public function transaction_process(Request $request) 24 | { 25 | $vt = new Veritrans; 26 | $order_id = $request->input('order_id'); 27 | $action = $request->input('action'); 28 | switch ($action) { 29 | case 'status': 30 | $this->status($order_id); 31 | break; 32 | case 'approve': 33 | $this->approve($order_id); 34 | break; 35 | case 'expire': 36 | $this->expire($order_id); 37 | break; 38 | case 'cancel': 39 | $this->cancel($order_id); 40 | break; 41 | } 42 | } 43 | 44 | public function status($order_id) 45 | { 46 | $vt = new Veritrans; 47 | echo 'test get status '; 48 | print_r ($vt->status($order_id) ); 49 | } 50 | 51 | public function cancel($order_id) 52 | { 53 | $vt = new Veritrans; 54 | echo 'test cancel trx '; 55 | echo $vt->cancel($order_id); 56 | } 57 | 58 | public function approve($order_id) 59 | { 60 | $vt = new Veritrans; 61 | echo 'test get approve '; 62 | print_r ($vt->approve($order_id) ); 63 | } 64 | 65 | public function expire($order_id) 66 | { 67 | $vt = new Veritrans; 68 | echo 'test get expire '; 69 | print_r ($vt->expire($order_id) ); 70 | } 71 | 72 | 73 | } -------------------------------------------------------------------------------- /app/Http/Controllers/VtdirectController.php: -------------------------------------------------------------------------------- 1 | '; 15 | Veritrans::$isProduction = false; 16 | } 17 | 18 | public function vtdirect() 19 | { 20 | return view('checkout'); 21 | } 22 | 23 | public function checkout_process(Request $request) 24 | { 25 | $token = $request->input('token_id'); 26 | $vt = new Veritrans; 27 | 28 | $transaction_details = array( 29 | 'order_id' => uniqid(), 30 | 'gross_amount' => 10000 31 | ); 32 | 33 | // Populate items 34 | $items = [ 35 | array( 36 | 'id' => 'item1', 37 | 'price' => 5000, 38 | 'quantity' => 1, 39 | 'name' => 'Adidas f50' 40 | ), 41 | array( 42 | 'id' => 'item2', 43 | 'price' => 2500, 44 | 'quantity' => 2, 45 | 'name' => 'Nike N90' 46 | ) 47 | ]; 48 | 49 | // Populate customer's billing address 50 | $billing_address = array( 51 | 'first_name' => "Andri", 52 | 'last_name' => "Setiawan", 53 | 'address' => "Karet Belakang 15A, Setiabudi.", 54 | 'city' => "Jakarta", 55 | 'postal_code' => "51161", 56 | 'phone' => "081322311801", 57 | 'country_code' => 'IDN' 58 | ); 59 | 60 | // Populate customer's shipping address 61 | $shipping_address = array( 62 | 'first_name' => "John", 63 | 'last_name' => "Watson", 64 | 'address' => "Bakerstreet 221B.", 65 | 'city' => "Jakarta", 66 | 'postal_code' => "51162", 67 | 'phone' => "081322311801", 68 | 'country_code'=> 'IDN' 69 | ); 70 | 71 | // Populate customer's Info 72 | $customer_details = array( 73 | 'first_name' => "Andri", 74 | 'last_name' => "Setiawan", 75 | 'email' => "andrisetiawan@me.com", 76 | 'phone' => "081322311801", 77 | 'billing_address' => $billing_address, 78 | 'shipping_address'=> $shipping_address 79 | ); 80 | 81 | $transaction_data = array( 82 | 'payment_type' => 'credit_card', 83 | 'credit_card' => array( 84 | 'token_id' => $token, 85 | 'bank' => 'bni' 86 | ), 87 | 'transaction_details' => $transaction_details, 88 | 'item_details' => $items 89 | ); 90 | 91 | 92 | $response = null; 93 | try 94 | { 95 | $response= $vt->vtdirect_charge($transaction_data); 96 | } 97 | catch (Exception $e) 98 | { 99 | return $e->getMessage; 100 | } 101 | 102 | //var_dump($response); 103 | if($response) 104 | { 105 | if($response->transaction_status == "capture") 106 | { 107 | //success 108 | echo "Transaksi berhasil.
The requested content cannot be loaded.
Please try again later.