Ejegg has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/350960 )

Change subject: WIP Paypal EC: send donor back to PP on code 10486
......................................................................

WIP Paypal EC: send donor back to PP on code 10486

It's a code that means "their first funding source didn't work, but
they might have another"

Bug: T163458
Change-Id: Ia61b118b968284a2c0e377d3b79dc9ee412646fa
---
M gateway_common/PaymentResult.php
M paypal_gateway/express_checkout/paypal_express.adapter.php
M tests/phpunit/Adapter/PayPal/PayPalExpressTest.php
A 
tests/phpunit/includes/Responses/paypal_ec/DoExpressCheckoutPayment_10486.testresponse
A 
tests/phpunit/includes/Responses/paypal_ec/GetExpressCheckoutDetails_10486.testresponse
5 files changed, 72 insertions(+), 8 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DonationInterface 
refs/changes/60/350960/1

diff --git a/gateway_common/PaymentResult.php b/gateway_common/PaymentResult.php
index 77acb8f..44a0970 100644
--- a/gateway_common/PaymentResult.php
+++ b/gateway_common/PaymentResult.php
@@ -27,9 +27,9 @@
        protected $iframe;
        protected $form;
        protected $redirect;
-       protected $refresh;
+       protected $refresh = false;
        protected $errors = array();
-       protected $failed;
+       protected $failed = false;
 
        protected function __construct() {
        }
diff --git a/paypal_gateway/express_checkout/paypal_express.adapter.php 
b/paypal_gateway/express_checkout/paypal_express.adapter.php
index 4fee2d4..56e44d1 100644
--- a/paypal_gateway/express_checkout/paypal_express.adapter.php
+++ b/paypal_gateway/express_checkout/paypal_express.adapter.php
@@ -466,13 +466,51 @@
                                ) );
                        }
                } catch ( Exception $ex ) {
-                       // TODO: Parse the API error fields and log them.
-                       $this->logger->error( "Failure detected in " . 
json_encode( $response ) );
-                       $this->finalizeInternalStatus( FinalStatus::FAILED );
-                       throw $ex;
+                       $errors = $this->parseResponseErrors( $response );
+                       $fatal = true;
+                       // TODO: Handle more error codes
+                       foreach ( $errors as $code => $error ) {
+                               // There are errors, so it wasn't a total comms 
failure
+                               
$this->transaction_response->setCommunicationStatus( true );
+                               $this->logger->warning(
+                                       "Error code $code returned: 
'{$error['debugInfo']}'"
+                               );
+                               switch ( $code ) {
+                                       case '10486':
+                                               // Donor's first funding method 
failed, but they might have another
+                                               
$this->transaction_response->setRedirect(
+                                                       
$this->account_config['RedirectURL'] . $response['TOKEN']
+                                               );
+                                               $fatal = false;
+                                               break;
+                                       default:
+                                               
$this->transaction_response->addError( $code, $error );
+                               }
+                       }
+                       if ( $fatal ) {
+                               if ( empty( $errors ) ) {
+                                       // Unrecognizable problems, log the 
whole thing
+                                       $this->logger->error( "Failure detected 
in " . json_encode( $response ) );
+                               }
+                               $this->finalizeInternalStatus( 
FinalStatus::FAILED );
+                               throw $ex;
+                       }
                }
        }
 
+       protected function parseResponseErrors( $response ) {
+               $errors = array();
+               // TODO: can they put errors in other places too?
+               if ( isset( $response['L_ERRORCODE0'] ) ) {
+                       $errors[$response['L_ERRORCODE0']] = array(
+                               'logLevel' => LogLevel::ERROR,
+                               'message' => '',
+                               'debugInfo' => isset( 
$response['L_LONGMESSAGE0'] ) ? $response['L_LONGMESSAGE0'] : '',
+                       );
+               }
+               return $errors;
+       }
+
        public function processDonorReturn( $requestValues ) {
                $this->addRequestData( array(
                        'ec_token' => $requestValues['token'],
diff --git a/tests/phpunit/Adapter/PayPal/PayPalExpressTest.php 
b/tests/phpunit/Adapter/PayPal/PayPalExpressTest.php
index 4c655c9..953b3fc 100644
--- a/tests/phpunit/Adapter/PayPal/PayPalExpressTest.php
+++ b/tests/phpunit/Adapter/PayPal/PayPalExpressTest.php
@@ -88,7 +88,6 @@
                );
        }
 
-
        public function testProcessDonorReturnRecurring() {
                $init = $this->getDonorTestData( 'US' );
                $init['contribution_tracking_id'] = '45931210';
@@ -98,7 +97,7 @@
                $gateway->setDummyGatewayResponseCode( 'Recurring-OK' );
                $gateway->processDonorReturn( array(
                        'token' => 'EC%2d4V987654XA123456V',
-                       'PayerID' => 'ASDASD'
+                       'PayerID' => 'ASDASD    '
                ) );
 
                $message = DonationQueue::instance()->pop( 'complete' );
@@ -137,4 +136,29 @@
                        'Sending extra messages to complete queue!'
                );
        }
+
+       /**
+        * Check that we send the donor back to paypal to try a different source
+        */
+       function testProcessDonorReturnPaymentRetry() {
+               $init = $this->getDonorTestData( 'US' );
+               $init['contribution_tracking_id'] = '45931210';
+
+               $gateway = $this->getFreshGatewayObject( $init );
+               $gateway->setDummyGatewayResponseCode( '10486' );
+               $result = $gateway->processDonorReturn( array(
+                       'token' => 'EC%2d2D123456D9876543U',
+                       'PayerID' => 'ASDASD'
+               ) );
+
+               $message = DonationQueue::instance()->pop( 'complete' );
+               $this->assertNull( $message, 'Should not queue a message' );
+               $this->assertFalse( $result->isFailed() );
+               $redirect = $result->getRedirect();
+               $this->assertEquals(
+                       
'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-2D123456D9876543U',
+                       $redirect
+               );
+       }
+
 }
diff --git 
a/tests/phpunit/includes/Responses/paypal_ec/DoExpressCheckoutPayment_10486.testresponse
 
b/tests/phpunit/includes/Responses/paypal_ec/DoExpressCheckoutPayment_10486.testresponse
new file mode 100644
index 0000000..7eab458
--- /dev/null
+++ 
b/tests/phpunit/includes/Responses/paypal_ec/DoExpressCheckoutPayment_10486.testresponse
@@ -0,0 +1 @@
+TOKEN=EC%2d2D123456D9876543U&SUCCESSPAGEREDIRECTREQUESTED=false&TIMESTAMP=2017%2d04%2d20T16%3a59%3a06Z&CORRELATIONID=537ffff0fefa&ACK=Failure&VERSION=204&BUILD=32574509&L_ERRORCODE0=10486&L_SHORTMESSAGE0=This%20transaction%20couldn%27t%20be%20completed%2e&L_LONGMESSAGE0=This%20transaction%20couldn%27t%20be%20completed%2e%20Please%20redirect%20your%20customer%20to%20PayPal%2e&L_SEVERITYCODE0=Error
diff --git 
a/tests/phpunit/includes/Responses/paypal_ec/GetExpressCheckoutDetails_10486.testresponse
 
b/tests/phpunit/includes/Responses/paypal_ec/GetExpressCheckoutDetails_10486.testresponse
new file mode 100644
index 0000000..f945850
--- /dev/null
+++ 
b/tests/phpunit/includes/Responses/paypal_ec/GetExpressCheckoutDetails_10486.testresponse
@@ -0,0 +1 @@
+TOKEN=EC%2d2D123456D9876543U&BILLINGAGREEMENTACCEPTEDSTATUS=0&CHECKOUTSTATUS=PaymentActionNotInitiated&TIMESTAMP=2017%2d02%2d01T20%3a07%3a14Z&CORRELATIONID=d70c9a334455e&ACK=Success&VERSION=204&BUILD=28806785&EMAIL=donor%40generous%2enet&PAYERID=8R297FE87CD8S&PAYERSTATUS=unverified&FIRSTNAME=Fezziwig&LASTNAME=Fowl&COUNTRYCODE=US&BILLINGNAME=Fezziwig%20Fowl&STREET=123%20Notta%20Way&CITY=Whoville&STATE=OR&ZIP=97211&COUNTRY=US&COUNTRYNAME=United%20States&ADDRESSID=PayPal&ADDRESSSTATUS=Confirmed&CURRENCYCODE=USD&AMT=1%2e55&ITEMAMT=1%2e55&SHIPPINGAMT=0&HANDLINGAMT=0&TAXAMT=0&CUSTOM=45931210&DESC=Donation%20to%20the%20Wikimedia%20Foundation&INVNUM=45931210%2e0&NOTIFYURL=http%3a%2f%2ffundraising%2ewikimedia%2eorg%2fIPNListener_Standalone%2ephp&INSURANCEAMT=0&SHIPDISCAMT=0&INSURANCEOPTIONOFFERED=false&L_QTY0=1&L_TAXAMT0=0&L_AMT0=1%2e55&L_DESC0=Donation%20to%20the%20Wikimedia%20Foundation&PAYMENTREQUEST_0_CURRENCYCODE=USD&PAYMENTREQUEST_0_AMT=1%2e55&PAYMENTREQUEST_0_ITEMAMT=1%2e55&PAYMENTREQUEST_0_SHIPPINGAMT=0&PAYMENTREQUEST_0_HANDLINGAMT=0&PAYMENTREQUEST_0_TAXAMT=0&PAYMENTREQUEST_0_CUSTOM=45931210&PAYMENTREQUEST_0_DESC=Donation%20to%20the%20Wikimedia%20Foundation&PAYMENTREQUEST_0_INVNUM=45931210%2e0&PAYMENTREQUEST_0_NOTIFYURL=http%3a%2f%2ffundraising%2ewikimedia%2eorg%2fIPNListener_Standalone%2ephp&PAYMENTREQUEST_0_INSURANCEAMT=0&PAYMENTREQUEST_0_SHIPDISCAMT=0&PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID=receiver%40wikimedia%2eorg&PAYMENTREQUEST_0_INSURANCEOPTIONOFFERED=false&PAYMENTREQUEST_0_ADDRESSSTATUS=Confirmed&L_PAYMENTREQUEST_0_QTY0=1&L_PAYMENTREQUEST_0_TAXAMT0=0&L_PAYMENTREQUEST_0_AMT0=1%2e55&L_PAYMENTREQUEST_0_DESC0=Donation%20to%20the%20Wikimedia%20Foundation&PAYMENTREQUESTINFO_0_ERRORCODE=0

-- 
To view, visit https://gerrit.wikimedia.org/r/350960
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia61b118b968284a2c0e377d3b79dc9ee412646fa
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to