Пример #1
0
 /**
  * Check for subscription renewal
  */
 function renew_subscription($order_id, $die = true)
 {
     $product_id = get_post_meta($order_id, '_acp_order_product_id', true);
     if (!$product_id) {
         return false;
     }
     $coupon_id = get_post_meta($order_id, '_acp_order_member_id', true);
     $order_details = array('_acp_order_member_id' => get_post_meta($order_id, '_acp_order_member_id', true), '_acp_order_product_id' => $product_id, '_acp_order_price' => AccessPress_Products::get_product_price($product_id, $coupon_id), '_acp_order_time' => get_post_meta($order_id, '_acp_order_time', true), '_acp_order_renewal_time' => get_post_meta($order_id, '_acp_order_renewal_time', true));
     $gateway = self::find_gateway($order_id);
     if (!$gateway) {
         return new WP_Error('no_gateway', __('No gateway configured for order', 'premise'));
     }
     $args = array('product_id' => $product_id, 'order_details' => $order_details, 'cc_profile_id' => get_user_option("memberaccess_{$gateway->payment_method}_profile_id", $order_details['_acp_order_member_id']), 'cc_payment_profile_id' => get_user_option("memberaccess_{$gateway->payment_method}_payment_{$product_id}", $order_details['_acp_order_member_id']));
     $result = $gateway->process_order($args);
     if (is_wp_error($result)) {
         if ($die) {
             wp_die($result->get_error_message());
         }
         return $result;
     }
     // update payment list on limited recurring payments
     $number_payments = get_post_meta($product_id, '_acp_product_number_payments', true);
     if ($number_payments) {
         $payments = get_post_meta($order_id, '_acp_order_payments', true);
         if (empty($payments)) {
             $payments = array();
         }
         $payments[time()] = $order_details['_acp_order_price'];
         $result['_acp_order_payments'] = $payments;
         if (count($payments) >= $number_payments) {
             unset($result['_acp_order_renewal_time']);
         }
     }
     foreach (array('_acp_order_price', '_acp_order_renewal_time', '_acp_order_anet_transaction_id', '_acp_order_payments') as $key) {
         if (isset($result[$key])) {
             update_post_meta($order_id, $key, $result[$key]);
         } else {
             delete_post_meta($order_id, $key);
         }
     }
     /** this is ugly but it's in a new window, so user still has original window */
     if ($die) {
         die(__('Subscription renewed. You can close the window.', 'premise'));
     }
     return $args;
 }
Пример #2
0
    /**
     * Handle the postback of the payment gateway form.
     *
     * @since 0.1.0
     */
    public function _process_order($args)
    {
        // create local user
        $user_id = $args['order_details']['_acp_order_member_id'];
        // we need a success & cancel url
        $base_url = add_query_arg(array('id' => $user_id, 'product_id' => $args['product_id']), get_permalink());
        $success = add_query_arg(array('action' => 'complete'), $base_url);
        $cancel = add_query_arg(array('action' => 'cancel'), $base_url);
        $product_post = get_post($args['product_id']);
        $duration = $this->get_subscription_duration($args['product_id']);
        $args['order_details']['_acp_order_coupon_id'] = MemberAccess_Coupons::get_product_coupon($args['product_id']);
        $initial_amount = $args['order_details']['_acp_order_price'] = AccessPress_Products::get_product_price($args['product_id'], $args['order_details']['_acp_order_coupon_id']);
        if ($duration) {
            $args['order_details']['_acp_order_trial_price'] = AccessPress_Products::get_product_trial_price($args['product_id'], $args['order_details']['_acp_order_coupon_id']);
            if ($args['order_details']['_acp_order_trial_price']) {
                $initial_amount = $args['order_details']['_acp_order_trial_price'];
            }
        }
        // create authorization token
        $auth_request = sprintf('&L_NAME0=%1$s&L_AMT0=%2$s&L_QTY0=1&AMT=%2$s&ReturnUrl=%3$s&CANCELURL=%4$s&CURRENCYCODE=USD&PAYMENTACTION=DoAuthorization', urlencode($product_post->post_name), urlencode(sprintf('%.2f', $initial_amount)), urlencode($success), urlencode($cancel));
        $profile_date = '';
        if ($duration) {
            $trial_duration = $this->_get_trial_duration($args['product_id']);
            $args['order_details']['_acp_order_renewal_time'] = $args['order_details']['_acp_order_time'] + ($trial_duration ? $trial_duration : $duration) * 86400;
            $profile_date = date('Y-m-d H:i:s', $args['order_details']['_acp_order_renewal_time']) . 'Z';
            $auth_request .= sprintf('&L_BILLINGTYPE0=RecurringPayments&L_PROFILESTARTDATE0=%s&L_BILLINGAGREEMENTDESCRIPTION0=%s&L_BILLINGPERIOD0=Day&L_BILLINGFREQUENCY0=%d&L_TOTALBILLINGCYCLES0=0&PAYMENTREQUEST_0_AMT=%s', $profile_date, urlencode($product_post->post_title), $duration, urlencode(sprintf('%.2f', $args['order_details']['_acp_order_price'])));
        }
        if (!($response = $this->_send_request('SetExpressCheckout', $auth_request))) {
            return $this->response;
        }
        // we have a token - update the user meta with the transaction info
        $sale_meta = $args['order_details'];
        $sale_meta['token'] = $response['TOKEN'];
        $sale_meta['profile_date'] = $profile_date;
        $accesspress_pp = array($args['product_id'] => $sale_meta);
        update_user_option($user_id, 'accesspress_pp', $accesspress_pp);
        // redirect the user to Paypal
        $url = $this->_customer_uri . urlencode($response['TOKEN']);
        //@todo: translation support
        ?>
Redirecting to Paypal. Click this link if not redirected automatically:
<a href="<?php 
        echo $url;
        ?>
">Proceed to Paypal</a>
<script type="text/javascript">
//<!--
window.location = '<?php 
        echo $url;
        ?>
';
//-->
</script>
<?php 
        return false;
    }
Пример #3
0
 /**
  * Handle the postback of the payment gateway form.
  *
  * @since 0.1.0
  */
 public function _process_order($args)
 {
     // create local user
     $user_id = $args['order_details']['_acp_order_member_id'];
     $memberaccess_cc_profile_id = isset($args['cc_profile_id']) ? $args['cc_profile_id'] : 0;
     $memberaccess_cc_payment_profile_id = isset($args['cc_payment_profile_id']) ? $args['cc_payment_profile_id'] : 0;
     if (empty($memberaccess_cc_profile_id) && is_user_logged_in()) {
         $memberaccess_cc_profile_id = get_user_option('memberaccess_cc_profile_id');
     }
     /** for initial payment attempts only */
     if (!$memberaccess_cc_profile_id) {
         if (is_user_logged_in() && empty($args['first-name']) && empty($args['last-name'])) {
             $user = get_user_by('id', $user_id);
             $args['first-name'] = $user->first_name;
             $args['last-name'] = $user->last_name;
             $args['email'] = $user->user_email;
         }
         // create member profile
         $customer_info = sprintf('<merchantCustomerId>%d</merchantCustomerId><description>%s</description><email>%s</email>', $user_id, trim($args['first-name'] . ' ' . $args['last-name']), $args['email']);
         if (!($response = $this->_send_request('createCustomerProfileRequest', '<profile>' . $customer_info . '</profile>'))) {
             return $this->response;
         }
         $this->customer_response = $response;
         $memberaccess_cc_profile_id = (string) $response->customerProfileId;
     }
     $customer = sprintf('<customerProfileId>%d</customerProfileId>', $memberaccess_cc_profile_id);
     /** for new subscriptions only */
     if (!$memberaccess_cc_payment_profile_id) {
         // profile created now send billing info
         $bill_to = sprintf('<billTo><firstName>%s</firstName><lastName>%s</lastName><zip>%s</zip><country>%s</country></billTo>', esc_html($args['first-name']), esc_html($args['last-name']), $args['card-postal'], $args['card-country']);
         $payment = sprintf('<payment><creditCard><cardNumber>%s</cardNumber><expirationDate>%04d-%02d</expirationDate><cardCode>%s</cardCode></creditCard></payment>', $args['card-number'], $args['card-year'], $args['card-month'], $args['card-security']);
         $profile = '<paymentProfile>' . $bill_to . $payment . '</paymentProfile>';
         if (!($response = $this->_send_request('createCustomerPaymentProfileRequest', $customer . $profile . $this->_gateway_mode))) {
             return $this->response;
         }
         $this->profile_response = $repsonse;
         $memberaccess_cc_payment_profile_id = (string) $response->customerPaymentProfileId;
     }
     // payment profile created now charge the account
     $product_post = get_post($args['product_id']);
     $args['order_details']['_acp_order_coupon_id'] = MemberAccess_Coupons::get_product_coupon($args['product_id']);
     $args['order_details']['_acp_order_price'] = AccessPress_Products::get_product_price($args['product_id'], $args['order_details']['_acp_order_coupon_id']);
     if (empty($args['order_details']['_acp_order_renewal_time'])) {
         $trial_amount = AccessPress_Products::get_product_trial_price($args['product_id'], $args['order_details']['_acp_order_coupon_id']);
         if ($trial_amount) {
             $amount = sprintf('<amount>%.2f</amount>', $trial_amount);
         }
         $duration = $trial_duration = $this->_get_trial_duration($args['product_id']);
     }
     if (empty($amount) || empty($duration)) {
         $amount = sprintf('<amount>%.2f</amount>', $args['order_details']['_acp_order_price']);
         $duration = $this->get_subscription_duration($args['product_id']);
     }
     $recurring = $duration ? 'true' : 'false';
     $args['order_details']['order_title'] = time() . '-' . $user_id;
     $product_description = $product_post->post_title . ' (' . $args['order_details']['order_title'] . ')';
     $payment_profile = sprintf('<customerPaymentProfileId>%d</customerPaymentProfileId><recurringBilling>%s</recurringBilling>', $memberaccess_cc_payment_profile_id, $recurring);
     $item = sprintf('<lineItems><itemId>%s</itemId><name>%s</name><description>%s</description><quantity>1</quantity><unitPrice>%.2f</unitPrice><taxable>false</taxable></lineItems>', $args['product_id'] . '-' . time(), substr($product_post->post_name, 0, 31), esc_html($product_description), !empty($trial_amount) && !empty($trial_duration) ? $trial_amount : $args['order_details']['_acp_order_price']);
     if (!$duration || $trial_amount) {
         $transaction = '<transaction><profileTransAuthCapture>' . $amount . $item . $customer . $payment_profile . '</profileTransAuthCapture></transaction>';
         if (!($response = $this->_send_request('createCustomerProfileTransactionRequest', $transaction))) {
             return $this->response;
         }
     }
     // we made it - update the user meta
     if (!is_user_logged_in()) {
         update_user_option($user_id, 'memberaccess_cc_profile_id', $memberaccess_cc_profile_id);
     }
     if ($duration) {
         $args['order_details']['_acp_order_renewal_time'] = (!empty($args['order_details']['_acp_order_renewal_time']) ? $args['order_details']['_acp_order_renewal_time'] : $args['order_details']['_acp_order_time']) + $duration * 86400;
         $args['order_details']['_acp_order_status'] = 'active';
         update_user_option($user_id, 'memberaccess_cc_payment_' . $args['product_id'], $memberaccess_cc_payment_profile_id);
         $number_payments = get_post_meta($args['product_id'], '_acp_product_number_payments', true);
         if ((int) $number_payments) {
             $args['order_details']['_acp_order_payments'] = $trial_amount ? array($args['order_details']['_acp_order_time'] => $args['order_details']['_acp_order_price']) : array();
         }
     }
     $direct_response = explode(',', $response->directResponse);
     $sale_meta = $args['order_details'];
     $sale_meta['_acp_order_anet_transaction_id'] = $direct_response[6];
     return $sale_meta;
 }
Пример #4
0
function accesspress_product_info_content($atts, $field)
{
    global $product_post;
    $atts = shortcode_atts(array('productid' => 0, 'format' => '', 'title' => '', 'target' => ''), $atts);
    if (!$atts['productid'] && isset($_REQUEST['product_id'])) {
        $atts['productid'] = (int) $_REQUEST['product_id'];
    }
    if (!$atts['productid'] && isset($_POST['accesspress-checkout']['product_id'])) {
        $atts['productid'] = (int) $_POST['accesspress-checkout']['product_id'];
    }
    if (!$atts['productid'] && isset($product_post->ID)) {
        $atts['productid'] = (int) $product_post->ID;
    }
    if (!memberaccess_is_valid_product($atts['productid'])) {
        return '';
    }
    if ($field == 'post_title') {
        if (!empty($product_post->post_title)) {
            return $product_post->post_title;
        }
        $product = get_post($atts['productid']);
        if (empty($product->post_title)) {
            return '';
        }
        return $product->post_title;
    }
    if ($field == 'purchase_link') {
        $url = accesspress_get_checkout_link($atts['productid']);
        if (!$url) {
            return '%s';
        }
        $target = $atts['target'] ? 'target="' . $atts['target'] . '"' : '';
        return sprintf('<a href="%s" title="%s" %s>', $url, $atts['title'], $target) . '%s</a>';
    }
    $coupon_id = MemberAccess_Coupons::get_product_coupon($atts['productid']);
    if ($field == '_acp_product_price') {
        $meta = AccessPress_Products::get_product_price($atts['productid'], $coupon_id);
    } elseif ($field == '_acp_product_trial_price') {
        $meta = AccessPress_Products::get_product_trial_price($atts['productid'], $coupon_id);
    } else {
        $meta = get_post_meta($atts['productid'], $field, true);
    }
    if (empty($meta)) {
        return '';
    }
    return $atts['format'] ? sprintf($atts['format'], $meta) : $meta;
}