Skip to content

Commit 75f3047

Browse files
authored
Merge pull request #2132 from angelleye/FEAT_BN_CODE
Added the BN code & Resolved the Authorization flow with PayPal Pro payment gateway
2 parents a8e8d48 + de509f8 commit 75f3047

File tree

7 files changed

+100
-9
lines changed

7 files changed

+100
-9
lines changed

angelleye-includes/angelleye-utility.php

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,13 @@ public function angelleye_get_transactionDetails($transaction_id) {
14311431
}
14321432
$this->add_ec_angelleye_paypal_php_library();
14331433

1434+
// Avoid fatal errors when gateway context could not be resolved and
1435+
// PayPal client was not initialized.
1436+
if (!is_object($this->paypal)) {
1437+
$this->ec_add_log('GetTransactionDetails skipped: PayPal client not initialized.', 'error');
1438+
return false;
1439+
}
1440+
14341441
if ($this->payment_method != 'paypal_pro_payflow') {
14351442
$GTDFields = array(
14361443
'transactionid' => $transaction_id
@@ -1584,14 +1591,54 @@ public function angelleye_set_payment_method() {
15841591

15851592
public function angelleye_set_payment_method_using_transaction_id($transaction) {
15861593
if (empty($this->payment_method) || $this->payment_method == false) {
1594+
// Primary path: resolve paypal_transaction post by transaction ID.
1595+
$transaction_post_id = $this->get_post_by_title($transaction);
1596+
if (!empty($transaction_post_id)) {
1597+
$transaction_post = get_post($transaction_post_id);
1598+
if (!empty($transaction_post) && !empty($transaction_post->post_parent)) {
1599+
$order = wc_get_order($transaction_post->post_parent);
1600+
if (!empty($order)) {
1601+
$this->payment_method = $order->get_payment_method();
1602+
$this->order_id = $order->get_id();
1603+
return;
1604+
}
1605+
}
1606+
1607+
$order_id = get_post_meta($transaction_post_id, 'order_id', true);
1608+
if (!empty($order_id)) {
1609+
$order = wc_get_order($order_id);
1610+
if (!empty($order)) {
1611+
$this->payment_method = $order->get_payment_method();
1612+
$this->order_id = $order->get_id();
1613+
return;
1614+
}
1615+
}
1616+
}
1617+
1618+
// Fallback for legacy/meta-only mappings.
15871619
global $wpdb;
15881620
$results = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_value = %s ORDER BY meta_id", $transaction));
15891621
if (!empty($results[0]->post_id)) {
1590-
$order = wc_get_order($results[0]->post_id);
1591-
if (empty($order)) {
1592-
return false;
1622+
$candidate_post_id = (int) $results[0]->post_id;
1623+
$candidate_post = get_post($candidate_post_id);
1624+
$order_id = 0;
1625+
1626+
if (!empty($candidate_post) && 'paypal_transaction' === $candidate_post->post_type) {
1627+
$order_id = (int) $candidate_post->post_parent;
1628+
if (empty($order_id)) {
1629+
$order_id = (int) get_post_meta($candidate_post_id, 'order_id', true);
1630+
}
1631+
} else {
1632+
$order_id = $candidate_post_id;
1633+
}
1634+
1635+
if (!empty($order_id)) {
1636+
$order = wc_get_order($order_id);
1637+
if (!empty($order)) {
1638+
$this->payment_method = $order->get_payment_method();
1639+
$this->order_id = $order->get_id();
1640+
}
15931641
}
1594-
$this->payment_method = $order->get_payment_method();
15951642
}
15961643
}
15971644
}
@@ -2107,6 +2154,7 @@ public function angelleye_wc_braintree_docapture($order) {
21072154
$request_data['paymentMethodToken'] = $transaction_id;
21082155
$request_data['amount'] = $AMT;
21092156
$request_data['options'] = array('submitForSettlement' => true);
2157+
$request_data['channel'] = PAYPAL_PARTNER_ATTRIBUTION_ID;
21102158
$result = $gateway_obj->pfw_braintree_do_capture($order, $request_data);
21112159
if ($result != false) {
21122160
$maybe_settled_later = array(

angelleye-includes/paypal-rest-api-utility.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,13 @@ public function set_payment() {
367367
public function getAuth() {
368368
$this->mode = $this->testmode == true ? 'SANDBOX' : 'LIVE';
369369
$auth = new ApiContext(new OAuthTokenCredential($this->rest_client_id, $this->rest_secret_id));
370-
$auth->setConfig(array('mode' => $this->mode, 'log.LogEnabled' => $this->debug, 'log.LogLevel' => ($this->mode == 'SANDBOX') ? 'DEBUG' : 'INFO', 'log.FileName' => angelleye_get_log_path('paypal_credit_card_rest')));
370+
$config = [
371+
'mode' => $this->mode, 'log.LogEnabled' => $this->debug, 'log.LogLevel' => ($this->mode == 'SANDBOX') ? 'DEBUG' : 'INFO', 'log.FileName' => angelleye_get_log_path('paypal_credit_card_rest')
372+
];
373+
if (defined('PAYPAL_PARTNER_ATTRIBUTION_ID') && !empty(PAYPAL_PARTNER_ATTRIBUTION_ID)) {
374+
$config['http.headers.PayPal-Partner-Attribution-Id'] = PAYPAL_PARTNER_ATTRIBUTION_ID;
375+
}
376+
$auth->setConfig($config);
371377
return $auth;
372378
}
373379

classes/lib/angelleye/paypal-php-library/includes/paypal.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function __construct($DataArray)
7676

7777
$this->APIVersion = isset($DataArray['APIVersion']) ? $DataArray['APIVersion'] : '124.0';
7878
$this->APIMode = isset($DataArray['APIMode']) ? $DataArray['APIMode'] : 'Signature';
79-
$this->APIButtonSource = '';
79+
$this->APIButtonSource = defined( 'PAYPAL_PARTNER_ATTRIBUTION_ID' ) ? PAYPAL_PARTNER_ATTRIBUTION_ID : '';
8080
$this->PathToCertKeyPEM = '/path/to/cert/pem.txt';
8181
$this->SSL = $_SERVER['SERVER_PORT'] == '443' ? true : false;
8282
$this->APISubject = isset($DataArray['APISubject']) ? $DataArray['APISubject'] : '';
@@ -107,7 +107,7 @@ function __construct($DataArray)
107107
$this->APIUsername = urlencode($this->APIUsername);
108108

109109
// Create the NVP credentials string which is required in all calls.
110-
$this->NVPCredentials = 'USER=' . $this->APIUsername . '&PWD=' . $this->APIPassword . '&VERSION=' . $this->APIVersion ;
110+
$this->NVPCredentials = 'USER=' . $this->APIUsername . '&PWD=' . $this->APIPassword . '&VERSION=' . $this->APIVersion . (!empty($this->APIButtonSource) ? '&BUTTONSOURCE=' . $this->APIButtonSource : '');
111111
$this->NVPCredentials .= $this->APISubject != '' ? '&SUBJECT=' . $this->APISubject : '';
112112
$this->NVPCredentials .= $this->APIMode == 'Signature' ? '&SIGNATURE=' . $this->APISignature : '';
113113

classes/wc-gateway-braintree-angelleye.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,9 @@ public function angelleye_do_payment($order) {
14111411
}
14121412
$request_data['orderId'] = $order->get_order_number();
14131413
$request_data['options'] = $this->get_braintree_options();
1414+
if (defined('PAYPAL_PARTNER_ATTRIBUTION_ID') && !empty(PAYPAL_PARTNER_ATTRIBUTION_ID)) {
1415+
$request_data['channel'] = PAYPAL_PARTNER_ATTRIBUTION_ID;
1416+
}
14141417
if (!empty($this->softdescriptor)) {
14151418
$request_data['descriptor'] = array('name' => $this->softdescriptor);
14161419
}
@@ -1427,7 +1430,7 @@ public function angelleye_do_payment($order) {
14271430
} else {
14281431
$log['paymentMethodNonce'] = '*********************';
14291432
}
1430-
$this->add_log('Transaction::sale() Reuest Data ' . print_r($log, true));
1433+
$this->add_log('Transaction::sale() Reuest Data ' . print_r($this->mask_sensitive_data($log), true));
14311434
}
14321435
try {
14331436
$this->response = $this->braintree_gateway->transaction()->sale(apply_filters('angelleye_woocommerce_braintree_sale_request_args', $request_data));
@@ -2337,6 +2340,22 @@ public function is_subscription($order_id) {
23372340
return ( function_exists('wcs_order_contains_subscription') && ( wcs_order_contains_subscription($order_id) || wcs_is_subscription($order_id) || wcs_order_contains_renewal($order_id) ) );
23382341
}
23392342

2343+
private function mask_sensitive_data($log_request_data) {
2344+
$sensitive_keys = array(
2345+
'paymentMethodToken',
2346+
'paymentMethodNonce',
2347+
'cvv',
2348+
'cardNumber',
2349+
'creditCard'
2350+
);
2351+
foreach ($sensitive_keys as $sensitive_key) {
2352+
if (isset($log_request_data[$sensitive_key])) {
2353+
$log_request_data[$sensitive_key] = '******';
2354+
}
2355+
}
2356+
return $log_request_data;
2357+
}
2358+
23402359
public function process_subscription_payment($order, $amount, $payment_token = null) {
23412360
$this->angelleye_reload_gateway_credentials_for_woo_subscription_renewal_order($order);
23422361
$order_id = $order->get_id();
@@ -2396,9 +2415,13 @@ public function process_subscription_payment($order, $amount, $payment_token = n
23962415
}
23972416
$request_data['orderId'] = $order->get_order_number();
23982417
$request_data['options'] = $this->get_braintree_options();
2418+
if (defined('PAYPAL_PARTNER_ATTRIBUTION_ID') && !empty(PAYPAL_PARTNER_ATTRIBUTION_ID)) {
2419+
$request_data['channel'] = PAYPAL_PARTNER_ATTRIBUTION_ID;
2420+
}
23992421
if ($this->debug) {
24002422
$this->add_log('Begin Transaction::sale() request');
24012423
$this->add_log('Order: ' . print_r($order->get_order_number(), true));
2424+
$this->add_log('Request Data 2: ' . print_r($this->mask_sensitive_data($request_data), true));
24022425
}
24032426
try {
24042427
$this->response = $this->braintree_gateway->transaction()->sale($request_data);
@@ -3014,6 +3037,7 @@ public function pfw_braintree_do_capture($order, $request_data = array()) {
30143037
if ($this->debug) {
30153038
$this->add_log('Begin Transaction::sale() request');
30163039
$this->add_log('Order: ' . print_r($order->get_order_number(), true));
3040+
$this->add_log('Request Data 3: ' . print_r($request_data, true));
30173041
}
30183042
try {
30193043
$this->response = $this->braintree_gateway->transaction()->sale($request_data);

classes/wc-gateway-paypal-advanced-angelleye.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ public function inquiry_transaction($order, $order_id) {
203203
'TRXTYPE' => 'I'
204204
);
205205

206+
if (defined('PAYPAL_PARTNER_ATTRIBUTION_ID') && !empty(PAYPAL_PARTNER_ATTRIBUTION_ID)) {
207+
$paypal_args['BUTTONSOURCE'] = PAYPAL_PARTNER_ATTRIBUTION_ID;
208+
}
206209
$postData = ''; //stores the post data string
207210
foreach ($paypal_args as $key => $val) {
208211
$postData .= '&' . $key . '=' . $val;
@@ -510,6 +513,9 @@ public function get_secure_token($order) {
510513
'LABELTEXTCOLOR' => ltrim($this->settings['label_textcolor'], '#'),
511514
'MERCHDESCR' => $this->softdescriptor
512515
);
516+
if (defined('PAYPAL_PARTNER_ATTRIBUTION_ID') && !empty(PAYPAL_PARTNER_ATTRIBUTION_ID)) {
517+
$paypal_args['BUTTONSOURCE'] = PAYPAL_PARTNER_ATTRIBUTION_ID;
518+
}
513519
if (empty($shipping_state)) {
514520
$paypal_args['SHIPTOSTATE[' . strlen($shipping_city) . ']'] = $shipping_city;
515521
} else {
@@ -1200,6 +1206,9 @@ public function create_reference_transaction($token, $order) {
12001206
'SHIPTOCOUNTRY[' . strlen($shipping_country) . ']' => $shipping_country,
12011207
'MERCHDESCR' => $this->softdescriptor
12021208
);
1209+
if (defined('PAYPAL_PARTNER_ATTRIBUTION_ID') && !empty(PAYPAL_PARTNER_ATTRIBUTION_ID)) {
1210+
$paypal_args['BUTTONSOURCE'] = PAYPAL_PARTNER_ATTRIBUTION_ID;
1211+
}
12031212
if ($this->is_subscription($order_id)) {
12041213
$paypal_args['origid'] = $order->get_meta( '_payment_tokens_id', true);
12051214
}

classes/wc-gateway-paypal-pro-angelleye.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,8 @@ function do_payment($order, $card_number, $card_type, $card_exp_month, $card_exp
14121412
AngellEYE_Utility::angelleye_add_order_meta($order_id, $payment_order_meta);
14131413
AngellEYE_Utility::angelleye_paypal_for_woocommerce_add_paypal_transaction($PayPalResult, $order, $this->payment_action);
14141414
$angelleye_utility = new AngellEYE_Utility(null, null);
1415+
$angelleye_utility->payment_method = 'paypal_pro';
1416+
$angelleye_utility->order_id = $order_id;
14151417
$angelleye_utility->angelleye_get_transactionDetails($PayPalResult['TRANSACTIONID']);
14161418
$order->add_order_note('Payment Action: ' . $this->payment_action);
14171419
}

paypal-for-woocommerce.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
if (!defined('ABSPATH')) {
2929
exit();
3030
}
31-
31+
if (!defined('PAYPAL_PARTNER_ATTRIBUTION_ID')) {
32+
define('PAYPAL_PARTNER_ATTRIBUTION_ID', 'AngellEYELLC_SP_AwesomeMotive');
33+
}
3234
require_once('angelleye-includes/angelleye-functions.php');
3335
require_once('angelleye-includes/angelleye-session-functions.php');
3436
require_once('angelleye-includes/angelleye-conditional-functions.php');

0 commit comments

Comments
 (0)