Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action( 'ms_invoice_paid', function( $invoice, $subscription ){
- $membership = $subscription->get_membership();
- $set_up_bonus = $membership->price;
- global $affiliate; // Used for communication with Affiliates plugin.
- global $blog_id, $site_id; // Used for logging.
- // Don't pay a commission if the membership is not active.
- if ( $subscription->status != $subscription::STATUS_ACTIVE ) {
- return;
- }
- $user_id = $invoice->user_id;
- $pay_once = defined( 'AFFILIATE_PAYONCE' ) && 'yes' == AFFILIATE_PAYONCE;
- $user_id_referrer = get_user_meta( $user_id, 'affiliate_referred_by', true );
- if ( empty( $user_id_referrer ) ) {
- // We do not know who referred the user, don't pay a commission.
- return;
- }
- // If affiliate is aleady paid it's not the first payment. So we don't need to charge the set up.
- // We'll let the default action take over
- $affiliate_paid = get_user_meta( $user_id, 'affiliate_paid', true );
- if ( $affiliate_paid ) {
- return;
- }
- $complete_records = $affiliate->get_complete_records(
- $user_id_referrer,
- date( 'Ym' ),
- array( Affiliate_Membership2_Integration::AREA_KEY ),
- $user_id
- );
- if ( $pay_once && is_array( $complete_records ) ) {
- // Make sure that this subscription was not commissioned before.
- foreach ( $complete_records as $record ) {
- $meta = maybe_unserialize( $record->meta );
- if ( $meta['subscription_id'] == $subscription->id ) {
- // It seems this subscription was already commissioned.
- return;
- }
- }
- }
- $reward = $membership->get_custom_data( 'aff_reward_value' );
- $reward = max( 0, intval( $reward ) );
- $reward = (float) $reward / 100;
- $available_types = array( 'inv', 'mem', 'abs' );
- $type = $membership->get_custom_data( 'aff_reward_type' );
- if ( ! in_array( $type, $available_types ) ) {
- $type = 'abs';
- }
- switch ( $type ) {
- case 'inv':
- $base = $invoice->subtotal; // Invoice amount without taxes.
- $reward = $base * $reward / 100;
- break;
- case 'mem':
- $base = $membership->price; // Membership price setting.
- $reward = $base * $reward / 100;
- break;
- case 'abs':
- default:
- // Reward already has correct value.
- break;
- }
- $reward = round( ( $reward + $set_up_bonus ), 2, PHP_ROUND_HALF_DOWN );
- // Note: lib2() used here is provided by the Membership2 plugin.
- $meta = array(
- 'subscription_id' => $subscription->id,
- 'invoice_id' => $invoice->id,
- 'gateway_id' => $invoice->gateway_id,
- 'transaction_id' => $invoice->external_id,
- 'blog_id' => $blog_id,
- 'site_id' => $site_id,
- 'current_user_id' => get_current_user_id(),
- 'REMOTE_URL' => $_SERVER['HTTP_REFERER'],
- 'LOCAL_URL' => mslib3()->net->current_url(),
- 'IP' => mslib3()->net->current_ip()->ip,
- );
- $affiliate_process_payment = apply_filters( 'affiliate_process_payment', true, $user_id_referrer, $reward, $user_id, $meta );
- if ( ! $affiliate_process_payment ) return;
- do_action(
- 'affiliate_purchase',
- $user_id_referrer,
- $reward,
- Affiliate_Membership2_Integration::AREA_KEY,
- $user_id,
- __( 'Membership2', 'affiliate' ),
- $meta
- );
- update_user_meta( $user_id, 'affiliate_paid', 'yes' );
- }, 8, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement