Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow organisers to refund tickets more freely #991

Open
wants to merge 4 commits into
base: production
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 67 additions & 24 deletions public_html/wp-content/plugins/camptix/camptix.php
Original file line number Diff line number Diff line change
Expand Up @@ -3982,6 +3982,19 @@ function metabox_attendee_submitdiv() {
<span><a href="<?php echo esc_url( $edit_link ); ?>"><?php _e( 'Edit Attendee Info', 'wordcamporg' ); ?></a></span>
</div>

<div class="misc-pub-section">
<?php
$access_token = get_post_meta( $post->ID, 'tix_access_token', true );
$refund_link = $this->get_refund_tickets_link( $access_token );

$refunds_available_text = __( 'only for organizers', 'wordcamporg' );
if ( $this->options['refunds_enabled'] ) {
$refunds_available_text = __( 'for attendee', 'wordcamporg' );
}
?>
<span><a href="<?php echo esc_url( $refund_link ); ?>"><?php _e( 'Refund Request', 'wordcamporg' ); ?></a> (<?php echo $refunds_available_text ?>)</span>
</div>

<div class="misc-pub-section">
<div class="tix-pub-section-item">
<input id="tix_privacy_<?php esc_attr( $post->ID ); ?>" name="tix_privacy" type="checkbox" <?php checked( get_post_meta( $post->ID, 'tix_privacy', true ), 'private' ); ?> />
Expand Down Expand Up @@ -4662,6 +4675,12 @@ function metabox_attendee_info() {
$rows[] = array( __( 'Edit Token', 'wordcamporg' ), sprintf( '<a href="%s">%s</a>', $this->get_edit_attendee_link( $post->ID, $edit_token ), $edit_token ) );
$rows[] = array( __( 'Access Token', 'wordcamporg' ), sprintf( '<a href="%s">%s</a>', $this->get_access_tickets_link( $access_token ), $access_token ) );

$refunds_available_text = __( 'only for organizers', 'wordcamporg' );
if ( $this->options['refunds_enabled'] ) {
$refunds_available_text = __( 'for attendee', 'wordcamporg' );
}
$rows[] = array( __( 'Refund Request', 'wordcamporg' ), sprintf( '<a href="%s">Link</a> %s', $this->get_refund_tickets_link( $access_token ), $refunds_available_text ) );

// Transaction
$rows[] = array( __( 'Transaction', 'wordcamporg' ), '' );
$rows[] = array( __( 'Payment Method', 'wordcamporg' ), $payment_method );
Expand Down Expand Up @@ -5346,7 +5365,7 @@ function template_redirect() {
$this->shortcode_contents = $this->form_access_tickets();
} elseif ( 'edit_attendee' == $tix_action ) {
$this->shortcode_contents = $this->form_edit_attendee();
} elseif ( 'refund_request' == $tix_action && $this->options['refunds_enabled'] ) {
} elseif ( 'refund_request' == $tix_action && ( $this->options['refunds_enabled'] || current_user_can( $this->caps['manage_attendees'] ) ) ) {
$this->shortcode_contents = $this->form_refund_request();
} else {
// If we end up here, start over.
Expand Down Expand Up @@ -6348,7 +6367,13 @@ function form_refund_request() {
// Clean things up before and after the shortcode.
$post->post_content = apply_filters( 'camptix_post_content_override', $this->shortcode_str, $post->post_content, $_GET['tix_action'] );

if ( ! $this->options['refunds_enabled'] || ! isset( $_REQUEST['tix_access_token'] ) || ! ctype_alnum( $_REQUEST['tix_access_token'] ) ) {
if ( ! isset( $_REQUEST['tix_access_token'] ) || ! ctype_alnum( $_REQUEST['tix_access_token'] ) ) {
$this->error_flags['invalid_access_token'] = true;
$this->redirect_with_error_flags();
die();
}

if ( ! $this->options['refunds_enabled'] && ! current_user_can( $this->caps['manage_attendees'] ) ) {
$this->error_flags['invalid_access_token'] = true;
$this->redirect_with_error_flags();
die();
Expand Down Expand Up @@ -6408,44 +6433,62 @@ function form_refund_request() {
$tickets[$ticket_id] = 1;
}

if ( count( $transactions ) != 1 || $transactions[ $txn_id ]['payment_amount'] <= 0 ) {
$this->error_flags['cannot_refund'] = true;
$this->redirect_with_error_flags();
die();
}
if ( ! current_user_can( $this->caps['manage_attendees'] ) ) {
if ( count( $transactions ) != 1 || $transactions[ $txn_id ]['payment_amount'] <= 0 ) {
$this->error_flags['cannot_refund'] = true;
$this->redirect_with_error_flags();
die();
}

$transaction = array_shift( $transactions );
if ( ! $transaction['receipt_email'] || ! $transaction['transaction_id'] || ! $transaction['payment_amount'] ) {
$this->error_flags['cannot_refund'] = true;
$this->redirect_with_error_flags();
die();
$transaction = array_shift( $transactions );
if ( ! $transaction['receipt_email'] || ! $transaction['transaction_id'] || ! $transaction['payment_amount'] ) {
$this->error_flags['cannot_refund'] = true;
$this->redirect_with_error_flags();
die();
}
}

// Has a refund request been submitted?
$reason = '';
if ( current_user_can( $this->caps['manage_attendees'] ) ) {
$reason = wp_sprintf( __( 'In behalf of attendee by %s (%s)', 'wordcamporg' ), wp_get_current_user()->display_name, wp_get_current_user()->user_login );
}

if ( isset( $_POST['tix_refund_request_submit'] ) ) {
$reason = esc_html( $_POST['tix_refund_request_reason'] );
$check = isset( $_POST['tix_refund_request_confirmed'] ) ? $_POST['tix_refund_request_confirmed'] : false;

if ( ! $check ) {
$this->error( __( 'You have to agree to the terms to request a refund.', 'wordcamporg' ) );
} else {
// Allow organisers to refund tickets without transactions (i.e. free tickets)
if ( current_user_can( $this->caps['manage_attendees'] ) && empty( $transactions ) ) {
// Change status for all attendees within the same purchase.
foreach ( $attendees as $attendee ) {
$attendee->post_status = 'refund';
wp_update_post( $attendee );
}

// Dumb result in order for checks below to pass, with this we avoid adding new check.
$result = CampTix_Plugin::PAYMENT_STATUS_REFUNDED;
} else {
$payment_method_obj = $this->get_payment_method_by_id( $transaction['payment_method'] );

$payment_method_obj = $this->get_payment_method_by_id( $transaction['payment_method'] );
// Bail if a payment method does not exist.
if ( ! $payment_method_obj ) {
$this->error_flags['cannot_refund'] = true;
$this->redirect_with_error_flags();
die();
}

// Bail if a payment method does not exist.
if ( ! $payment_method_obj ) {
$this->error_flags['cannot_refund'] = true;
$this->redirect_with_error_flags();
die();
}
/**
* @todo: Better error messaging for misconfigured payment methods
*/

/**
* @todo: Better error messaging for misconfigured payment methods
*/
// Attempt to process the refund transaction
$result = $payment_method_obj->payment_refund( $transaction['payment_token'] );
}

// Attempt to process the refund transaction
$result = $payment_method_obj->payment_refund( $transaction['payment_token'] );
$this->log( 'Individual refund request result.', $attendee->ID, $result, 'refund' );
if ( CampTix_Plugin::PAYMENT_STATUS_REFUNDED == $result ) {
foreach ( $attendees as $attendee ) {
Expand Down