From eadcc84d4a315fa995e0b37df06f7bcdb9a8ed57 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 6 Apr 2018 15:57:04 -0400 Subject: [PATCH] Updating db with payment fields for Authorize.net CIM CIM Authorize.net to save the credit card on authorize.net. --- classes/billingSupport.php | 123 ++++++++++++++---- classes/data/dataAccounts.php | 19 +++ index.php | 2 +- ...0.0.18.sql => create_database_V0.0.19.sql} | 5 +- setup/databaseScripts/dbVersions.php | 1 + .../update_database_V0.0.19.sql | 19 +++ 6 files changed, 142 insertions(+), 27 deletions(-) rename setup/databaseScripts/{create_database_V0.0.18.sql => create_database_V0.0.19.sql} (97%) create mode 100644 setup/databaseScripts/update_database_V0.0.19.sql diff --git a/classes/billingSupport.php b/classes/billingSupport.php index c98e70b..445211e 100644 --- a/classes/billingSupport.php +++ b/classes/billingSupport.php @@ -1057,12 +1057,20 @@ class GlmBillingSupport ); } - public function processPayment() + public function processPayment( $account_id, $payment_id ) { $errors = array(); // Get the selected credit card processor type $ccProcessor = $this->config['billing_settings']['proc_methods']; + // Other data fields that might be supplied + $transOpt = 0; + $customerProfileId = false; + $paymentProfileId = false; + $profileStatus = false; + $profileStatusText = false; + $checkCC = true; + // Setup the required account information for the selected payment processor switch ( $ccProcessor ) { @@ -1130,52 +1138,83 @@ class GlmBillingSupport // Add billing information to billing array $billing = array( + 'id' => false, 'fname' => $paymentData['billing_fname'], 'lname' => $paymentData['billing_lname'], 'addr1' => $paymentData['billing_addr1'], 'addr2' => $paymentData['billing_addr2'], 'city' => $paymentData['billing_city'], 'state' => $paymentData['billing_state'], + 'country' => '', 'zip' => $paymentData['billing_zip'], 'phone' => $paymentData['billing_phone'], 'email' => $paymentData['email'] ); $cardMatch = $this->config['credit_card_match']; - // Check all credit card input - if ( $cardData['cc_type'] && $cardData['cc_type'] > 0 - && $cardData['cc_name'] && $cardData['cc_name'] != '' - && $cardData['cc_numb'] && $cardData['cc_numb'] > 0 - && $cardData['cc_exp'] && $cardData['cc_exp'] != '' - && $cardData['cc_cvv'] && $cardData['cc_cvv'] > 0 - ) { - if ( !isset( $this->config['credit_card_match'][$cardData['cc_type']] ) || - !preg_match( $this->config['credit_card_match'][$cardData['cc_type']], $cardData['cc_numb'] ) + switch ( $ccProcessor ) { + case $this->config['proc_method_numb']['Authorize.net']: + // Add payment fields for Payment Profiles + if ( isset( $_REQUEST['use_saved_cc_profile'] ) + && trim($_REQUEST['use_saved_cc_profile'] ) != '' ) { - $errors[] = 'The credit card number you entered does not match the selected type of credit card.'; + $transOpt = 2; + $checkCC = false; + $customerProfileId = $regAccount['fieldData']['customer_profile_id']; + $paymentProfileId = $regAccount['fieldData']['payment_profile_id']; + + } elseif ( isset( $_REQUEST['cc_save'] ) + && trim( $_REQUEST['cc_save'] ) != '' + ) { + $transOpt = 1; } - } else { - $errors[] = 'You did not supply all required credit card information correctly.'; + // Convert card expiration date to required format + if ($transOpt < 2) { + $cardExp = explode('/', $cardData['cc_exp']); + $cardData['cc_exp'] = '20'.$cardExp[1].'-'.$cardExp[0]; + } + break; + } + + // If we need to check for proper credit card data + if ($checkCC) { + // Check all credit card input + if ( $cardData['cc_type'] && $cardData['cc_type'] > 0 + && $cardData['cc_name'] && $cardData['cc_name'] != '' + && $cardData['cc_numb'] && $cardData['cc_numb'] > 0 + && $cardData['cc_exp'] && $cardData['cc_exp'] != '' + && $cardData['cc_cvv'] && $cardData['cc_cvv'] > 0 + ) { + if ( !isset( $this->config['credit_card_match'][$cardData['cc_type']] ) || + !preg_match( $this->config['credit_card_match'][$cardData['cc_type']], $cardData['cc_numb'] ) + ) { + $errors[] = 'The credit card number you entered does not match the selected type of credit card.'; + } + + } else { + $errors[] = 'You did not supply all required credit card information correctly.'; + } } - if (count($errors) == 0) { + if ( count( $errors ) == 0 ) { // Determine the directory of the payment processor to load and instantiate it. if ( $ccProcessor && isset( $this->config['proc_dir'][$ccProcessor] ) ) { - require_once GLM_MEMBERS_BILLING_PLUGIN_LIB_PATH . '/paymentProcessors/' . $this->config['proc_dir'][$ccProcessor] . '/paymentGateway.php'; + require_once GLM_MEMBERS_PLUGIN_LIB_PATH . '/paymentProcessors/' . $this->config['proc_dir'][$ccProcessor] . '/paymentGateway.php'; $CcProcessor = new PaymentGateway( $account ); } // TODO: Need to update the $invoice_id to something better for billing. $payment = array( - 'name' => $this->config['settings']['company_name'], // Company Name - 'charge' => $amount, // Total charges - 'cctype' => $cardData['cc_type'], // Card Type - 'ccname' => $cardData['cc_name'], // Name on Card - 'ccnumb' => $cardData['cc_numb'], // Card Number - 'ccexp' => $cardData['cc_exp'], // Expiration Date - 'cccode' => $cardData['cc_cvv'], // CCV - security code - 'invoice' => 'billing-'//.$invoice_id // Invoice # is "reg-" plus cart ID + 'transOpt' => $transOpt, // 0 = charge card, 1 = charge and store, 2 = charge stored card + 'name' => $this->config['settings']['company_name'], // Company Name + 'charge' => $amount, // Total charges + 'cctype' => $cardData['cc_type'], // Card Type + 'ccname' => $cardData['cc_name'], // Name on Card + 'ccnumb' => $cardData['cc_numb'], // Card Number + 'ccexp' => $cardData['cc_exp'], // Expiration Date + 'cccode' => $cardData['cc_cvv'], // CCV - security code + 'invoice' => 'billing-' . $payment_id // Invoice # is "reg-" plus cart ID ); // Now try to run the card processor @@ -1183,6 +1222,39 @@ class GlmBillingSupport // If successful submission - say we're complete if ( is_array( $ccResult ) && isset( $ccResult['status'] ) && $ccResult['status'] == 1 ) { + + // Check if we need to store new card profile data + if ( $transOpt == 1 ) { + $customerProfileId = filter_var( $ccResult['customerProfileId'], FILTER_SANITIZE_NUMBER_INT ); + $paymentProfileId = filter_var( $ccResult['paymentProfileId'], FILTER_SANITIZE_NUMBER_INT ); + $profileStatus = filter_var( $ccResult['profileStatus'], FILTER_SANITIZE_NUMBER_INT ); + $profileStatusText = filter_var( $ccResult['profileStatusText'], FILTER_SANITIZE_STRING ); + + // If saving the credit card was successful + if ( $profileStatus ) { + + // Save the profile information in the submitting account + $updated = $this->wpdb->update( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX.'accounts', + array( + 'customer_profile_id' => $customerProfileId, + 'payment_profile_id' => $paymentProfileId, + 'payment_profile_card' => $cc_numb_store + ), + array( 'id' => $account_id ), + array( + '%s', + '%s', + '%s' + ) + ); + + } else { + $messages[] = "Sorry, we were unable to store your credit card data for future use, but we did try."; + } + + } + $result = array( 'status' => 1, 'errors' => array(), @@ -1213,7 +1285,7 @@ class GlmBillingSupport */ public function processMemberRenewal( $account_id, $invoice_id, $amount, $employees = array() ) { - $result = $this->processPayment(); + $result = $this->processPayment( $account_id, $invoice_id ); if ( $result['status'] === 1 ) { @@ -1264,7 +1336,8 @@ class GlmBillingSupport public function makePayment( $account_id, $invoices, $amount ) { - $result = $this->processPayment(); + $payment_id = implode( '-', $invoices ); + $result = $this->processPayment( $account_id, $payment_id ); if ( $result['status'] === 1 ) { // Need to record the payment. diff --git a/classes/data/dataAccounts.php b/classes/data/dataAccounts.php index e269f12..7d86f73 100644 --- a/classes/data/dataAccounts.php +++ b/classes/data/dataAccounts.php @@ -240,6 +240,25 @@ class GlmDataAccounts extends GlmDataAbstract 'required' => false, ), + // Customer Profile Id + 'customer_profile_id' => array( + 'field' => 'customer_profile_id', + 'type' => 'text', + 'use' => 'a', + ), + // Payment Profile Id + 'payment_profile_id' => array( + 'field' => 'payment_profile_id', + 'type' => 'text', + 'use' => 'a', + ), + // Payment Profile Card + 'payment_profile_card' => array( + 'field' => 'payment_profile_card', + 'type' => 'text', + 'use' => 'a', + ), + ); diff --git a/index.php b/index.php index 3cb10ea..83475b4 100644 --- a/index.php +++ b/index.php @@ -38,7 +38,7 @@ * version from this plugin. */ define('GLM_MEMBERS_BILLING_PLUGIN_VERSION', '0.0.1'); -define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.18'); +define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.19'); // This is the minimum version of the GLM Members DB plugin require for this plugin. define('GLM_MEMBERS_BILLING_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.8.0'); diff --git a/setup/databaseScripts/create_database_V0.0.18.sql b/setup/databaseScripts/create_database_V0.0.19.sql similarity index 97% rename from setup/databaseScripts/create_database_V0.0.18.sql rename to setup/databaseScripts/create_database_V0.0.19.sql index 15c38ee..ba08d4d 100644 --- a/setup/databaseScripts/create_database_V0.0.18.sql +++ b/setup/databaseScripts/create_database_V0.0.19.sql @@ -1,6 +1,6 @@ -- Gaslight Media Billing Module -- File Created: 11/08/2017 --- Database Version: 0.0.15 +-- Database Version: 0.0.19 -- Database Creation Script -- -- To permit each query below to be executed separately, @@ -26,6 +26,9 @@ CREATE TABLE {prefix}accounts ( anniversary_date DATE NOT NULL, -- anniversary date - used for main invoice generation renewal_date DATE NULL, -- renewal date of account payment_data TEXT NULL, -- stored payment data + customer_profile_id TINYTEXT NULL, -- Customer Profile Id (Authorize.net) + payment_profile_id TINYTEXT NULL, -- Payment Profile Id (Authorize.net) + payment_profile_card TINYTEXT NULL, -- Payment Profile Card (Authorize.net) email TINYTEXT NULL, -- billing email PRIMARY KEY (id), INDEX(ref_dest), diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php index 729b630..c12c896 100644 --- a/setup/databaseScripts/dbVersions.php +++ b/setup/databaseScripts/dbVersions.php @@ -32,5 +32,6 @@ $glmMembersBillingDbVersions = array( '0.0.16' => array('version' => '0.0.16', 'tables' => 14), '0.0.17' => array('version' => '0.0.17', 'tables' => 14), '0.0.18' => array('version' => '0.0.18', 'tables' => 14), + '0.0.19' => array('version' => '0.0.19', 'tables' => 14), ); diff --git a/setup/databaseScripts/update_database_V0.0.19.sql b/setup/databaseScripts/update_database_V0.0.19.sql new file mode 100644 index 0000000..4079815 --- /dev/null +++ b/setup/databaseScripts/update_database_V0.0.19.sql @@ -0,0 +1,19 @@ +-- Gaslight Media Billing Database +-- File Created: 04/06/2018 +-- Database Version: 0.0.19 +-- +-- To permit each query below to be executed separately, +-- all queries must be separated by a line with four dashes + +-- Add customer profile id +ALTER TABLE {prefix}accounts ADD customer_profile_id TINYTEXT NULL; -- Customer Profile Id (Authorize.net) + +---- + +-- Add payment profile id +ALTER TABLE {prefix}accounts ADD payment_profile_id TINYTEXT NULL; -- Payment Profile Id (Authorize.net) + +---- + +-- Add payment profile id +ALTER TABLE {prefix}accounts ADD payment_profile_card TINYTEXT NULL; -- Payment Profile Card (Authorize.net) -- 2.17.1