-
Notifications
You must be signed in to change notification settings - Fork 2
/
midtrans-standard.php
261 lines (238 loc) · 8.96 KB
/
midtrans-standard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/*
Plugin Name: Midtrans Payment Gateway for Easy Digital Downloads
Plugin URI: https://github.com/Midtrans/midtrans-edd
Description: Accept various payment methods directly on your Easy Digital Downloads site in a seamless and secure checkout environment with <a href="https://midtrans.com" target="_blank">Midtrans.com</a>
Version: 2.5.0
Author: Midtrans
Author URI: https://midtrans.com
*/
//exit if opened directly
if ( ! defined( 'ABSPATH' ) ) exit;
DEFINE ('EDD_MIDTRANS_PLUGIN_VERSION', get_file_data(__FILE__, array('Version' => 'Version'), false)['Version'] );
/*
|--------------------------------------------------------------------------
| CONSTANTS
|--------------------------------------------------------------------------
*/
define( 'EDDMIDTRANS_DIR', plugin_dir_path( __FILE__ ) );
/*
|--------------------------------------------------------------------------
| INCLUDES
|--------------------------------------------------------------------------
*/
include_once( EDDMIDTRANS_DIR . 'includes/edd-midtrans.php' );
include_once( EDDMIDTRANS_DIR . 'includes/edd-midtrans-installment.php' );
include_once( EDDMIDTRANS_DIR . 'includes/edd-midtrans-installmentoff.php' );
// include_once( EDDMIDTRANS_DIR . 'includes/edd-midtrans-promo.php' );
if(!class_exists("Midtrans\Config")){
require_once plugin_dir_path( __FILE__ ) . '/lib/Midtrans.php';
}
#To add currency Rp and IDR
#
function midtrans_gateway_rupiah_currencies( $currencies ) {
if(!array_key_exists('Rp', $currencies)){
$currencies['Rp'] = __('Indonesian Rupiah ( Rp )', 'edd-midtrans');
}
return $currencies;
}
add_filter( 'edd_currencies', 'midtrans_gateway_rupiah_currencies');
// to get notification from veritrans
function edd_midtrans_notification(){
global $edd_options;
if(!class_exists("Midtrans\Config")){
require_once plugin_dir_path( __FILE__ ) . '/lib/Midtrans.php';
}
if(edd_is_test_mode()){
// set Sandbox credentials here
\Midtrans\Config::$serverKey = $edd_options['mt_sandbox_server_key'];
\Midtrans\Config::$isProduction = false;
}else {
// set Production credentials here
\Midtrans\Config::$serverKey = $edd_options['mt_production_server_key'];
\Midtrans\Config::$isProduction = true;
}
$notif = new \Midtrans\Notification();
$transaction = $notif->transaction_status;
$fraud = $notif->fraud_status;
$order_id = $notif->order_id;
if ($transaction == 'capture') {
if ($fraud == 'challenge') {
edd_insert_payment_note( $order_id, __( 'Midtrans Challenged Payment', 'edd-midtrans' ) );
edd_update_payment_status($order_id, 'pending');
}
else if ($fraud == 'accept') {
edd_insert_payment_note( $order_id, __( 'Midtrans Payment Completed', 'edd-midtrans' ) );
edd_update_payment_status($order_id, 'complete');
}
}
else if ($notif->transaction_status != 'credit_card' && $transaction == 'settlement') {
edd_insert_payment_note( $order_id, __( 'Midtrans Payment Completed', 'edd-midtrans' ) );
edd_update_payment_status($order_id, 'complete');
}
else if ($transaction == 'pending') {
edd_insert_payment_note( $order_id, __( 'Midtrans Awaiting Payment', 'edd-midtrans' ) );
edd_update_payment_status($order_id, 'pending');
}
else if ($transaction == 'cancel') {
edd_insert_payment_note( $order_id, __( 'Midtrans Cancelled Payment', 'edd-midtrans' ) );
edd_update_payment_status($order_id, 'failed');
}
else if ($transaction == 'expire') {
edd_insert_payment_note( $order_id, __( 'Midtrans Expired Payment', 'edd-midtrans' ) );
edd_update_payment_status($order_id, 'failed');
}
else if ($transaction == 'deny') {
edd_insert_payment_note( $order_id, __( 'Midtrans Expired Payment', 'edd-midtrans' ) );
edd_update_payment_status($order_id, 'failed');
}
};
add_action( 'edd_midtrans_notification', 'edd_midtrans_notification' );
function edd_listen_for_midtrans_notification() {
global $edd_options;
// check if payment url http://site.com/?edd-listener=midtrans
if ( isset( $_GET['edd-listener'] ) && sanitize_text_field($_GET['edd-listener']) == 'midtrans' ) {
do_action( 'edd_midtrans_notification' );
}
if ( isset( $_GET['confirmation_page'] ) && sanitize_text_field($_GET['confirmation_page']) == 'midtrans' && wp_verify_nonce(sanitize_text_field($_GET['nonce']), 'edd_midtrans_gateway' . sanitize_text_field($_REQUEST['order_id']) )) {
$order = sanitize_text_field($_REQUEST['order_id']);
$status = sanitize_text_field($_REQUEST['transaction_status']);
if (isset( $_GET['edd-listener'])){
edd_send_to_success_page();
}
else{
if ($status == 'capture'){
$_SESSION['pdf'] = "";
edd_send_to_success_page();
}
else if ($status == 'pending'){
if (isset($_REQUEST['pdf'])){
$_SESSION['pdf'] = sanitize_text_field($_REQUEST['pdf']);
}
else{
$_SESSION['pdf'] = "";
}
edd_send_to_success_page();
}
}
}
else if ( isset( $_GET['confirmation_page'] ) && sanitize_text_field($_GET['confirmation_page']) == 'midtrans') {
$order = sanitize_text_field($_REQUEST['order_id']);
$status = sanitize_text_field($_REQUEST['transaction_status']);
if (isset( $_GET['edd-listener'])){
edd_send_to_success_page();
}
else{
if ($status == 'capture' || $status == 'pending'){
edd_send_to_success_page();
}
}
}
}
add_action( 'init', 'edd_listen_for_midtrans_notification' );
function mid_edd_display_checkout_fields() {
?>
<p id="edd-phone-wrap">
<label class="edd-label" for="edd-phone">Phone Number</label>
<span class="edd-description">
Please input your phone number.
</span>
<input class="edd-input" type="text" name="edd_phone" id="edd-phone" placeholder="Phone Number" />
</p>
<?php
}
add_action( 'edd_purchase_form_user_info_fields', 'mid_edd_display_checkout_fields' );
/**
* Make phone number required
* Add more required fields here if you need to
*/
function mid_edd_required_checkout_fields( $required_fields ) {
$required_fields['edd_phone'] = array(
'error_id' => 'invalid_phone_number',
'error_message' => 'Please enter a valid Phone number'
);
return $required_fields;
}
add_filter( 'edd_purchase_form_required_fields', 'mid_edd_required_checkout_fields' );
/**
* Set error if phone number field is empty
* You can do additional error checking here if required
*/
function mid_edd_validate_checkout_fields( $valid_data, $data ) {
if ( empty( $data['edd_phone'] ) ) {
edd_set_error( 'invalid_phone', 'Please enter your phone number.' );
}
}
add_action( 'edd_checkout_error_checks', 'mid_edd_validate_checkout_fields', 10, 2 );
/**
* Store the custom field data into EDD's payment meta
*/
function mid_edd_store_custom_fields( $payment_meta ) {
if( did_action( 'edd_purchase' ) ) {
$payment_meta['phone'] = isset( $_POST['edd_phone'] ) ? sanitize_text_field( $_POST['edd_phone'] ) : '';
}
return $payment_meta;
}
add_filter( 'edd_payment_meta', 'mid_edd_store_custom_fields');
/**
* Add the phone number to the "View Order Details" page
*/
function mid_edd_view_order_details( $payment_meta, $user_info ) {
$phone = isset( $payment_meta['phone'] ) ? $payment_meta['phone'] : 'none';
?>
<div class="column-container">
<div class="column">
<strong>Phone: </strong>
<?php echo $phone; ?>
</div>
</div>
<?php
}
add_action( 'edd_payment_personal_details_list', 'mid_edd_view_order_details', 10, 2 );
/**
* Add a {phone} tag for use in either the purchase receipt email or admin notification emails
*/
function mid_edd_add_email_tag() {
edd_add_email_tag( 'phone', 'Customer\'s phone number', 'mid_edd_email_tag_phone' );
}
add_action( 'edd_add_email_tags', 'mid_edd_add_email_tag' );
/**
* The {phone} email tag
*/
function mid_edd_email_tag_phone( $payment_id ) {
$payment_data = edd_get_payment_meta( $payment_id );
return $payment_data['phone'];
}
// remove_action( 'edd_checkout_form_top', 'edd_discount_field', -1 );
/**
* Applies filters to the success page content.
*
* @param string $content Content before filters
* @return string $content Filtered content
*
* TODO : handle status changed from pending to settlemen
*/
function edd_midtrans_page_content( $content ) {
// Check if we're on the success page
if (edd_is_success_page()) {
if ($_SESSION['pdf']){
$sanitized = [];
$sanitized['payment-confirmation'] = sanitize_text_field($_GET['payment-confirmation']);
$sanitized['pdf'] = sanitize_text_field($_SESSION['pdf']);
$message = '<div class="edd-midtrans">';
$message .= '<h3>Payment Instruction</h3>';
$message .= '<p><a href="' . $sanitized['pdf'] . '" target="_blank">' . $sanitized['pdf'] . '</a></p>' ;
$message .= '</div>';
if (has_filter('edd_payment_confirm_' . $sanitized['payment-confirmation'])) {
$content = apply_filters('edd_payment_confirm_' . $sanitized['payment-confirmation'], $content);
}
$_SESSION['pdf'] = "";
return $content . $message;
}
else return $content;
}
// Fallback to returning the default content
else
return $content;
}
add_filter( 'the_content', 'edd_midtrans_page_content' );