/**
  * Mark a referral as complete when an payment is received
  *
  * @access  public
  * @since   1.7
  */
 public function generate_referral()
 {
     if (!empty($_REQUEST['s2member_affiliatewp_notify']) && 'payment' === $_REQUEST['s2member_affiliatewp_notify']) {
         $auth_key = defined('AUTH_KEY') ? AUTH_KEY : '';
         $secret_auth = defined('SECURE_AUTH_KEY') ? SECURE_AUTH_KEY : '';
         $hash = md5($auth_key . home_url() . get_bloginfo('admin_email') . $secret_auth);
         if (empty($_REQUEST['secret']) || !hash_equals($hash, $_REQUEST['secret'])) {
             return;
         }
         if (!empty($_REQUEST['user_id']) && !empty($_REQUEST['amount']) && !empty($_REQUEST['affiliate_id'])) {
             $affiliate_id = (int) $_REQUEST['affiliate_id'];
             $user_id = (int) $_REQUEST['user_id'];
             $amount = affwp_sanitize_amount($_REQUEST['amount']);
             $txn_id = sanitize_text_field($_REQUEST['txn_id']);
             $item_name = sanitize_text_field($_REQUEST['item_name']);
             $user_ip = sanitize_text_field($_REQUEST['ip']);
             $item_number = sanitize_text_field($_REQUEST['item_number']);
             $payer_email = sanitize_text_field($_REQUEST['payer_email']);
             $args = array('user_id' => $user_id, 'amount' => $amount, 'txn_id' => $txn_id, 'desc' => $item_name, 'affiliate_id' => $affiliate_id);
             $this->add_pending_referral($args);
             $this->complete_referral($txn_id);
         }
         exit;
     }
 }
 /**
  * Get the data being exported
  *
  * @access public
  * @since 1.0
  * @return array $data Data for Export
  */
 public function get_data()
 {
     $args = array('status' => 'unpaid', 'date' => !empty($this->date) ? $this->date : '', 'number' => -1);
     // Final data to be exported
     $data = array();
     // The affiliates that have earnings to be paid
     $affiliates = array();
     // The list of referrals that are possibly getting marked as paid
     $to_maybe_pay = array();
     // Retrieve the referrals from the database
     $referrals = affiliate_wp()->referrals->get_referrals($args);
     // The minimum payout amount
     $minimum = !empty($_POST['minimum']) ? sanitize_text_field(affwp_sanitize_amount($_POST['minimum'])) : 0;
     if ($referrals) {
         foreach ($referrals as $referral) {
             if (in_array($referral->affiliate_id, $affiliates)) {
                 // Add the amount to an affiliate that already has a referral in the export
                 $amount = $data[$referral->affiliate_id]['amount'] + $referral->amount;
                 $data[$referral->affiliate_id]['amount'] = $amount;
             } else {
                 $email = affwp_get_affiliate_email($referral->affiliate_id);
                 $data[$referral->affiliate_id] = array('email' => $email, 'amount' => $referral->amount, 'currency' => !empty($referral->currency) ? $referral->currency : affwp_get_currency());
                 $affiliates[] = $referral->affiliate_id;
             }
             // Add the referral to the list of referrals to maybe payout
             if (!array_key_exists($referral->affiliate_id, $to_maybe_pay)) {
                 $to_maybe_pay[$referral->affiliate_id] = array();
             }
             $to_maybe_pay[$referral->affiliate_id][] = $referral->referral_id;
         }
         // Now determine which affiliates are above the minimum payout amount
         if ($minimum > 0) {
             foreach ($data as $affiliate_id => $payout) {
                 if ($payout['amount'] < $minimum) {
                     unset($data[$affiliate_id]);
                     unset($to_maybe_pay[$affiliate_id]);
                 }
             }
         }
         // We now know which referrals should be marked as paid
         foreach ($to_maybe_pay as $referral_list) {
             foreach ($referral_list as $referral_id) {
                 affwp_set_referral_status($referral_id, 'paid');
             }
         }
     }
     $data = apply_filters('affwp_export_get_data', $data);
     $data = apply_filters('affwp_export_get_data_' . $this->export_type, $data);
     return $data;
 }
 /**
  * Get the purchase total
  *
  * @access  private
  * @since   1.6
  */
 public function get_total()
 {
     global $ninja_forms_processing;
     $total = $ninja_forms_processing->get_calc_total();
     if (is_array($total)) {
         // If this is an array, grab the string total.
         if (isset($total['total'])) {
             $purchase_total = $total['total'];
         } else {
             $purchase_total = '';
         }
     } else {
         // This isn't an array, so $purchase_total can just be set to the string value.
         if (!empty($total)) {
             $purchase_total = $total;
         } else {
             $purchase_total = 0.0;
         }
     }
     return affwp_sanitize_amount($purchase_total);
 }
Esempio n. 4
0
 /**
  * Update a referral
  *
  * @access  public
  * @since   1.5
  */
 public function update_referral($referral_id = 0, $data = array())
 {
     if (empty($referral_id)) {
         return false;
     }
     $referral = $this->get($referral_id);
     if (!$referral) {
         return false;
     }
     if (isset($data['amount'])) {
         $data['amount'] = affwp_sanitize_amount($data['amount']);
     }
     if (!empty($data['products'])) {
         $data['products'] = maybe_serialize($data['products']);
     }
     $update = $this->update($referral_id, $data, '', 'referral');
     if ($update) {
         if (!empty($data['status']) && $referral->status !== $data['status']) {
             affwp_set_referral_status($referral, $data['status']);
         } elseif ('paid' === $referral->status) {
             if ($referral->amount > $data['amount']) {
                 $change = $referral->amount - $data['amount'];
                 affwp_decrease_affiliate_earnings($referral->affiliate_id, $change);
             } elseif ($referral->amount < $data['amount']) {
                 $change = $data['amount'] - $referral->amount;
                 affwp_increase_affiliate_earnings($referral->affiliate_id, $change);
             }
         }
         return $update;
     }
     return false;
 }
Esempio n. 5
0
    /**
     * Output the conversion tracking script
     *
     * @since 1.0
     */
    public function conversion_script($args = array())
    {
        $defaults = array('amount' => '', 'description' => '', 'context' => '', 'campaign' => '', 'reference' => '');
        $args = wp_parse_args($args, $defaults);
        if (empty($args['amount']) && !empty($_REQUEST['amount']) && 0 !== $args['amount']) {
            // Allow the amount to be passed via a query string or post request
            $args['amount'] = affwp_sanitize_amount(sanitize_text_field(urldecode($_REQUEST['amount'])));
        }
        if (empty($args['reference']) && !empty($_REQUEST['reference'])) {
            // Allow the reference to be passed via a query string or post request
            $args['reference'] = sanitize_text_field($_REQUEST['reference']);
        }
        if (empty($args['context']) && !empty($_REQUEST['context'])) {
            $args['context'] = sanitize_text_field($_REQUEST['context']);
        }
        if (empty($args['description']) && !empty($_REQUEST['description'])) {
            $args['description'] = sanitize_text_field($_REQUEST['description']);
        }
        if (empty($args['status']) && !empty($_REQUEST['status'])) {
            $args['status'] = sanitize_text_field($_REQUEST['status']);
        }
        if (empty($args['campaign']) && !empty($_REQUEST['campaign'])) {
            $args['campaign'] = sanitize_text_field($_REQUEST['campaign']);
        }
        $md5 = md5($args['amount'] . $args['description'] . $args['reference'] . $args['context'] . $args['status']);
        ?>
		<script type="text/javascript">
		jQuery(document).ready(function($) {

			var ref   = $.cookie( 'affwp_ref' );
			var visit = $.cookie( 'affwp_ref_visit_id' );

			// If a referral var is present and a referral cookie is not already set
			if( ref && visit ) {

				// Fire an ajax request to log the hit
				$.ajax({
					type: "POST",
					data: {
						action      : 'affwp_track_conversion',
						affiliate   : ref,
						amount      : '<?php 
        echo $args["amount"];
        ?>
',
						status      : '<?php 
        echo $args["status"];
        ?>
',
						description : '<?php 
        echo $args["description"];
        ?>
',
						context     : '<?php 
        echo $args["context"];
        ?>
',
						reference   : '<?php 
        echo $args["reference"];
        ?>
',
						campaign    : '<?php 
        echo $args["campaign"];
        ?>
',
						md5         : '<?php 
        echo $md5;
        ?>
'
					},
					url: affwp_scripts.ajaxurl,
					success: function (response) {
						if ( window.console && window.console.log ) {
							console.log( response );
						}
					}

				}).fail(function (response) {
					if ( window.console && window.console.log ) {
						console.log( response );
					}
				}).done(function (response) {
				});

			}

		});
		</script>
<?php 
    }