· 6 years ago · Mar 30, 2020, 03:32 PM
1<?php
2
3/*
4 Plugin Name: iPaymu Payment Gateway
5 Plugin URI: http://ipaymu.com
6 Description: iPaymu Payment Gateway
7 Version: 1.1
8 Author: iPaymu Development Team
9 Author URI: http://ipaymu.com
10 */
11
12if ( ! defined( 'ABSPATH' ) ) exit;
13
14add_action('plugins_loaded', 'woocommerce_ipaymu_init', 0);
15
16function woocommerce_ipaymu_init() {
17
18 if (!class_exists('WC_Payment_Gateway'))
19 return;
20
21 class WC_Gateway_iPaymu extends WC_Payment_Gateway {
22
23 public function __construct() {
24
25 //plugin id
26 $this->id = 'ipaymu';
27 //Payment Gateway title
28 $this->method_title = 'iPaymu Payment Gateway';
29 //true only in case of direct payment method, false in our case
30 $this->has_fields = false;
31 //payment gateway logo
32 $this->icon = plugins_url('/ipaymu_badge.png', __FILE__);
33
34 //redirect URL
35 $this->redirect_url = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'WC_Gateway_iPaymu', home_url( '/' ) ) );
36
37 //Load settings
38 $this->init_form_fields();
39 $this->init_settings();
40
41 // Define user set variables
42 $this->enabled = $this->settings['enabled'];
43 $this->title = "Ipaymu Payment";
44 $this->description = $this->settings['description'];
45 $this->apikey = $this->settings['apikey'];
46 $this->password = $this->settings['password'];
47 $this->processor_id = $this->settings['processor_id'];
48 $this->salemethod = $this->settings['salemethod'];
49 $this->gatewayurl = $this->settings['gatewayurl'];
50 $this->order_prefix = $this->settings['order_prefix'];
51 $this->debugon = $this->settings['debugon'];
52 $this->debugrecip = $this->settings['debugrecip'];
53 $this->cvv = $this->settings['cvv'];
54
55 // Actions
56 add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options'));
57 add_action('woocommerce_receipt_ipaymu', array(&$this, 'receipt_page'));
58
59 // Payment listener/API hook
60 add_action( 'woocommerce_api_wc_gateway_ipaymu', array( $this, 'check_ipaymu_response' ) );
61 }
62
63 function init_form_fields() {
64
65 $this->form_fields = array(
66 'enabled' => array(
67 'title' => __( 'Enable/Disable', 'woothemes' ),
68 'label' => __( 'Enable iPaymu', 'woothemes' ),
69 'type' => 'checkbox',
70 'description' => '',
71 'default' => 'no'
72 ),
73 'title' => array(
74 'title' => __( 'Title', 'woothemes' ),
75 'type' => 'text',
76 'description' => __( '', 'woothemes' ),
77 'default' => __( 'Pembayaran iPaymu', 'woothemes' )
78 ),
79 'description' => array(
80 'title' => __( 'Description', 'woothemes' ),
81 'type' => 'textarea',
82 'description' => __( '', 'woothemes' ),
83 'default' => 'Sistem pembayaran menggunakan iPaymu.'
84 ),
85 'apikey' => array(
86 'title' => __( 'API Key', 'woothemes' ),
87 'type' => 'text',
88 'description' => __( ' Dapatkan API Key <a href=https://ipaymu.com/login/members/profile.htm target=_blank>di sini</a></small>.', 'woothemes' ),
89 'default' => ''
90 ),
91 /*'debugrecip' => array(
92 'title' => __( 'Debugging Email', 'woothemes' ),
93 'type' => 'text',
94 'description' => __( 'Who should receive the debugging emails.', 'woothemes' ),
95 'default' => get_option('admin_email')
96 ),*/
97 );
98 }
99
100 public function admin_options() {
101 echo '<table class="form-table">';
102 $this->generate_settings_html();
103 echo '</table>';
104 }
105
106 function payment_fields() {
107 if ($this->description)
108 echo wpautop(wptexturize($this->description));
109 }
110
111
112 function receipt_page($order) {
113 echo $this->generate_ipaymu_form($order);
114 }
115
116
117 public function generate_ipaymu_form($order_id) {
118
119 global $woocommerce;
120
121 $order = new WC_Order($order_id);
122
123 // URL Payment IPAYMU
124 $url = 'https://my.ipaymu.com/payment.htm';
125
126 // Prepare Parameters
127 $params = array(
128 'key' => $this->apikey, // API Key Merchant / Penjual
129 'action' => 'payment',
130 'product' => 'Order : #'.$order_id,
131 'price' => $order->order_total, // Total Harga
132 'quantity' => 1,
133 'comments' => '', // Optional
134 'ureturn' => $this->redirect_url.'&id_order='.$order_id,
135 'unotify' => $this->redirect_url.'&id_order='.$order_id.'¶m=notify',
136 'ucancel' => $this->redirect_url.'&id_order='.$order_id.'¶m=cancel',
137 'format' => 'json' // Format: xml / json. Default: xml
138 );
139
140 $params_string = http_build_query($params);
141
142 //open connection
143 $ch = curl_init();
144
145 curl_setopt($ch, CURLOPT_URL, $url);
146 curl_setopt($ch, CURLOPT_POST, count($params));
147 curl_setopt($ch, CURLOPT_POSTFIELDS, $params_string);
148 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
149 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
150
151 //execute post
152 $request = curl_exec($ch);
153
154 if ( $request === false ) {
155 echo 'Curl Error: ' . curl_error($ch);
156 } else {
157
158 $result = json_decode($request, true);
159
160 if( isset($result['url']) )
161 header('location: '. $result['url']);
162 else {
163 echo "Request Error ". $result['Status'] .": ". $result['Keterangan'];
164 }
165 }
166
167 //close connection
168 curl_close($ch);
169 }
170
171
172 function process_payment($order_id) {
173 global $woocommerce;
174 $order = new WC_Order($order_id);
175
176 $order->reduce_order_stock();
177
178 WC()->cart->empty_cart();
179
180 return array(
181 'result' => 'success',
182 'redirect' => $order->get_checkout_payment_url( true ));
183 }
184
185
186 function check_ipaymu_response() {
187
188 global $woocommerce;
189 $order = new WC_Order($_REQUEST['id_order']);
190
191 if($_REQUEST['status'] == 'berhasil') {
192 $order->add_order_note( __( 'Pembayaran telah dilakukan melalui ipaymu dengan id transaksi '.$_REQUEST['trx_id'], 'woocommerce' ) );
193 $order->payment_complete();
194 } else {
195 $order->add_order_note( __( 'Menunggu pembayaran melalui non-member ipaymu dengan id transaksi '.$_REQUEST['trx_id'], 'woocommerce' ) );
196 }
197
198 $redirect = add_query_arg('key', $order->order_key, add_query_arg('order', $_REQUEST['id_order'], get_permalink(woocommerce_get_page_id('thanks'))));
199 wp_redirect($redirect);
200 exit;
201
202 }
203
204 }
205
206 function add_ipaymu_gateway($methods) {
207 $methods[] = 'WC_Gateway_iPaymu';
208 return $methods;
209 }
210
211 add_filter('woocommerce_payment_gateways', 'add_ipaymu_gateway');
212}