Skip to content

Instantly share code, notes, and snippets.

@ChrisFlannagan
Created April 27, 2017 02:25
Show Gist options
  • Save ChrisFlannagan/e214d5df8843e9125df14501907405b8 to your computer and use it in GitHub Desktop.
Save ChrisFlannagan/e214d5df8843e9125df14501907405b8 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Give - Free
**/
/**
* Register our plugin so it shows up as an option in the Give gateway settings
*/
add_filter( 'give_payment_gateways', function() {
// Here replace 'give_free' with a unique slug for your plugin. You will use this slug throughout this plugin.
$gateways['give_free'] = array(
'admin_label' => esc_attr__( 'Free', 'give-free' ),
'checkout_label' => esc_attr__( 'Free', 'give-free' )
);
return $gateways;
} );
/**
* This action will run the function attached to it when it's time to process the donation submission.
*
* Here you use the slug you made above at the end of the action name, give_gateway_YOUR_SLUG
**/
add_action( 'give_gateway_give_free', function( $purchase_data ) {
$payment_data = array(
'price' => $purchase_data['price'],
'give_form_title' => $purchase_data['post_data']['give-form-title'],
'give_form_id' => intval( $purchase_data['post_data']['give-form-id'] ),
'give_price_id' => isset( $purchase_data['post_data']['give-price-id'] ) ? $purchase_data['post_data']['give-price-id'] : '',
'date' => $purchase_data['date'],
'user_email' => $purchase_data['user_email'],
'purchase_key' => $purchase_data['purchase_key'],
'currency' => give_get_currency(),
'user_info' => $purchase_data['user_info'],
'status' => 'pending', /** THIS MUST BE SET TO PENDING TO AVOID PHP WARNINGS */
'gateway' => 'give_free' /** USE YOUR SLUG AGAIN HERE */
);
/**
* Here you will reach out to whatever payment processor you are building for and record a successful payment
*
* If it's not correct, make $payment false and attach errors
*/
// record the payment which is super important so you have the proper records in the Give administration
$payment = give_insert_payment( $payment_data );
if ( $payment ) {
give_update_payment_status( $payment, 'publish' ); /** This line will finalize the donation, you can run some other verification function if you want before setting to publish */
give_send_to_success_page();
} else {
// if errors are present, send the user back to the donation form so they can be corrected
give_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['give-gateway'] );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment