Skip to content

Commit

Permalink
Merge pull request #57 from angelleye/PFWMA-132
Browse files Browse the repository at this point in the history
Payment Load Balancer, PFWMA-132
  • Loading branch information
deepakmaurya authored Jun 7, 2020
2 parents c67db13 + 3c1af73 commit 3cee71c
Show file tree
Hide file tree
Showing 12 changed files with 1,382 additions and 325 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ public function angelleye_get_account_for_ec_parallel_payments($gateways, $gatew
array(
'key' => 'woocommerce_paypal_express_enable',
'value' => 'on',
'compare' => 'LIKE'
'compare' => '='
),
array(
'key' => 'woocommerce_paypal_express_testmode',
'value' => ($gateways->testmode == true) ? 'on' : '',
'compare' => 'LIKE'
'compare' => '='
),
array(
'key' => 'woocommerce_priority',
Expand Down Expand Up @@ -518,7 +518,12 @@ public function angelleye_paypal_for_woocommerce_multi_account_api_paypal_expres
}
$payment_action = array('set_express_checkout', 'get_express_checkout_details', 'do_express_checkout_payment');
if (!empty($_GET['pp_action']) && ( in_array($_GET['pp_action'], $payment_action))) {
return $this->angelleye_get_account_for_ec_parallel_payments($gateways, $gateway_setting, $order_id, $request);
$angelleye_payment_load_balancer = get_option('angelleye_payment_load_balancer', '');
if($angelleye_payment_load_balancer != '') {
return $this->angelleye_get_account_for_ec_payment_load_balancer($gateways, $gateway_setting, $order_id, $request);
} else {
return $this->angelleye_get_account_for_ec_parallel_payments($gateways, $gateway_setting, $order_id, $request);
}
}
return $request;
}
Expand All @@ -533,12 +538,12 @@ public function angelleye_get_multi_account_details_by_api_user_name($gateway_se
array(
'key' => 'woocommerce_paypal_express_sandbox_api_username',
'value' => $_multi_account_api_username,
'compare' => 'LIKE'
'compare' => '='
),
array(
'key' => 'woocommerce_paypal_express_api_username',
'value' => $_multi_account_api_username,
'compare' => 'LIKE'
'compare' => '='
)
)
);
Expand Down Expand Up @@ -1244,9 +1249,14 @@ public function own_angelleye_express_checkout_order_data($paypal_response, $ord
if (empty($ec_parallel_data_map)) {
return false;
}
for ($payment = 1; $payment <= 10; $payment++) {
for ($payment = 0; $payment <= 10; $payment++) {
if (!empty($paypal_response['PAYMENTINFO_' . $payment . '_TRANSACTIONID'])) {
$order->add_order_note(sprintf(__('PayPal Express payment Transaction ID: %s', 'paypal-for-woocommerce-multi-account-management'), isset($paypal_response['PAYMENTINFO_' . $payment . '_TRANSACTIONID']) ? $paypal_response['PAYMENTINFO_' . $payment . '_TRANSACTIONID'] : ''));
} elseif(!empty($paypal_response['PAYMENTINFO_' . $payment . '_ERRORCODE'])) {
$long_message = !empty($paypal_response['PAYMENTINFO_' . $payment . '_LONGMESSAGE']) ? $paypal_response['PAYMENTINFO_' . $payment . '_LONGMESSAGE'] : '';
if( !empty($long_message) ) {
$order->add_order_note($long_message);
}
} else {
break;
}
Expand All @@ -1258,8 +1268,12 @@ public function own_angelleye_express_checkout_order_data($paypal_response, $ord
$PAYMENTREQUESTID_array = $paypal_response['PAYMENTINFO_' . $transaction_map . '_PAYMENTREQUESTID'];
$request_order_item_id = explode('-', $PAYMENTREQUESTID_array);
if( !empty($request_order_item_id[0]) && $ec_parallel_data['order_item_id'] == $request_order_item_id[0]) {
$PAYMENTREQUESTID_array = $ec_parallel_data_map[$ec_parallel_data['product_id']]['transaction_id'] = $paypal_response['PAYMENTINFO_' . $transaction_map . '_TRANSACTIONID'];
wc_update_order_item_meta($ec_parallel_data['order_item_id'], '_transaction_id', $paypal_response['PAYMENTINFO_' . $transaction_map . '_TRANSACTIONID']);
if( !empty($paypal_response['PAYMENTINFO_' . $transaction_map . '_TRANSACTIONID'])) {
$ec_parallel_data_map[$ec_parallel_data['product_id']]['transaction_id'] = $paypal_response['PAYMENTINFO_' . $transaction_map . '_TRANSACTIONID'];
wc_update_order_item_meta($ec_parallel_data['order_item_id'], '_transaction_id', $paypal_response['PAYMENTINFO_' . $transaction_map . '_TRANSACTIONID']);
} elseif(!empty($paypal_response['PAYMENTINFO_' . $payment . '_ERRORCODE'])) {
wc_update_order_item_meta($ec_parallel_data['order_item_id'], 'Payment Status', __('Not Paid', 'paypal-for-woocommerce-multi-account-management'));
}
}
}
}
Expand Down Expand Up @@ -1313,10 +1327,12 @@ public function own_angelleye_is_express_checkout_parallel_payment_handle($bool,
$refund_error_message_pre = __('We can not refund this order as the Express Checkout API keys are missing! Please go to multi-account setup and add API key to process the refund', 'paypal-for-woocommerce-multi-account-management');
$refund_error_message_after = array();
$angelleye_multi_account_ec_parallel_data_map = get_post_meta($order_id, '_angelleye_multi_account_ec_parallel_data_map', true);
foreach ($angelleye_multi_account_ec_parallel_data_map as $key => $value) {
if (!empty($value['product_id']) && isset($value['is_api_set']) && $value['is_api_set'] == false) {
$product = wc_get_product($value['product_id']);
$refund_error_message_after[] = $product->get_title();
if( !empty($angelleye_multi_account_ec_parallel_data_map) ) {
foreach ($angelleye_multi_account_ec_parallel_data_map as $key => $value) {
if (!empty($value['product_id']) && isset($value['is_api_set']) && $value['is_api_set'] == false) {
$product = wc_get_product($value['product_id']);
$refund_error_message_after[] = $product->get_title();
}
}
}
if (!empty($refund_error_message_after)) {
Expand Down Expand Up @@ -1538,4 +1554,104 @@ public function own_angelleye_multi_account_need_shipping($bool, $order_id = '',
return $bool;

}

public function angelleye_get_account_for_ec_payment_load_balancer($gateways, $gateway_setting, $order_id, $request) {
if (!isset($gateways->testmode)) {
return;
}
$found_account = false;
$found_email = '';
if ($gateways->testmode == true) {
$option_key = 'angelleye_multi_ec_payment_load_balancer_sandbox';
$session_key = 'angelleye_sandbox_payment_load_balancer_ec_email';
$session_key_account = 'angelleye_sandbox_payment_load_balancer_ec_account';
} else {
$option_key = 'angelleye_multi_ec_payment_load_balancer';
$session_key = 'angelleye_payment_load_balancer_ec_email';
$session_key_account = 'angelleye_payment_load_balancer_ec_account';
}
$found_email = WC()->session->get( $session_key );
if( empty($found_email) ) {
$found_email = '';
$express_checkout_accounts = get_option($option_key);
if(!empty($express_checkout_accounts)) {
foreach ($express_checkout_accounts as $key => $account) {
if(empty($account['is_used'])) {
if ( $key != 'default' && false === get_post_status( $key ) ) {
unset($express_checkout_accounts[$key]);
} else {
$found_email = $account['email'];
WC()->session->set($session_key, $account['email']);
$account['is_used'] = 'yes';
$express_checkout_accounts[$key] = $account;
WC()->session->set($session_key_account, $account);
update_option($option_key, $express_checkout_accounts);
$found_account = true;
break;
}
}
}
if($found_account == false) {
foreach ($express_checkout_accounts as $key => $account) {
$account['is_used'] = '';
$express_checkout_accounts[$key] = $account;
}
foreach ($express_checkout_accounts as $key => $account) {
if ( $key != 'default' && false === get_post_status( $key ) ) {
unset($express_checkout_accounts[$key]);
} else {
$found_email = $account['email'];
WC()->session->set($session_key, $account['email']);
$account['is_used'] = 'yes';
$express_checkout_accounts[$key] = $account;
WC()->session->set($session_key_account, $account);
update_option($option_key, $express_checkout_accounts);
$found_account = true;
break;
}
}
}
}
}

if( !empty($request)) {
if($found_email != 'default') {
$request['Payments'][0]['sellerpaypalaccountid'] = $found_email;
if( !empty($order_id)) {
$angelleye_payment_load_balancer_account = WC()->session->get( $session_key_account );
update_post_meta($order_id, '_angelleye_payment_load_balancer_account', $angelleye_payment_load_balancer_account);
}
}
}
return $request;
}

public function own_angelleye_is_payment_load_balancer_not_used($bool, $order_id) {
$angelleye_payment_load_balancer_account = get_post_meta($order_id, '_angelleye_payment_load_balancer_account', true);
if (!empty($angelleye_payment_load_balancer_account)) {
return false;
}
return $bool;
}

public function own_angelleye_is_express_checkout_payment_load_balancer_handle($bool, $order_id, $gateway) {
try {
$processed_transaction_id = array();
$refund_error_message_pre = __('We can not refund this order as the Express Checkout API keys are missing! Please go to multi-account setup and add API key to process the refund', 'paypal-for-woocommerce-multi-account-management');
$angelleye_payment_load_balancer_account = get_post_meta($order_id, '_angelleye_payment_load_balancer_account', true);
if (!empty($angelleye_payment_load_balancer_account)) {
if( !empty($angelleye_payment_load_balancer_account['is_api_set']) && $angelleye_payment_load_balancer_account['is_api_set'] === true) {
$_transaction_id = get_post_meta($order_id, '_transaction_id', true);
$angelleye_payment_load_balancer_account['transaction_id'] = $_transaction_id;
$this->angelleye_express_checkout_load_paypal($angelleye_payment_load_balancer_account, $gateway, $order_id);
return true;
} else {
return new WP_Error('invalid_refund', $refund_error_message_pre);
}
}
return $bool;
} catch (Exception $ex) {

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ public function angelleye_get_multi_account_by_order_total_latest($gateways, $ga
array(
'key' => 'woocommerce_paypal_pro_payflow_enable',
'value' => 'on',
'compare' => 'LIKE'
'compare' => '='
),
array(
'key' => 'woocommerce_paypal_pro_payflow_testmode',
'value' => ($gateways->testmode == true) ? 'on' : '',
'compare' => 'LIKE'
'compare' => '='
),
array(
'key' => 'woocommerce_priority',
Expand Down Expand Up @@ -346,7 +346,7 @@ public function angelleye_get_multi_account_by_order_total_latest($gateways, $ga
} elseif (count($this->final_associate_account) == 0) {
return $this->final_associate_account;
} else {
return $this->angelleye_get_closest_amount($this->final_associate_account, $order_total);
return angelleye_get_closest_amount($this->final_associate_account, $order_total);
}
}
}
Expand All @@ -371,12 +371,12 @@ public function angelleye_get_multi_account_by_order_total($gateways, $gateway_s
array(
'key' => 'woocommerce_paypal_pro_payflow_enable',
'value' => 'on',
'compare' => 'LIKE'
'compare' => '='
),
array(
'key' => 'woocommerce_paypal_pro_payflow_testmode',
'value' => ($gateways->testmode == true) ? 'on' : '',
'compare' => 'LIKE'
'compare' => '='
)
)
);
Expand All @@ -388,12 +388,12 @@ public function angelleye_get_multi_account_by_order_total($gateways, $gateway_s
array(
'key' => 'woocommerce_paypal_express_enable',
'value' => 'on',
'compare' => 'LIKE'
'compare' => '='
),
array(
'key' => 'woocommerce_paypal_express_testmode',
'value' => ($gateways->testmode == true) ? 'on' : '',
'compare' => 'LIKE'
'compare' => '='
)
)
);
Expand Down Expand Up @@ -443,7 +443,7 @@ public function angelleye_get_multi_account_by_order_total($gateways, $gateway_s
} elseif (count($this->final_associate_account) == 0) {
return $this->final_associate_account;
} else {
return $this->angelleye_get_closest_amount($this->final_associate_account, $order_total);
return angelleye_get_closest_amount($this->final_associate_account, $order_total);
}
}
}
Expand All @@ -466,7 +466,10 @@ public function angelleye_paypal_for_woocommerce_multi_account_api_paypal_payflo
return false;
}
}
if ($this->is_angelleye_multi_account_used($order_id)) {
$angelleye_payment_load_balancer = get_option('angelleye_payment_load_balancer', '');
if(!empty($angelleye_payment_load_balancer)) {
$microprocessing_value = $this->angelleye_get_account_for_payflow_payment_load_balancer($gateways, $gateway_setting, $order_id);
} elseif ($this->is_angelleye_multi_account_used($order_id)) {
$_multi_account_api_username = $this->angelleye_get_multi_account_api_user_name($order_id);
$microprocessing_value = $this->angelleye_get_multi_account_details_by_api_user_name($gateway_setting, $_multi_account_api_username);
} elseif (!empty($gateway_setting->id) && $gateway_setting->id == 'paypal_pro_payflow') {
Expand Down Expand Up @@ -525,12 +528,12 @@ public function angelleye_get_multi_account_details_by_api_user_name($gateway_se
array(
'key' => 'woocommerce_paypal_pro_payflow_sandbox_api_paypal_user',
'value' => $_multi_account_api_username,
'compare' => 'LIKE'
'compare' => '='
),
array(
'key' => 'woocommerce_paypal_pro_payflow_api_paypal_user',
'value' => $_multi_account_api_username,
'compare' => 'LIKE'
'compare' => '='
)
)
);
Expand Down Expand Up @@ -597,4 +600,71 @@ public function card_type_from_account_number($account_number) {
}
return null;
}

public function angelleye_get_account_for_payflow_payment_load_balancer($gateways, $gateway_setting, $order_id) {
if (!isset($gateways->testmode)) {
return;
}
if ($gateways->testmode == true) {
$option_key = 'angelleye_multi_payflow_payment_load_balancer_sandbox';
} else {
$option_key = 'angelleye_multi_payflow_payment_load_balancer';
}
$is_account_found = false;
$used_account = get_post_meta($order_id, '_multi_account_api_username_load_balancer', true);
if( $used_account == 'default' ) {
return;
}
if( empty($used_account)) {
$payflow_accounts = get_option($option_key);
if(!empty($payflow_accounts)) {
foreach ($payflow_accounts as $key => $account) {
if(empty($account['is_used'])) {
if ( $key != 'default' && false === get_post_status( $key ) ) {
unset($payflow_accounts[$key]);
} else {
$account['is_used'] = 'yes';
$is_account_found = true;
$payflow_accounts[$key] = $account;
$used_account = $account['multi_account_id'];
update_post_meta($order_id, '_multi_account_api_username_load_balancer', $used_account);
update_option($option_key, $payflow_accounts);
break;
}
}
}
if($is_account_found == false) {
foreach ($payflow_accounts as $key => $account) {
$account['is_used'] = '';
$payflow_accounts[$key] = $account;
}
foreach ($payflow_accounts as $key => $account) {
if ( $key != 'default' && false === get_post_status( $key ) ) {
unset($payflow_accounts[$key]);
} else {
$account['is_used'] = 'yes';
$payflow_accounts[$key] = $account;
$used_account = $account['multi_account_id'];
update_post_meta($order_id, '_multi_account_api_username_load_balancer', $used_account);
update_option($option_key, $payflow_accounts);
break;
}
}
}
}
}
if($used_account == 'default') {
return;
}
if(!empty($used_account)) {
$post_meta = get_post_meta($used_account);
if( !empty($post_meta) ) {
$microprocessing_value = array();
foreach ($post_meta as $key => $value) {
$microprocessing_value[$key] = isset($value[0]) ? $value[0] : '';
}
return $microprocessing_value;
}
}
}
}
Loading

0 comments on commit 3cee71c

Please sign in to comment.