From c3162ec751595b093f704faa2cf9f0d7b53765e0 Mon Sep 17 00:00:00 2001 From: Nick Snellock Date: Fri, 11 Dec 2015 10:30:53 +0000 Subject: [PATCH 1/5] Adding 'Number' attribute to PayPal items --- src/Message/AbstractRequest.php | 18 +++++++++++ src/PayPalItem.php | 32 +++++++++++++++++++ src/PayPalItemBag.php | 32 +++++++++++++++++++ tests/Message/ExpressAuthorizeRequestTest.php | 3 +- 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/PayPalItem.php create mode 100644 src/PayPalItemBag.php diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index d69a6f0..ab04b08 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -5,6 +5,9 @@ namespace Omnipay\PayPal\Message; +use Omnipay\Common\ItemBag; +use Omnipay\PayPal\PayPalItemBag; + /** * PayPal Abstract Request * @@ -302,6 +305,7 @@ protected function getItemData() $data["L_PAYMENTREQUEST_0_DESC$n"] = $item->getDescription(); $data["L_PAYMENTREQUEST_0_QTY$n"] = $item->getQuantity(); $data["L_PAYMENTREQUEST_0_AMT$n"] = $this->formatCurrency($item->getPrice()); + $data["L_PAYMENTREQUEST_0_NUMBER$n"] = $item->getCode(); $data["PAYMENTREQUEST_0_ITEMAMT"] += $item->getQuantity() * $this->formatCurrency($item->getPrice()); } @@ -328,4 +332,18 @@ protected function createResponse($data) { return $this->response = new Response($this, $data); } + + /** + * Set the items in this order + * + * @param ItemBag|array $items An array of items in this order + */ + public function setItems($items) + { + if ($items && !$items instanceof ItemBag) { + $items = new PayPalItemBag($items); + } + + return $this->setParameter('items', $items); + } } diff --git a/src/PayPalItem.php b/src/PayPalItem.php new file mode 100644 index 0000000..9b2ded6 --- /dev/null +++ b/src/PayPalItem.php @@ -0,0 +1,32 @@ +getParameter('code'); + } + + /** + * Set the item code + */ + public function setCode($value) + { + return $this->setParameter('code', $value); + } +} diff --git a/src/PayPalItemBag.php b/src/PayPalItemBag.php new file mode 100644 index 0000000..5f12320 --- /dev/null +++ b/src/PayPalItemBag.php @@ -0,0 +1,32 @@ +items[] = $item; + } else { + $this->items[] = new PayPalItem($item); + } + } +} diff --git a/tests/Message/ExpressAuthorizeRequestTest.php b/tests/Message/ExpressAuthorizeRequestTest.php index 16848af..b87600d 100644 --- a/tests/Message/ExpressAuthorizeRequestTest.php +++ b/tests/Message/ExpressAuthorizeRequestTest.php @@ -148,7 +148,7 @@ public function testGetDataWithCard() public function testGetDataWithItems() { $this->request->setItems(array( - array('name' => 'Floppy Disk', 'description' => 'MS-DOS', 'quantity' => 2, 'price' => 10), + array('name' => 'Floppy Disk', 'description' => 'MS-DOS', 'quantity' => 2, 'price' => 10, 'code' => '123456'), array('name' => 'CD-ROM', 'description' => 'Windows 95', 'quantity' => 1, 'price' => 40), )); @@ -157,6 +157,7 @@ public function testGetDataWithItems() $this->assertSame('MS-DOS', $data['L_PAYMENTREQUEST_0_DESC0']); $this->assertSame(2, $data['L_PAYMENTREQUEST_0_QTY0']); $this->assertSame('10.00', $data['L_PAYMENTREQUEST_0_AMT0']); + $this->assertSame('123456', $data['L_PAYMENTREQUEST_0_NUMBER0']); $this->assertSame('CD-ROM', $data['L_PAYMENTREQUEST_0_NAME1']); $this->assertSame('Windows 95', $data['L_PAYMENTREQUEST_0_DESC1']); From f31aee71dbd15dfc977f4c66953f5e578bede2e5 Mon Sep 17 00:00:00 2001 From: Nick Snellock Date: Fri, 11 Dec 2015 10:39:13 +0000 Subject: [PATCH 2/5] Fixing tests --- tests/Message/RestCreateSubscriptionRequestTest.php | 2 +- tests/RestGatewayTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Message/RestCreateSubscriptionRequestTest.php b/tests/Message/RestCreateSubscriptionRequestTest.php index 7dfba2b..2ae9cd5 100644 --- a/tests/Message/RestCreateSubscriptionRequestTest.php +++ b/tests/Message/RestCreateSubscriptionRequestTest.php @@ -19,7 +19,7 @@ public function setUp() $this->request->initialize(array( 'name' => 'Test Subscription', 'description' => 'Test Billing Subscription', - 'startDate' => new \DateTime(), + 'startDate' => new \DateTime('now', new \DateTimeZone('UTC')), 'planId' => 'ABC-123', 'payerDetails' => array( 'payment_method' => 'paypal', diff --git a/tests/RestGatewayTest.php b/tests/RestGatewayTest.php index 185ac6a..d1dc8a9 100644 --- a/tests/RestGatewayTest.php +++ b/tests/RestGatewayTest.php @@ -138,7 +138,7 @@ public function testFullRefund() $endPoint = $request->getEndpoint(); $this->assertSame('https://api.paypal.com/v1/payments/sale/abc123/refund', $endPoint); $data = $request->getData(); - $this->assertEmpty($data); + $this->assertEmpty((array)$data); } public function testFetchTransaction() From 4dd174a60865cf65ef854e2ede03af94c30a6fc7 Mon Sep 17 00:00:00 2001 From: Nick Snellock Date: Fri, 5 Feb 2016 12:29:14 +0000 Subject: [PATCH 3/5] Implementing Order request --- src/ExpressGateway.php | 11 +++++++++++ src/Message/ExpressCompleteOrderRequest.php | 17 +++++++++++++++++ src/Message/ExpressOrderRequest.php | 17 +++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 src/Message/ExpressCompleteOrderRequest.php create mode 100644 src/Message/ExpressOrderRequest.php diff --git a/src/ExpressGateway.php b/src/ExpressGateway.php index f379e89..1b4cfe1 100644 --- a/src/ExpressGateway.php +++ b/src/ExpressGateway.php @@ -147,4 +147,15 @@ public function fetchCheckout(array $parameters = array()) { return $this->createRequest('\Omnipay\PayPal\Message\ExpressFetchCheckoutRequest', $parameters); } + + public function order(array $parameters = array()) + { + return $this->createRequest('\Omnipay\PayPal\Message\ExpressOrderRequest', $parameters); + } + + public function completeOrder(array $parameters = array()) + { + return $this->createRequest('\Omnipay\PayPal\Message\ExpressCompleteOrderRequest', $parameters); + } + } diff --git a/src/Message/ExpressCompleteOrderRequest.php b/src/Message/ExpressCompleteOrderRequest.php new file mode 100644 index 0000000..a282252 --- /dev/null +++ b/src/Message/ExpressCompleteOrderRequest.php @@ -0,0 +1,17 @@ + Date: Fri, 19 Feb 2016 16:24:20 +0000 Subject: [PATCH 4/5] Adding tests for order processing --- tests/ExpressGatewayTest.php | 26 ++++++++++++++++++++++++++ tests/Mock/ExpressOrderFailure.txt | 8 ++++++++ tests/Mock/ExpressOrderSuccess.txt | 8 ++++++++ 3 files changed, 42 insertions(+) create mode 100644 tests/Mock/ExpressOrderFailure.txt create mode 100644 tests/Mock/ExpressOrderSuccess.txt diff --git a/tests/ExpressGatewayTest.php b/tests/ExpressGatewayTest.php index fabf79e..944403a 100644 --- a/tests/ExpressGatewayTest.php +++ b/tests/ExpressGatewayTest.php @@ -89,6 +89,32 @@ public function testPurchaseFailure() $this->assertSame('This transaction cannot be processed. The amount to be charged is zero.', $response->getMessage()); } + public function testOrderSuccess() + { + $this->setMockHttpResponse('ExpressOrderSuccess.txt'); + + $response = $this->gateway->order($this->options)->send(); + + $this->assertInstanceOf('\Omnipay\PayPal\Message\ExpressAuthorizeResponse', $response); + $this->assertFalse($response->isPending()); + $this->assertFalse($response->isSuccessful()); + $this->assertTrue($response->isRedirect()); + $this->assertEquals('https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=EC-42721413K79637829', $response->getRedirectUrl()); + } + + public function testOrderFailure() + { + $this->setMockHttpResponse('ExpressOrderFailure.txt'); + + $response = $this->gateway->order($this->options)->send(); + + $this->assertFalse($response->isPending()); + $this->assertFalse($response->isSuccessful()); + $this->assertFalse($response->isRedirect()); + $this->assertNull($response->getTransactionReference()); + $this->assertSame('This transaction cannot be processed. The amount to be charged is zero.', $response->getMessage()); + } + public function testVoidSuccess() { $this->setMockHttpResponse('ExpressVoidSuccess.txt'); diff --git a/tests/Mock/ExpressOrderFailure.txt b/tests/Mock/ExpressOrderFailure.txt new file mode 100644 index 0000000..431a16f --- /dev/null +++ b/tests/Mock/ExpressOrderFailure.txt @@ -0,0 +1,8 @@ +HTTP/1.1 200 OK +Date: Fri, 15 Feb 2013 19:21:05 GMT +Server: Apache +Content-Length: 292 +Connection: close +Content-Type: text/plain; charset=utf-8 + +TIMESTAMP=2013%2d02%2d15T19%3a21%3a06Z&CORRELATIONID=2b58be2b8e60e&ACK=Failure&VERSION=85%2e0&BUILD=5060305&L_ERRORCODE0=10525&L_SHORTMESSAGE0=Invalid%20Data&L_LONGMESSAGE0=This%20transaction%20cannot%20be%20processed%2e%20The%20amount%20to%20be%20charged%20is%20zero%2e&L_SEVERITYCODE0=Error \ No newline at end of file diff --git a/tests/Mock/ExpressOrderSuccess.txt b/tests/Mock/ExpressOrderSuccess.txt new file mode 100644 index 0000000..86c40d4 --- /dev/null +++ b/tests/Mock/ExpressOrderSuccess.txt @@ -0,0 +1,8 @@ +HTTP/1.1 200 OK +Date: Fri, 15 Feb 2013 19:19:21 GMT +Server: Apache +Content-Length: 136 +Connection: close +Content-Type: text/plain; charset=utf-8 + +TOKEN=EC%2d42721413K79637829&TIMESTAMP=2013%2d02%2d15T19%3a19%3a21Z&CORRELATIONID=37b8b9915987c&ACK=Success&VERSION=85%2e0&BUILD=5060305 \ No newline at end of file From 35c90b252a5c0ae418f0f661a1b02a3f7be0b8b7 Mon Sep 17 00:00:00 2001 From: Nick Snellock Date: Mon, 21 Mar 2016 17:23:13 +0000 Subject: [PATCH 5/5] Correcting PSR-2 fault --- src/ExpressGateway.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ExpressGateway.php b/src/ExpressGateway.php index 5523f19..0510e72 100644 --- a/src/ExpressGateway.php +++ b/src/ExpressGateway.php @@ -165,5 +165,4 @@ public function completeOrder(array $parameters = array()) { return $this->createRequest('\Omnipay\PayPal\Message\ExpressCompleteOrderRequest', $parameters); } - }