| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmTransLitePaymentsController extends FrmTransLiteCRUDController { |
| 7 |
|
| 8 |
/** |
| 9 |
* @return void |
| 10 |
*/ |
| 11 |
public static function menu() { |
| 12 |
if ( FrmTransLiteAppHelper::should_fallback_to_paypal() ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
$frm_settings = FrmAppHelper::get_settings(); |
| 17 |
|
| 18 |
// Remove the PayPal submenu (PayPal payments will just appear in the regular Payments page). |
| 19 |
remove_action( 'admin_menu', 'FrmPaymentsController::menu', 26 ); |
| 20 |
|
| 21 |
if ( in_array( FrmAppHelper::simple_get( 'action' ), array( 'edit', 'new' ), true ) && is_callable( 'FrmPaymentsController::route' ) ) { |
| 22 |
// Use the PayPal addon for add new and edit routing if it is active. |
| 23 |
// This is required to support the "edit" link when using the Stripe Lite table view. |
| 24 |
// It is also required for the "Add New" button to work on the payments table page. |
| 25 |
$menu_route = 'FrmPaymentsController::route'; |
| 26 |
} else { |
| 27 |
$menu_route = 'FrmTransLitePaymentsController::route'; |
| 28 |
} |
| 29 |
|
| 30 |
add_submenu_page( 'formidable', $frm_settings->menu . ' | Payments', 'Payments', 'frm_view_entries', 'formidable-payments', $menu_route ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public static function route() { |
| 37 |
$action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; |
| 38 |
$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); |
| 39 |
$type = FrmAppHelper::get_param( 'type', '', 'get', 'sanitize_title' ); |
| 40 |
|
| 41 |
$class_name = $type === 'subscriptions' ? 'FrmTransLiteSubscriptionsController' : 'FrmTransLitePaymentsController'; |
| 42 |
if ( method_exists( $class_name, $action ) ) { |
| 43 |
$class_name::$action(); |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
FrmTransLiteListsController::route( $action ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @param object $payment |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public static function load_sidebar_actions( $payment ) { |
| 55 |
FrmTransLiteActionsController::actions_js(); |
| 56 |
|
| 57 |
$date_format = __( 'M j, Y @ G:i', 'formidable' ); |
| 58 |
$created_at = FrmAppHelper::get_localized_date( $date_format, $payment->created_at ); |
| 59 |
include FrmTransLiteAppHelper::plugin_path() . '/views/payments/sidebar_actions.php'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Echo a receipt link. |
| 64 |
* |
| 65 |
* @param object $payment |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public static function show_receipt_link( $payment ) { |
| 69 |
$link = esc_html( $payment->receipt_id ); |
| 70 |
$paysys = $payment->paysys; |
| 71 |
|
| 72 |
if ( $payment->receipt_id !== 'None' && self::should_filter_receipt_link( $paysys ) ) { |
| 73 |
/** |
| 74 |
* Filter a receipt link for a specific gateway. |
| 75 |
* For example, Stripe uses frm_pay_stripe_receipt. |
| 76 |
* |
| 77 |
* @param string $link |
| 78 |
*/ |
| 79 |
$link = apply_filters( 'frm_pay_' . $paysys . '_receipt', $link ); |
| 80 |
} |
| 81 |
|
| 82 |
echo FrmAppHelper::kses( $link, array( 'a' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @param string $paysys |
| 87 |
* |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
private static function should_filter_receipt_link( $paysys ) { |
| 91 |
$allowed_types = array( 'stripe', 'authnet_aim' ); |
| 92 |
return in_array( $paysys, $allowed_types, true ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Echo a refund link. |
| 97 |
* |
| 98 |
* @param object $payment |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public static function show_refund_link( $payment ) { |
| 102 |
$link = self::refund_link( $payment ); |
| 103 |
FrmTransLiteAppHelper::echo_confirmation_link( $link ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Show a link to a payment entry (unless it is deleted). |
| 108 |
* |
| 109 |
* @param object $payment |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
public static function show_entry_link( $payment ) { |
| 113 |
$entry = FrmDb::get_col( 'frm_items', array( 'id' => $payment->item_id ) ); |
| 114 |
|
| 115 |
if ( ! $entry ) { |
| 116 |
// translators: %d: Entry ID. |
| 117 |
echo esc_html( sprintf( __( '%d (Deleted)', 'formidable' ), $payment->item_id ) ); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
?> |
| 122 |
<a href="?page=formidable-entries&action=show&frm_action=show&id=<?php echo absint( $payment->item_id ); ?>"> |
| 123 |
<?php echo absint( $payment->item_id ); ?> |
| 124 |
</a> |
| 125 |
<?php |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get a refund link. |
| 130 |
* |
| 131 |
* @param object $payment |
| 132 |
* @return string |
| 133 |
*/ |
| 134 |
public static function refund_link( $payment ) { |
| 135 |
if ( $payment->status === 'refunded' ) { |
| 136 |
$link = esc_html__( 'Refunded', 'formidable' ); |
| 137 |
} else { |
| 138 |
$confirm = __( 'Are you sure you want to refund that payment?', 'formidable' ); |
| 139 |
|
| 140 |
$link = admin_url( 'admin-ajax.php?action=frm_trans_refund&payment_id=' . $payment->id . '&nonce=' . wp_create_nonce( 'frm_trans_ajax' ) ); |
| 141 |
$link = '<a href="' . esc_url( $link ) . '" class="frm_trans_ajax_link" data-frmverify="' . esc_attr( $confirm ) . '">'; |
| 142 |
$link .= esc_html__( 'Refund', 'formidable' ); |
| 143 |
$link .= '</a>'; |
| 144 |
} |
| 145 |
|
| 146 |
$paysys = $payment->paysys; |
| 147 |
if ( self::should_filter_refund_link( $paysys ) ) { |
| 148 |
/** |
| 149 |
* Filter the refund link for a specific gateway. |
| 150 |
* For example, Stripe uses frm_pay_stripe_refund_link. |
| 151 |
* |
| 152 |
* @param string $link |
| 153 |
* @param object $payment |
| 154 |
*/ |
| 155 |
$link = apply_filters( 'frm_pay_' . $paysys . '_refund_link', $link, $payment ); |
| 156 |
} |
| 157 |
|
| 158 |
return $link; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @param string $paysys |
| 163 |
* |
| 164 |
* @return bool |
| 165 |
*/ |
| 166 |
private static function should_filter_refund_link( $paysys ) { |
| 167 |
$allowed_types = array( 'stripe', 'authnet_aim' ); |
| 168 |
return in_array( $paysys, $allowed_types, true ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Process the ajax request to refund a payment. |
| 173 |
* |
| 174 |
* @return void |
| 175 |
*/ |
| 176 |
public static function refund_payment() { |
| 177 |
FrmAppHelper::permission_check( 'frm_edit_entries' ); |
| 178 |
check_ajax_referer( 'frm_trans_ajax', 'nonce' ); |
| 179 |
|
| 180 |
$payment_id = FrmAppHelper::get_param( 'payment_id', '', 'get', 'absint' ); |
| 181 |
if ( ! $payment_id ) { |
| 182 |
wp_die( esc_html__( 'Oops! No payment was selected for refund.', 'formidable' ) ); |
| 183 |
} |
| 184 |
|
| 185 |
$frm_payment = new FrmTransLitePayment(); |
| 186 |
$payment = $frm_payment->get_one( $payment_id ); |
| 187 |
$refunded = FrmStrpLiteAppHelper::call_stripe_helper_class( 'refund_payment', $payment->receipt_id ); |
| 188 |
|
| 189 |
if ( $refunded ) { |
| 190 |
self::change_payment_status( $payment, 'refunded' ); |
| 191 |
$message = __( 'Refunded', 'formidable' ); |
| 192 |
} else { |
| 193 |
$message = __( 'Failed', 'formidable' ); |
| 194 |
} |
| 195 |
|
| 196 |
wp_die( esc_html( $message ) ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Update the status of a payment. |
| 201 |
* |
| 202 |
* @param object $payment |
| 203 |
* @param string $status |
| 204 |
* @return void |
| 205 |
*/ |
| 206 |
public static function change_payment_status( $payment, $status ) { |
| 207 |
if ( $status === $payment->status ) { |
| 208 |
// The payment status has not actually changed. |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
$frm_payment = new FrmTransLitePayment(); |
| 213 |
$frm_payment->update( $payment->id, array( 'status' => $status ) ); |
| 214 |
FrmTransLiteActionsController::trigger_payment_status_change( compact( 'status', 'payment' ) ); |
| 215 |
} |
| 216 |
} |
| 217 |
|