Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. <?php
  2.  
  3. add_action( 'ms_invoice_paid', function( $invoice, $subscription ){
  4.  
  5. $membership = $subscription->get_membership();
  6. $set_up_bonus = $membership->price;
  7. global $affiliate; // Used for communication with Affiliates plugin.
  8. global $blog_id, $site_id; // Used for logging.
  9.  
  10. // Don't pay a commission if the membership is not active.
  11. if ( $subscription->status != $subscription::STATUS_ACTIVE ) {
  12. return;
  13. }
  14.  
  15. $user_id = $invoice->user_id;
  16.  
  17. $pay_once = defined( 'AFFILIATE_PAYONCE' ) && 'yes' == AFFILIATE_PAYONCE;
  18.  
  19. $user_id_referrer = get_user_meta( $user_id, 'affiliate_referred_by', true );
  20.  
  21. if ( empty( $user_id_referrer ) ) {
  22. // We do not know who referred the user, don't pay a commission.
  23. return;
  24. }
  25.  
  26. // If affiliate is aleady paid it's not the first payment. So we don't need to charge the set up.
  27. // We'll let the default action take over
  28. $affiliate_paid = get_user_meta( $user_id, 'affiliate_paid', true );
  29. if ( $affiliate_paid ) {
  30. return;
  31. }
  32.  
  33. $complete_records = $affiliate->get_complete_records(
  34. $user_id_referrer,
  35. date( 'Ym' ),
  36. array( Affiliate_Membership2_Integration::AREA_KEY ),
  37. $user_id
  38. );
  39.  
  40. if ( $pay_once && is_array( $complete_records ) ) {
  41. // Make sure that this subscription was not commissioned before.
  42. foreach ( $complete_records as $record ) {
  43. $meta = maybe_unserialize( $record->meta );
  44.  
  45. if ( $meta['subscription_id'] == $subscription->id ) {
  46. // It seems this subscription was already commissioned.
  47. return;
  48. }
  49. }
  50. }
  51.  
  52. $reward = $membership->get_custom_data( 'aff_reward_value' );
  53. $reward = max( 0, intval( $reward ) );
  54. $reward = (float) $reward / 100;
  55.  
  56. $available_types = array( 'inv', 'mem', 'abs' );
  57. $type = $membership->get_custom_data( 'aff_reward_type' );
  58.  
  59. if ( ! in_array( $type, $available_types ) ) {
  60. $type = 'abs';
  61. }
  62.  
  63. switch ( $type ) {
  64. case 'inv':
  65. $base = $invoice->subtotal; // Invoice amount without taxes.
  66. $reward = $base * $reward / 100;
  67. break;
  68.  
  69. case 'mem':
  70. $base = $membership->price; // Membership price setting.
  71. $reward = $base * $reward / 100;
  72. break;
  73.  
  74. case 'abs':
  75. default:
  76. // Reward already has correct value.
  77. break;
  78. }
  79.  
  80. $reward = round( ( $reward + $set_up_bonus ), 2, PHP_ROUND_HALF_DOWN );
  81.  
  82. // Note: lib2() used here is provided by the Membership2 plugin.
  83. $meta = array(
  84. 'subscription_id' => $subscription->id,
  85. 'invoice_id' => $invoice->id,
  86. 'gateway_id' => $invoice->gateway_id,
  87. 'transaction_id' => $invoice->external_id,
  88. 'blog_id' => $blog_id,
  89. 'site_id' => $site_id,
  90. 'current_user_id' => get_current_user_id(),
  91. 'REMOTE_URL' => $_SERVER['HTTP_REFERER'],
  92. 'LOCAL_URL' => mslib3()->net->current_url(),
  93. 'IP' => mslib3()->net->current_ip()->ip,
  94. );
  95.  
  96. $affiliate_process_payment = apply_filters( 'affiliate_process_payment', true, $user_id_referrer, $reward, $user_id, $meta );
  97.  
  98. if ( ! $affiliate_process_payment ) return;
  99.  
  100. do_action(
  101. 'affiliate_purchase',
  102. $user_id_referrer,
  103. $reward,
  104. Affiliate_Membership2_Integration::AREA_KEY,
  105. $user_id,
  106. __( 'Membership2', 'affiliate' ),
  107. $meta
  108. );
  109.  
  110. update_user_meta( $user_id, 'affiliate_paid', 'yes' );
  111.  
  112. }, 8, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement