jenkins-bot has submitted this change and it was merged.

Change subject: Fix Amazon order IDs for manual captures
......................................................................


Fix Amazon order IDs for manual captures

When a capture that we request via the front end goes through
correctly, the associated IPN message has our merchant reference
as 'CaptureReferenceId'. However, when the capture fails and DS
has to push it through via the payments console, it gets an
Amazon-generated CaptureReferenceId starting with AUTHORIZE_.
That ID is useless to us, but we can look up the original
merchant reference via the AmazonCaptureId.

The order reference ID is the first 19 digits of that number,
and we should now be setting the merchant reference on the parent
order ID.

Bug: T147973
Change-Id: I02f54e8f74f07e436a3d25409c5377ed08c68b21
---
A PaymentProviders/Amazon/Actions/ReconstructMerchantReference.php
M PaymentProviders/Amazon/AmazonApi.php
A PaymentProviders/Amazon/Tests/AmazonTestCase.php
M PaymentProviders/Amazon/Tests/Data/responses/getOrderReferenceDetails.json
A PaymentProviders/Amazon/Tests/phpunit/ActionsTest.php
M PaymentProviders/Amazon/Tests/phpunit/ApiTest.php
M SmashPig.yaml
7 files changed, 109 insertions(+), 18 deletions(-)

Approvals:
  XenoRyet: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/PaymentProviders/Amazon/Actions/ReconstructMerchantReference.php 
b/PaymentProviders/Amazon/Actions/ReconstructMerchantReference.php
new file mode 100644
index 0000000..5b25b91
--- /dev/null
+++ b/PaymentProviders/Amazon/Actions/ReconstructMerchantReference.php
@@ -0,0 +1,42 @@
+<?php namespace SmashPig\PaymentProviders\Amazon\Actions;
+
+use SmashPig\Core\Actions\IListenerMessageAction;
+use SmashPig\Core\Logging\Logger;
+use SmashPig\Core\Messages\ListenerMessage;
+use SmashPig\Core\SmashPigException;
+use SmashPig\PaymentProviders\Amazon\AmazonApi;
+use SmashPig\PaymentProviders\Amazon\ExpatriatedMessages\PaymentCapture;
+use SmashPig\PaymentProviders\Amazon\Tests\AmazonTestConfiguration;
+
+/**
+ * Looks up our reference ID for transactions pushed through manually
+ */
+class ReconstructMerchantReference implements IListenerMessageAction {
+
+       public function execute( ListenerMessage $msg ) {
+               // Bail out if not a PaymentCapture
+               if ( !( $msg instanceof PaymentCapture ) ) {
+                       return true;
+               }
+               $captureReference = $msg->getOrderId();
+               if ( !substr( $captureReference, 0, 10 ) === 'AUTHORIZE_' ) {
+                       // We only have to fix Amazon-generated IDs with that 
prefix
+                       return true;
+               }
+
+               $orderReferenceId = $msg->getOrderReferenceId();
+               Logger::info(
+                       "Looking up merchant reference for OrderReference 
$orderReferenceId"
+               );
+               try {
+                       $orderId = AmazonApi::get()->findMerchantReference( 
$orderReferenceId );
+                       if ( $orderId ) {
+                               $msg->setOrderId( $orderId );
+                       }
+                       return true;
+               } catch ( SmashPigException $ex ) {
+                       Logger::error( $ex->getMessage() );
+                       return false;
+               }
+       }
+}
diff --git a/PaymentProviders/Amazon/AmazonApi.php 
b/PaymentProviders/Amazon/AmazonApi.php
index 530fb0d..6e27411 100644
--- a/PaymentProviders/Amazon/AmazonApi.php
+++ b/PaymentProviders/Amazon/AmazonApi.php
@@ -87,6 +87,20 @@
        }
 
        /**
+        * @param string $orderReferenceId
+        * @return string|null Merchant reference for the order ID, or null if
+        *  not set
+        */
+       public function findMerchantReference( $orderReferenceId ) {
+               $details = $this->getOrderReferenceDetails( $orderReferenceId );
+
+               if ( isset( $details['SellerOrderAttributes']['SellerOrderId'] 
) ) {
+                       return 
$details['SellerOrderAttributes']['SellerOrderId'];
+               }
+               return null;
+       }
+
+       /**
         * @param string $orderReferenceId 19 character Amazon order ID
         * @return array OrderReferenceDetails as an associative array
         *  @see 
https://payments.amazon.com/documentation/apireference/201752660
diff --git a/PaymentProviders/Amazon/Tests/AmazonTestCase.php 
b/PaymentProviders/Amazon/Tests/AmazonTestCase.php
new file mode 100644
index 0000000..3e95efd
--- /dev/null
+++ b/PaymentProviders/Amazon/Tests/AmazonTestCase.php
@@ -0,0 +1,30 @@
+<?php
+namespace SmashPig\PaymentProviders\Amazon\Tests;
+
+use ReflectionClass;
+use SmashPig\Core\Context;
+use SmashPig\Tests\BaseSmashPigUnitTestCase;
+
+class AmazonTestCase extends BaseSmashPigUnitTestCase {
+
+       protected $mockClient;
+
+       public function setUp() {
+               parent::setUp();
+               chdir( __DIR__ ); // So the mock client can find its response 
files
+               $config = AmazonTestConfiguration::instance();
+               Context::initWithLogger( $config );
+               $this->mockClient = $config->object( 'payments-client', true );
+               $this->mockClient->calls = array();
+               $this->mockClient->returns = array();
+               $this->mockClient->exceptions = array();
+       }
+
+       public function tearDown() {
+               parent::tearDown();
+               $api = new ReflectionClass( 
'SmashPig\PaymentProviders\Amazon\AmazonApi' );
+               $instance = $api->getProperty( 'instance' );
+               $instance->setAccessible( true );
+               $instance->setValue( null );
+       }
+}
diff --git 
a/PaymentProviders/Amazon/Tests/Data/responses/getOrderReferenceDetails.json 
b/PaymentProviders/Amazon/Tests/Data/responses/getOrderReferenceDetails.json
index 04c8f42..3d6ffc6 100644
--- a/PaymentProviders/Amazon/Tests/Data/responses/getOrderReferenceDetails.json
+++ b/PaymentProviders/Amazon/Tests/Data/responses/getOrderReferenceDetails.json
@@ -13,7 +13,9 @@
                     "P01-0133129-0199515-A019658"
                 ]
             },
-            "SellerOrderAttributes": [],
+            "SellerOrderAttributes": {
+                "SellerOrderId": "123456789-0"
+            },
             "OrderTotal": {
                 "CurrencyCode": "USD",
                 "Amount": "10.00"
@@ -32,4 +34,4 @@
         "RequestId": "896332ae-e316-4eb6-865e-79278e1aa214"
     },
     "ResponseStatus": "200"
-}
\ No newline at end of file
+}
diff --git a/PaymentProviders/Amazon/Tests/phpunit/ActionsTest.php 
b/PaymentProviders/Amazon/Tests/phpunit/ActionsTest.php
new file mode 100644
index 0000000..af1d965
--- /dev/null
+++ b/PaymentProviders/Amazon/Tests/phpunit/ActionsTest.php
@@ -0,0 +1,17 @@
+<?php
+namespace SmashPig\PaymentProviders\Amazon\Tests;
+
+use SmashPig\PaymentProviders\Amazon\Actions\ReconstructMerchantReference;
+use SmashPig\PaymentProviders\Amazon\ExpatriatedMessages\CaptureCompleted;
+
+class ActionsTest extends AmazonTestCase {
+
+       public function testReconstructMerchantId() {
+               $captureCompleted = $this->loadJson( __DIR__ . 
"/../Data/IPN/CaptureCompleted.json" );
+               $captureCompleted["CaptureDetails"]["CaptureReferenceId"] = 
'AUTHORIZE_123456767';
+               $message = new CaptureCompleted( $captureCompleted );
+               $action = new ReconstructMerchantReference();
+               $action->execute( $message );
+               $this->assertEquals( '123456789-0', $message->getOrderId() );
+       }
+}
diff --git a/PaymentProviders/Amazon/Tests/phpunit/ApiTest.php 
b/PaymentProviders/Amazon/Tests/phpunit/ApiTest.php
index ddb5387..abbaabf 100644
--- a/PaymentProviders/Amazon/Tests/phpunit/ApiTest.php
+++ b/PaymentProviders/Amazon/Tests/phpunit/ApiTest.php
@@ -1,24 +1,9 @@
 <?php
 namespace SmashPig\PaymentProviders\Amazon\Tests;
 
-use SmashPig\Core\Context;
 use SmashPig\PaymentProviders\Amazon\AmazonApi;
-use SmashPig\Tests\BaseSmashPigUnitTestCase;
 
-class ApiTest extends BaseSmashPigUnitTestCase {
-
-       protected $mockClient;
-
-       public function setUp() {
-               parent::setUp();
-               chdir( __DIR__ . '/..' ); // So the mock client can find its 
response files
-               $config = AmazonTestConfiguration::instance();
-               Context::initWithLogger( $config );
-               $this->mockClient = $config->object( 'payments-client', true );
-               $this->mockClient->calls = array();
-               $this->mockClient->returns = array();
-               $this->mockClient->exceptions = array();
-       }
+class ApiTest extends AmazonTestCase {
 
        public function testFindParent() {
                $this->mockClient->returns['getAuthorizationDetails'][] = 
'Declined';
diff --git a/SmashPig.yaml b/SmashPig.yaml
index 9c8eab8..eab2a66 100644
--- a/SmashPig.yaml
+++ b/SmashPig.yaml
@@ -313,6 +313,7 @@
 
 amazon:
     actions:
+        - SmashPig\PaymentProviders\Amazon\Actions\ReconstructMerchantReference
         - SmashPig\PaymentProviders\Amazon\Actions\CloseOrderReference
         - SmashPig\PaymentProviders\Amazon\Actions\AssociateRefundParent
         - SmashPig\PaymentProviders\Amazon\Actions\AddMessageToQueue

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I02f54e8f74f07e436a3d25409c5377ed08c68b21
Gerrit-PatchSet: 5
Gerrit-Project: wikimedia/fundraising/SmashPig
Gerrit-Branch: master
Gerrit-Owner: Ejegg <eeggles...@wikimedia.org>
Gerrit-Reviewer: Awight <awi...@wikimedia.org>
Gerrit-Reviewer: Cdentinger <cdentin...@wikimedia.org>
Gerrit-Reviewer: Ejegg <eeggles...@wikimedia.org>
Gerrit-Reviewer: Siebrand <siebr...@kitano.nl>
Gerrit-Reviewer: XenoRyet <dkozlow...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to