/** * 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; }
/** * Complete a sale on the Paypal gateway * * method is called by the checkout form after a sale is validated * * @since 0.1.0 */ public function complete_sale($args) { // validate based on the confirmation form if (empty($args['product_id']) || empty($args['member']) || empty($args['key'])) { return false; } $meta = get_user_option('accesspress_pp', $args['member']); if (is_wp_error($meta) || empty($meta) || empty($meta[$args['product_id']])) { return new WP_Error('invalid', __('Invalid Transaction.', 'premise')); } $transaction = $meta[$args['product_id']]; if (empty($transaction['token']) || !wp_verify_nonce($args['key'], $transaction['token'])) { return new WP_Error('invalid-key', __('Invalid Transaction Key.', 'premise')); } $transaction['_acp_order_coupon_id'] = MemberAccess_Coupons::get_product_coupon($args['product_id']); $initial_amount = $transaction['_acp_order_price']; if ($transaction['profile_date'] && $transaction['_acp_order_trial_price']) { $initial_amount = $transaction['_acp_order_trial_price']; } // complete the transaction $product_post = get_post($args['product_id']); $transaction['order_title'] = time() . '-' . $args['member']; $product_description = $product_post->post_title . ' (' . $transaction['order_title'] . ')'; if (!$transaction['profile_date'] || $transaction['_acp_order_trial_price']) { $complete = sprintf('&TOKEN=%s&PAYERID=%s&PAYMENTREQUEST_0_AMT=%s&PAYMENTREQUEST_0_CURRENCYCODE=USD&PAYMENTREQUEST_0_PAYMENTACTION=Sale&PAYMENTREQUEST_0_DESC=%s', urlencode($transaction['token']), urlencode($transaction['payer_id']), urlencode(sprintf('%.2f', $initial_amount)), urlencode($product_description)); if (!($response = $this->_send_request('DoExpressCheckoutPayment', $complete))) { return $this->response; } } $transaction['_acp_order_paypal_transaction_id'] = isset($response['PAYMENTINFO_0_TRANSACTIONID']) ? $response['PAYMENTINFO_0_TRANSACTIONID'] : $transaction['token']; if ($transaction['profile_date']) { $duration = $this->get_subscription_duration($args['product_id']); $number_payments = (int) get_post_meta($args['product_id'], '_acp_product_number_payments', true); $complete = sprintf('&TOKEN=%s&PAYERID=%s&PAYMENTREQUEST_0_AMT=%s&PAYMENTREQUEST_0_CURRENCYCODE=USD&PAYMENTREQUEST_0_PAYMENTACTION=Sale&PROFILESTARTDATE=%s&DESC=%s&BILLINGPERIOD=Day&BILLINGFREQUENCY=%d&AMT=%3$s&TOTALBILLINGCYCLES=%d&L_PAYMENTREQUEST_0_ITEMCATEGORY0=Digital', urlencode($transaction['token']), urlencode($transaction['payer_id']), urlencode(sprintf('%.2f', $transaction['_acp_order_price'])), urlencode($transaction['profile_date']), urlencode($product_post->post_title), $duration, $trial_amount ? $number_payments : ($number_payments > 1 ? $number_payments - 1 : 0)); if (!($response = $this->_send_request('CreateRecurringPaymentsProfile', $complete))) { return $this->response; } if (!empty($response['PROFILEID'])) { update_user_option($args['member'], 'memberaccess_paypal_profile_' . $args['product_id'], $response['PROFILEID']); } } // cleanup & return data to allow transaction to be completed by checkout unset($transaction['token']); delete_user_option($args['member'], 'accesspress_pp'); return array('member' => $args['member'], 'order_details' => $transaction); }
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; }