Skip to content

Instantly share code, notes, and snippets.

@mehul0810
Created April 22, 2020 13:35
Show Gist options
  • Save mehul0810/7fdc68205ba6e91367439c966c0a93b6 to your computer and use it in GitHub Desktop.
Save mehul0810/7fdc68205ba6e91367439c966c0a93b6 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: My PaiementPro with GiveWP
*.Version: 1.0.0
* Author: Mehul Gohil
*/
/**
* Register payment method.
*
* @since 1.0.0
*
* @param array $gateways List of registered gateways.
*
* @return array
*/
// change the prefix insta_for_give here to avoid collisions with other functions
function mppg_register_payment_method( $gateways ) {
// Duplicate this section to add support for multiple payment method from a custom payment gateway.
$gateways['paiementpro'] = array(
'admin_label' => __( 'Paiement Pro - Mobile Money', 'mppg' ), // This label will be displayed under Give settings in admin.
'checkout_label' => __( 'Mobile Money', 'mppg' ), // This label will be displayed on donation form in frontend.
);
return $gateways;
}
add_filter( 'give_payment_gateways', 'mppg_register_payment_method' );
/**
* Register Section for Payment Gateway Settings.
*
* @param array $sections List of payment gateway sections.
*
* @since 1.0.0
*
* @return array
*/
// change the insta_for_give prefix to avoid collisions with other functions.
function mppg_register_payment_gateway_sections( $sections ) {
// `instamojo-settings` is the name/slug of the payment gateway section.
$sections['paiementpro-settings'] = __( 'PaiementPro', 'mppg' );
return $sections;
}
add_filter( 'give_get_sections_gateways', 'mppg_register_payment_gateway_sections' );
/**
* Register Admin Settings.
*
* @param array $settings List of admin settings.
*
* @since 1.0.0
*
* @return array
*/
function mppg_register_payment_gateway_setting_fields( $settings ) {
switch ( give_get_current_setting_section() ) {
case 'paiementpro-settings':
$settings = array(
array(
'id' => 'give_title_paiementpro',
'type' => 'title',
),
);
$settings[] = array(
'name' => __( 'API Key', 'give-square' ),
'desc' => __( 'Enter your API Key, found in your Instamojo Dashboard.', 'mppg' ),
'id' => 'mggp_api_key',
'type' => 'text',
);
$settings[] = array(
'id' => 'give_title_paiementpro',
'type' => 'sectionend',
);
break;
} // End switch().
return $settings;
}
// change the insta_for_give prefix to avoid collisions with other functions.
add_filter( 'give_get_settings_gateways', 'mppg_register_payment_gateway_setting_fields' );
/**
* Process Square checkout submission.
*
* @param array $posted_data List of posted data.
*
* @since 1.0.0
* @access public
*
* @return void
*/
// change the insta_for_give prefix to avoid collisions with other functions.
function mppg_process_instamojo_donation( $posted_data ) {
// Make sure we don't have any left over errors present.
give_clear_errors();
// Any errors?
$errors = give_get_errors();
// No errors, proceed.
if ( ! $errors ) {
$form_id = intval( $posted_data['post_data']['give-form-id'] );
$price_id = ! empty( $posted_data['post_data']['give-price-id'] ) ? $posted_data['post_data']['give-price-id'] : 0;
$donation_amount = ! empty( $posted_data['price'] ) ? $posted_data['price'] : 0;
// Setup the payment details.
$donation_data = array(
'price' => $donation_amount,
'give_form_title' => $posted_data['post_data']['give-form-title'],
'give_form_id' => $form_id,
'give_price_id' => $price_id,
'date' => $posted_data['date'],
'user_email' => $posted_data['user_email'],
'purchase_key' => $posted_data['purchase_key'],
'currency' => give_get_currency( $form_id ),
'user_info' => $posted_data['user_info'],
'status' => 'pending',
'gateway' => 'instamojo',
);
// Record the pending donation.
$donation_id = give_insert_payment( $donation_data );
if ( ! $donation_id ) {
// Record Gateway Error as Pending Donation in Give is not created.
give_record_gateway_error(
__( 'paiementpro Error', 'mppg' ),
sprintf(
/* translators: %s Exception error message. */
__( 'Unable to create a pending donation with Give.', 'mppg' )
)
);
// Send user back to checkout.
give_send_back_to_checkout( '?payment-mode=paiementpro' );
return;
}
// Do the actual payment processing using the custom payment gateway API. To access the GiveWP settings, use give_get_option()
// as a reference, this pulls the API key entered above: give_get_option('insta_for_give_instamojo_api_key')
} else {
// Send user back to checkout.
give_send_back_to_checkout( '?payment-mode=paiementpro' );
} // End if().
}
// change the insta_for_give prefix to avoid collisions with other functions.
add_action( 'give_gateway_paiementpro', 'mppg_process_instamojo_donation' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment