| 1 |
<?php |
| 2 |
|
| 3 |
class Yatra_Payment |
| 4 |
{ |
| 5 |
public function payment_statuses() |
| 6 |
{ |
| 7 |
return array( |
| 8 |
'processing' => __('Pending', 'yatra'), |
| 9 |
'publish' => __('Completed', 'yatra'), |
| 10 |
'hold' => __('On Hold', 'yatra'), |
| 11 |
'refunded' => __('Refunded', 'yatra'), |
| 12 |
'failed' => __('Failed', 'yatra') |
| 13 |
); |
| 14 |
} |
| 15 |
|
| 16 |
public function create($booking_id, $payment_gateway) |
| 17 |
{ |
| 18 |
|
| 19 |
if (!$booking_id || $booking_id < 1) { |
| 20 |
return; |
| 21 |
} |
| 22 |
$booking = get_post($booking_id); |
| 23 |
|
| 24 |
$booking_status = $booking->post_status ?? ''; |
| 25 |
|
| 26 |
if (!in_array($booking_status, array('yatra-pending', 'yatra-processing'))) { |
| 27 |
|
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
$booking_details = new Yatra_Tour_Booking($booking_id); |
| 32 |
|
| 33 |
$all_booking_details = $booking_details->get_all_booking_details(); |
| 34 |
|
| 35 |
$title = 'Payment - #' . $booking_id; |
| 36 |
|
| 37 |
$paid_amount = $this->get_total_paid_amount($booking_id); |
| 38 |
|
| 39 |
$total_amount = $booking_details->get_total(true); |
| 40 |
|
| 41 |
$due_amount = ($total_amount - $paid_amount) > 0 ? ($total_amount - $paid_amount) : 0; |
| 42 |
|
| 43 |
$all_payment_info = $this->get_all_info($booking_id); |
| 44 |
|
| 45 |
$installment = is_array($all_payment_info) ? (count($all_payment_info) + 1) : 1; |
| 46 |
|
| 47 |
$payment_type = floatval($paid_amount) > 0 ? 'partial' : 'full'; |
| 48 |
|
| 49 |
$post_array = apply_filters('yatra_before_payment_created', array( |
| 50 |
'post_title' => $title, |
| 51 |
'post_content' => '', |
| 52 |
'post_status' => 'processing', |
| 53 |
'post_slug' => uniqid(), |
| 54 |
'post_type' => 'yatra-payment', |
| 55 |
'meta_input' => array( |
| 56 |
'booking_details' => $all_booking_details, |
| 57 |
'payment_gateway' => $payment_gateway, |
| 58 |
'total_amount' => $total_amount, |
| 59 |
'currency_code' => $booking_details->get_currency_code(), |
| 60 |
'paid_amount' => $paid_amount, |
| 61 |
'payable_amount' => $due_amount, |
| 62 |
'due_amount' => $due_amount, |
| 63 |
'payment_type' => $payment_type, |
| 64 |
'booking_id' => $booking_id, |
| 65 |
'installment' => $installment, |
| 66 |
'transaction_id' => '', |
| 67 |
) |
| 68 |
)); |
| 69 |
|
| 70 |
|
| 71 |
return wp_insert_post($post_array); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
public function get_all_info($booking_id, $payment_type = 'any', $include_booking_details = true) |
| 76 |
{ |
| 77 |
$posts = get_posts(array( |
| 78 |
'numberposts' => -1, |
| 79 |
'meta_key' => 'booking_id', |
| 80 |
'meta_value' => $booking_id, |
| 81 |
'post_type' => 'yatra-payment', |
| 82 |
'post_status' => sanitize_text_field($payment_type), |
| 83 |
'order' => 'ASC', |
| 84 |
|
| 85 |
)); |
| 86 |
$payment_info = array(); |
| 87 |
$all_status = $this->payment_statuses(); |
| 88 |
foreach ($posts as $post) { |
| 89 |
|
| 90 |
$payment_id = $post->ID; |
| 91 |
$status = $post->post_status; |
| 92 |
$status = $all_status[$status] ?? __('Pending', 'yatra'); |
| 93 |
$payment_info[$payment_id] = [ |
| 94 |
'title' => $post->post_title . '[' . $payment_id . ']', |
| 95 |
'payment_gateway' => get_post_meta($payment_id, 'payment_gateway', true), |
| 96 |
'total_amount' => get_post_meta($payment_id, 'total_amount', true), |
| 97 |
'currency_code' => get_post_meta($payment_id, 'currency_code', true), |
| 98 |
'paid_amount' => get_post_meta($payment_id, 'paid_amount', true), |
| 99 |
'payable_amount' => get_post_meta($payment_id, 'payable_amount', true), |
| 100 |
'due_amount' => get_post_meta($payment_id, 'due_amount', true), |
| 101 |
'payment_type' => get_post_meta($payment_id, 'payment_type', true), |
| 102 |
'booking_id' => get_post_meta($payment_id, 'booking_id', true), |
| 103 |
'installment' => get_post_meta($payment_id, 'installment', true), |
| 104 |
'transaction_id' => get_post_meta($payment_id, 'transaction_id', true), |
| 105 |
'status' => $status, |
| 106 |
'payment_date' => $post->post_date |
| 107 |
]; |
| 108 |
|
| 109 |
if ($include_booking_details) { |
| 110 |
$payment_info[$payment_id]['booking_details'] = get_post_meta($payment_id, 'booking_details', true); |
| 111 |
} |
| 112 |
} |
| 113 |
return $payment_info; |
| 114 |
} |
| 115 |
|
| 116 |
public function get_total_paid_amount($booking_id) |
| 117 |
{ |
| 118 |
$total_paid_amount = 0; |
| 119 |
|
| 120 |
$all_payments_by_booking_id = get_posts(array( |
| 121 |
'numberposts' => -1, |
| 122 |
'meta_key' => 'booking_id', |
| 123 |
'meta_value' => $booking_id, |
| 124 |
'post_type' => 'yatra-payment', |
| 125 |
'post_status' => 'publish' |
| 126 |
)); |
| 127 |
|
| 128 |
if (!is_wp_error($all_payments_by_booking_id)) { |
| 129 |
|
| 130 |
foreach ($all_payments_by_booking_id as $payment) { |
| 131 |
|
| 132 |
if ($payment->post_status === 'publish') { |
| 133 |
|
| 134 |
$id = $payment->ID; |
| 135 |
|
| 136 |
$total_paid = floatval(get_post_meta($id, 'paid_amount', true)); |
| 137 |
|
| 138 |
$total_paid_amount += $total_paid; |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
} |
| 143 |
return $total_paid_amount; |
| 144 |
} |
| 145 |
|
| 146 |
public function get_net_due_amount($payment_id) |
| 147 |
{ |
| 148 |
$booking_id = $this->get_booking_id($payment_id); |
| 149 |
|
| 150 |
$booking_details = new Yatra_Tour_Booking($booking_id); |
| 151 |
|
| 152 |
$paid_amount = $this->get_total_paid_amount($booking_id); |
| 153 |
|
| 154 |
$total_amount = $booking_details->get_total(true); |
| 155 |
|
| 156 |
return ($total_amount - $paid_amount) > 0 ? ($total_amount - $paid_amount) : 0; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
public function get_gateway($payment_id) |
| 161 |
{ |
| 162 |
return get_post_meta($payment_id, 'payment_gateway', true); |
| 163 |
} |
| 164 |
|
| 165 |
public function get_total_amount($payment_id) |
| 166 |
{ |
| 167 |
return get_post_meta($payment_id, 'total_amount', true); |
| 168 |
} |
| 169 |
|
| 170 |
public function get_currency_code($payment_id) |
| 171 |
{ |
| 172 |
return get_post_meta($payment_id, 'currency_code', true); |
| 173 |
} |
| 174 |
|
| 175 |
public function get_paid_amount($payment_id) |
| 176 |
{ |
| 177 |
return get_post_meta($payment_id, 'paid_amount', true); |
| 178 |
} |
| 179 |
|
| 180 |
public function get_payable_amount($payment_id) |
| 181 |
{ |
| 182 |
return floatval(get_post_meta($payment_id, 'payable_amount', true)); |
| 183 |
} |
| 184 |
|
| 185 |
public function get_due_amount($payment_id) |
| 186 |
{ |
| 187 |
return get_post_meta($payment_id, 'due_amount', true); |
| 188 |
} |
| 189 |
|
| 190 |
public function get_payment_type($payment_id) |
| 191 |
{ |
| 192 |
return get_post_meta($payment_id, 'payment_type', true); |
| 193 |
} |
| 194 |
|
| 195 |
public function get_booking_id($payment_id) |
| 196 |
{ |
| 197 |
return get_post_meta($payment_id, 'booking_id', true); |
| 198 |
} |
| 199 |
|
| 200 |
public function get_installment_id($payment_id) |
| 201 |
{ |
| 202 |
return get_post_meta($payment_id, 'installment', true); |
| 203 |
} |
| 204 |
|
| 205 |
public function get_status($payment_id) |
| 206 |
{ |
| 207 |
return get_post_status($payment_id); |
| 208 |
} |
| 209 |
|
| 210 |
public function transaction_id($payment_id) |
| 211 |
{ |
| 212 |
return get_post_meta($payment_id, 'transaction_id', true); |
| 213 |
} |
| 214 |
|
| 215 |
public function update_status($payment_id, $status) |
| 216 |
{ |
| 217 |
$all_status = $this->payment_statuses(); |
| 218 |
|
| 219 |
if (isset($all_status[$status])) { |
| 220 |
|
| 221 |
$arg['ID'] = $payment_id; |
| 222 |
|
| 223 |
$arg['post_status'] = $status; |
| 224 |
|
| 225 |
wp_update_post($arg); |
| 226 |
} |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
public function update_transaction_id($payment_id, $transaction_id = '') |
| 231 |
{ |
| 232 |
if ($transaction_id != '') { |
| 233 |
|
| 234 |
update_post_meta($payment_id, 'transaction_id', sanitize_text_field($transaction_id)); |
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
public function update_paid_amount($payment_id, $paid_amount) |
| 240 |
{ |
| 241 |
return update_post_meta($payment_id, 'paid_amount', floatval($paid_amount)); |
| 242 |
} |
| 243 |
|
| 244 |
public function update_due_amount($payment_id, $due_amount) |
| 245 |
{ |
| 246 |
return update_post_meta($payment_id, 'due_amount', floatval($due_amount)); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
} |