Example #1
0
 function do_process()
 {
     $freebie_code = ym_request('freebie_code');
     if ($freebie_code) {
         $this->common_process($freebie_code, 'Free/Coupon', TRUE, FALSE);
         list($buy, $what, $id, $user_id) = explode('_', $freebie_code);
         if ($what == 'subscription') {
             $data = array('id' => $id, 'cost' => 'Free/Coupon', 'duration' => 1, 'item_name' => get_bloginfo() . ' ' . __('Subscription Purchase:', 'ym') . ' ' . $post_title);
         } else {
             if ($what == 'post') {
                 $data = array('post_id' => $id, 'ppp' => true, 'cost' => 'Free/Coupon', 'duration' => 1, 'item_name' => get_bloginfo() . ' ' . __('Post Purchase:', 'ym') . ' ' . get_post_title($id));
             } else {
                 // assume bundle
                 $bundle = ym_get_bundle($id);
                 $data = array('ppp_pack_id' => $id, 'ppp' => true, 'cost' => 'Free/Coupon', 'duration' => 1, 'item_name' => get_bloginfo() . ' ' . __('Bundle Purchase:', 'ym') . ' ' . $bundle->name);
             }
         }
         $this->redirectlogic($data, TRUE);
     }
     $custom = ym_request('custom');
     if (!$custom) {
         echo 'No Data Passed';
         return;
     }
     list($buy, $what, $pack_id, $user_id) = explode('_', $custom);
     // verify
     $safe = FALSE;
     global $ym_packs;
     foreach ($ym_packs->packs as $pack) {
         if ($pack['id'] == $pack_id) {
             $cost_test = $pack['cost'];
             if (strpos($cost_test, '.')) {
                 $cost_test = $cost_test * 100;
             }
             if ($cost_test == 0) {
                 $safe = TRUE;
             }
         }
     }
     if (!$safe) {
         // error
         print_r($_POST);
         echo 'Could not Find a pack match';
         return;
     }
     $this->do_buy_subscription($pack_id, $user_id, TRUE);
 }
/**
 * code to run on the login page.. could be custom feedback or some js.
 * add_action('ym_return_true','yourfunction'); will fire up here :)
 *
 */
function ym_login_js()
{
    // gateways
    $gateways = get_option('ym_modules');
    foreach ($gateways as $way) {
        if (ym_get('from_gateway') == $way) {
            $js = new $way();
            if (isset($js->callback_script)) {
                $js = $js->callback_script;
                global $current_user;
                get_currentuserinfo();
                $js = str_replace('[user_id]', $current_user->ID, $js);
                $js = str_replace('[pack_id]', ym_get('pack_id'), $js);
                $js = str_replace('[post_id]', ym_get('post_id'), $js);
                $js = str_replace('[post_pack_id]', ym_get('post_pack_id'), $js);
                $js = str_replace('[item_code]', ym_get('item'), $js);
                $cost = 0;
                $account_type = '';
                if (ym_get('pack_id')) {
                    foreach (ym_get_packs() as $pack) {
                        if ($pack['id'] == ym_get('pack_id')) {
                            $cost = $pack['cost'];
                            $account_type = $pack['account_type'];
                            break;
                        }
                    }
                } else {
                    if (ym_get('post_id')) {
                        // post
                        $cost = get_post_meta(ym_get('post_id'), '_ym_post_purchasable_cost', true);
                    } else {
                        if (ym_get('post_pack_id')) {
                            // bundle
                            $bundle = ym_get_bundle(ym_get('post_pack_id'));
                            $cost = $bundle->cost;
                        }
                    }
                }
                $js = str_replace('[cost]', $cost, $js);
                $js = str_replace('[account_type]', $account_type, $js);
                add_shortcode('if_cb_pack', 'ym_login_js_cb_pack');
                add_shortcode('if_cb_post', 'ym_login_js_cb_post');
                add_shortcode('if_cb_bundle', 'ym_login_js_cb_bundle');
                $js = do_shortcode($js);
                $js = apply_filters('ym_login_js', $js);
                echo $js;
            }
            do_action('ym_return_true');
        }
    }
    // enctype not on payment buttons please!
    if (isset($_REQUEST['ym_subscribe'])) {
        return;
    }
    // it only breaks zombaio.....
    // but payment gateways arn't specific classed.
    // check for zombaio
    echo '
<script type="text/javascript">
jQuery(document).ready(function() {
	jQuery(\'form\').attr(\'enctype\', \'multipart/form-data\');
});
</script>
';
}
function ym_get_user_purchase_history($userID = FALSE, $limit = 5)
{
    if (!$userID) {
        global $current_user;
        get_current_user();
        $userID = $current_user->ID;
    }
    global $wpdb;
    $html = '';
    // get log
    $query = 'SELECT DISTINCT(transaction_id) FROM ' . $wpdb->prefix . 'ym_transaction
		WHERE user_id = ' . $userID . '
		ORDER BY unixtime DESC
		LIMIT ' . $limit;
    foreach ($wpdb->get_results($query) as $trans) {
        $id = $trans->transaction_id;
        $sub = 'SELECT * FROM ' . $wpdb->prefix . 'ym_transaction WHERE transaction_id = ' . $id . ' AND user_id = ' . $userID;
        $data = array();
        foreach ($wpdb->get_results($sub) as $log) {
            $data[$log->action_id] = $log->data;
        }
        if (isset($data[YM_PPP_PURCHASED])) {
            // Pay Per Post
            $html .= '<li>' . __('Content Purchase: ', 'ym') . get_the_title($data[YM_PPP_PURCHASED]) . '</li>';
        } else {
            if (isset($data[YM_PPP_PACK_PURCHASED])) {
                // bundle
                $html .= '<li>' . __('Content Purchase: ', 'ym');
                $bundle = ym_get_bundle($data[YM_PPP_PACK_PURCHASED]);
                $html .= $bundle->name . '</li>';
            } else {
                if (isset($data[YM_PACKAGE_PURCHASED])) {
                    $pack = $data[YM_PACKAGE_PURCHASED];
                    $html .= '<li>';
                    $html .= ym_get_pack_label($pack, FALSE, FALSE);
                    $html .= '</li>';
                } else {
                    if (isset($data[YM_PAYMENT])) {
                        $html .= '<li>' . __('Unknown Payment', 'ym') . ' ' . $data[YM_PAYMENT] . '</li>';
                    }
                }
            }
        }
    }
    if (empty($html)) {
        return FALSE;
    }
    $html = '<ul>' . $html . '</ul>';
    return $html;
}
 function notify_user($packet, $nomore = false)
 {
     if (isset($this->nomore_email) && $this->nomore_email) {
         return;
     }
     $this->nomore_email = $nomore;
     global $ym_res;
     $user = get_userdata($packet['user_id']);
     $message_id = '';
     // make sure its an int
     if ($packet['status']) {
         $packet['status'] = 1;
     } else {
         $packet['status'] = 0;
     }
     if (isset($packet['post_id']) && $packet['post_id']) {
         $message_id = 'post_' . $packet['status'];
     } else {
         if (isset($packet['ppack_id']) && $packet['ppack_id']) {
             $message_id = 'ppack_' . $packet['status'];
         } else {
             $message_id = 'pack_' . $packet['status'];
         }
     }
     $target = $subject = $message = $target_scan = '';
     $do = FALSE;
     if (method_exists($this, 'messages')) {
         // if data returned then send email here
         // otherwise no send or payment gateway handles
         // data returns message to send
         $data = $this->messages($message_id, $user, $packet);
         if ($data) {
             $target = $data['to'];
             $subject = $data['subject'];
             $message = $data['message'];
             $target_scan = $data['target_scan'];
             $do = TRUE;
         }
     } else {
         $target = $user->display_name . ' <' . $user->user_email . '>';
         $display_name = $user->display_name;
         // use default messages
         switch ($message_id) {
             case 'post_1':
                 if ($ym_res->payment_gateway_enable_post_success) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_post_success';
                 $posttitle = get_the_title($packet['post_id']);
                 $posttitle = strip_tags($posttitle);
                 $postlink = get_permalink($packet['post_id']);
                 $subject = $ym_res->payment_gateway_subject_post_success;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[post_title]', $posttitle, $subject);
                 $message = $ym_res->payment_gateway_message_post_success;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[post_title]', $posttitle, $message);
                 $message = str_replace('[post_link]', $postlink, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
             case 'post_0':
                 if ($ym_res->payment_gateway_enable_post_failed) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_post_failed';
                 $posttitle = get_the_title($packet['post_id']);
                 $posttitle = strip_tags($posttitle);
                 $subject = $ym_res->payment_gateway_subject_post_failed;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[post_title]', $posttitle, $subject);
                 $message = $ym_res->payment_gateway_message_post_failed;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[post_title]', $posttitle, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
             case 'ppack_1':
                 if ($ym_res->payment_gateway_enable_ppack_success) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_ppack_success';
                 $pack_data = ym_get_bundle($packet['ppack_id']);
                 $posts = ym_get_bundle_posts($packet['ppack_id']);
                 $post_urls = '';
                 foreach ($posts as $post) {
                     $post_urls .= '<a href="' . get_permalink($post->post_id) . '">' . get_the_title($post->post_id) . '</a><br />';
                 }
                 $subject = $ym_res->payment_gateway_subject_ppack_success;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[pack_title]', $pack_data->name, $subject);
                 $message = $ym_res->payment_gateway_message_ppack_success;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[pack_name]', $pack_data->name, $message);
                 $message = str_replace('[posts_in_pack]', $post_urls, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
             case 'ppack_0':
                 if ($ym_res->payment_gateway_enable_ppack_failed) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_ppack_failed';
                 $pack_data = ym_get_bundle($packet['ppack_id']);
                 $subject = $ym_res->payment_gateway_subject_ppack_failed;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[pack_title]', $pack_data->name, $subject);
                 $message = $ym_res->payment_gateway_message_ppack_failed;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[pack_name]', $pack_data->name, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
             case 'pack_1':
                 if ($ym_res->payment_gateway_enable_subscription_success) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_subscription_success';
                 $label = ym_get_pack_label($packet['pack_id']);
                 $label = strip_tags($label);
                 $user = new YourMember_User($packet['user_id']);
                 $expire = $user->expire_date;
                 $f = YM_DATE;
                 $expire = date($f, $expire);
                 $subject = $ym_res->payment_gateway_subject_subscription_success;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[pack_label]', $label, $subject);
                 $message = $ym_res->payment_gateway_message_subscription_success;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[pack_label]', $label, $message);
                 $message = str_replace('[pack_expire]', $expire, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
             case 'pack_0':
                 if ($ym_res->payment_gateway_enable_subscription_failed) {
                     $do = TRUE;
                 }
                 $target_scan = 'payment_gateway_email_subscription_failed';
                 $label = ym_get_pack_label($packet['pack_id']);
                 $label = strip_tags($label);
                 $subject = $ym_res->payment_gateway_subject_subscription_failed;
                 $subject = str_replace('[blogname]', get_option('blogname'), $subject);
                 $subject = str_replace('[pack_label]', $label, $subject);
                 $message = $ym_res->payment_gateway_message_subscription_failed;
                 $message = str_replace('[display_name]', $display_name, $message);
                 $message = str_replace('[pack_label]', $label, $message);
                 $message = str_replace('[blogname]', get_option('blogname'), $message);
                 break;
         }
     }
     remove_all_shortcodes();
     // login user to alow shortcodes to behave
     wp_set_current_user($packet['user_id']);
     // apply ym_user_custom shortcode
     add_shortcode('ym_user_is', 'ym_user_is');
     add_shortcode('ym_user_is_not', 'ym_user_is_not');
     add_shortcode('ym_user_custom', 'ym_shortcode_user');
     $message = do_shortcode($message);
     // transaction log ID
     global $ym_this_transaction_id;
     $log_id = 'YMTRANS_' . $ym_this_transaction_id;
     $message = str_replace('[ym_log_id]', $log_id, $message);
     $message_prepend = __('This is a copy of a Message not sent', 'ym');
     if ($do) {
         ym_email($target, $subject, $message);
         $message_prepend = __('This is a copy of a Message sent', 'ym');
     }
     // additional targets?
     if (is_array($ym_res->{$target_scan})) {
         $subject = 'CC: ' . $subject;
         $message = $message_prepend . '<br /><br />' . $message;
         foreach ($ym_res->{$target_scan} as $target) {
             if ($target) {
                 ym_email($target, $subject, $message);
             }
         }
     }
 }
function ym_bundle_units_limit($args)
{
    $bundle_id = isset($args['bundle_id']) ? $args['bundle_id'] : FALSE;
    if ($bundle_id) {
        return '';
    }
    $bundle = ym_get_bundle($bundle_id);
    if (!$bundle) {
        return '';
    }
    $limit = $bundle->purchaselimit;
    if ($limit) {
        return $limit;
    } else {
        return __('No Purchase Limit', 'ym');
    }
}
		<tr>
			<th>' . __('User ID', 'ym') . '</th>
			<th>' . __('User', 'ym') . '</th>
			<th>' . __('User Email', 'ym') . '</th>			
			<th>' . __('Bundles Purchased', 'ym') . '</th>
			<th>' . __('Date', 'ym') . '</th>
		</tr>
	</thead>
	<tbody>';
    foreach ($transactions as $transaction) {
        if ($transaction->user_login) {
            $profile = '<a alt="View user profile" title="View user profile" href="' . YM_ADMIN_URL . 'user-edit.php?user_id=' . $transaction->user_id . '" target="_top">' . $transaction->user_login . '</a>';
        } else {
            $profile = 'Deleted User';
        }
        $bundle = ym_get_bundle($transaction->data);
        echo '<tr>
			<td>' . $transaction->user_id . '</td>
			<td>' . $profile . '</td>
			<td>' . $transaction->user_email . '</td>			
			<td>' . '(' . $bundle->id . ') ' . $bundle->name . '</td>
			<td>' . date(YM_DATE, $transaction->unixtime) . '</td>
		</tr>';
    }
    echo '</tbody>
	<tfoot>' . $form . '</tfoot>
	</table>';
} else {
    echo __('<em>There are no Purchases logged.</em>', 'ym');
}
echo ym_end_box();
} else {
    echo '
		<tr>
			<td colspan="9">' . __('There are currently no bundles in the database.', 'ym') . '</td>
		</tr>';
}
echo '
	</tbody>
</table>
';
echo ym_end_box();
/**
Creating
*/
echo ym_start_box(__('Create New Bundle', 'ym'));
$bundle = ym_get_bundle('');
ym_bundle_form($bundle, __('Create Bundle', 'ym'));
echo ym_end_box();
/**
Gifting
*/
echo ym_start_box(__('Gift a Bundle', 'ym'));
global $wpdb, $ym_formgen;
echo '<form action="" method="post">';
echo '<table style="width: 100%;"><tr><td>' . __('Select a User', 'ym') . '</td><td>' . __('Select a Bundle', 'ym') . '</td></tr><tr><td>';
$user_sql = 'SELECT DISTINCT(ID) AS value, user_login AS label
			FROM ' . $wpdb->users . ' u
			ORDER BY user_login';
$ym_formgen->render_combo_from_query('user_to_gift', $user_sql);
echo '</td><td>';
echo '<select name="bundle_to_gift">';
Example #8
0
    function do_process()
    {
        $action = ym_request('action');
        if ($action == 'js') {
            header('Content-Type: text/javascript');
            ?>
jQuery(document).ready(function() {
	jQuery('.ym_braintree_button').click(function(event) {
		event.preventDefault();

		jQuery('.ym_form').slideUp();
		jQuery('#<?php 
            echo $this->code;
            ?>
_cc_form_unique_' + jQuery(this).attr('data-unique')).slideDown();
	});

	var braintree = Braintree.create("<?php 
            echo $this->encryptionkey;
            ?>
");
	jQuery('.<?php 
            echo $this->code;
            ?>
_cc_form').submit(function(e) {
		e.preventDefault();

		jQuery('.ym_braintree_icon').addClass('ym_ajax_loading_image');

		var target = jQuery(this);
		target.find('.error').remove();

		var data = jQuery(this).clone();
		data.find('#braintree_credit_card_number').val(braintree.encrypt(jQuery(this).find('#braintree_credit_card_number').val()));
		data.find('#braintree_credit_card_ccv').val(braintree.encrypt(jQuery(this).find('#braintree_credit_card_ccv').val()));
		data.find('#braintree_credit_card_exp').val(braintree.encrypt(jQuery(this).find('#braintree_credit_card_exp').val()));

		target.find('input').attr('disabled', 'disabled');

		jQuery.post('<?php 
            echo $this->action_url;
            ?>
&action=ajax', data.serialize(), function(resp) {
			jQuery('.ym_braintree_icon').removeClass('ym_ajax_loading_image');

			resp = jQuery.parseJSON(resp);
			if (resp['ok']) {
				target.find('#braintree_credit_card_number').val('');
				target.find('#braintree_credit_card_ccv').val('');
				target.find('#braintree_credit_card_exp').val('');

				jQuery('<div class="success"><p>' + resp['message'] + '</p></div>').prependTo(target);

				document.location = resp['url'];
			} else {
				target.find('input').removeAttr('disabled');
				jQuery('<div class="error"><p>' + resp['message'] + '</p></div>').prependTo(target);
			}
		});
	});
});
<?php 
            exit;
        } else {
            if ($action == 'ajax') {
                ob_start();
                $this->_braintree();
                // issue sale or subscribe
                $code = $_POST['code'];
                list($buy, $what, $pack_id, $user_id) = explode('_', $code);
                // credit card update
                $result = Braintree_Customer::update('ym_' . $user_id, array('creditCard' => array('number' => $_POST['customer']['credit_card']['number'], 'cvv' => $_POST['customer']['credit_card']['cvv'], 'expirationDate' => $_POST['customer']['credit_card']['expiration_date'])));
                if ($result->success) {
                    // grab token and subscribe
                    //				if ($pack['num_cycles'] == 1 || $planId) {
                    if ($what == 'subscription') {
                        // above catches both kinds of package/subscription
                        $pack = ym_get_pack_by_id($pack_id);
                        $planId = isset($pack['braintree_plan_id']) ? $pack['braintree_plan_id'] : false;
                        // initiate charge against just added credit card
                        if ($planId) {
                            $result = Braintree_Subscription::create(array('planId' => $planId, 'paymentMethodToken' => $result->customer->creditCards[0]->token));
                            $amount = $result->subscription->transactions[0]->amount;
                        } else {
                            $result = Braintree_Transaction::sale(array('amount' => $pack['cost'], 'options' => array('submitForSettlement' => true), 'customerId' => $result->customer->id, 'paymentMethodToken' => $result->customer->creditCards[0]->token));
                            $amount = $result->transaction->amount;
                        }
                        if ($result->success) {
                            // common
                            $this->common_process($code, $amount, true, false);
                            // thanks
                            $url = $this->redirectlogic($pack);
                            $r = array('ok' => true, 'url' => $url, 'message' => __('Payment Complete', 'ym'));
                        } else {
                            $r = $this->_failedBraintree($result, true);
                        }
                    } else {
                        if ($what == 'bundle' || $what == 'post') {
                            // post or bundle purchase
                            if ($what == 'post') {
                                $cost = get_post_meta($pack_id, '_ym_post_purchasable_cost', true);
                            } else {
                                $bundle = ym_get_bundle($pack_id);
                                if (!$bundle) {
                                    $r = array('ok' => false, 'message' => __('Bundle Error', 'ym'));
                                } else {
                                    $cost = $bundle->cost;
                                }
                            }
                            if ($cost) {
                                $result = Braintree_Transaction::sale(array('amount' => $cost, 'options' => array('submitForSettlement' => true), 'customerId' => $result->customer->id, 'paymentMethodToken' => $result->customer->creditCards[0]->token));
                                $amount = $result->transaction->amount;
                                if ($result->success) {
                                    // common
                                    $this->common_process($code, $amount, true, false);
                                    // thanks
                                    if ($what == 'subscription') {
                                        $url = $this->redirectlogic($pack);
                                    } else {
                                        if ($what == 'post') {
                                            $url = $this->redirectlogic(array('ppp' => true, 'post_id' => $pack_id));
                                        } else {
                                            $url = $this->redirectlogic(array('ppp' => true, 'ppp_pack_id' => $pack_id));
                                        }
                                    }
                                    $r = array('ok' => true, 'url' => $url, 'message' => __('Payment Complete', 'ym'));
                                } else {
                                    $r = $this->_failedBraintree($result, true);
                                }
                            }
                        } else {
                            // unhandled purchase
                            $r = $this->_failedBraintree($result, true);
                        }
                    }
                } else {
                    $r = $this->_failedBraintree($result, true);
                }
                ob_clean();
                echo json_encode($r);
                // bugger
                exit;
                // non ajax/primary js failed
                // transparent redirect handlers
            } else {
                if ($action == 'process') {
                    $this->_braintree();
                    $queryString = $_SERVER['QUERY_STRING'];
                    try {
                        $result = Braintree_TransparentRedirect::confirm($queryString);
                    } catch (Exception $e) {
                        if (get_class($e) == 'Braintree_Exception_NotFound') {
                            echo 'not found';
                        } else {
                            echo '<pre>';
                            print_r($e);
                            echo $e->getMessage();
                        }
                        exit;
                    }
                    if ($result->success) {
                        $code = ym_request('code');
                        // grab token and subscribe
                        list($buy, $what, $pack_id, $user_id) = explode('_', $code);
                        $pack = ym_get_pack_by_id($pack_id);
                        $planId = isset($pack['braintree_plan_id']) ? $pack['braintree_plan_id'] : false;
                        if ($pack['num_cycles'] == 1 || $planId) {
                            // initiate charge against just added credit card
                            if ($planId) {
                                $result = Braintree_Subscription::create(array('planId' => $planId, 'paymentMethodToken' => $result->customer->creditCards[0]->token));
                                $amount = $result->subscription->transactions[0]->amount;
                            } else {
                                $result = Braintree_Transaction::sale(array('amount' => $pack['cost'], 'options' => array('submitForSettlement' => true), 'customerId' => $result->customer->id, 'paymentMethodToken' => $result->customer->creditCards[0]->token));
                                $amount = $result->transaction->amount;
                            }
                            if ($result->success) {
                                // common
                                $this->common_process($code, $amount, true, false);
                                // thanks
                                $this->redirectlogic($pack, true);
                                exit;
                            } else {
                                $this->_failedBraintree($result);
                            }
                        } else {
                            $this->_failedBraintree($result);
                        }
                        exit;
                    }
                    $this->_failedBraintree($result);
                } else {
                    $this->_failedBraintree();
                }
            }
        }
    }