Skip to content

Commit

Permalink
Merge pull request #119 from Toolstation/toolstation
Browse files Browse the repository at this point in the history
Implementing Express gateway order request
  • Loading branch information
delatbabel committed Mar 25, 2016
2 parents 119550c + 35c90b2 commit 3d39ab6
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/ExpressGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,14 @@ public function transactionSearch(array $parameters = array())
{
return $this->createRequest('\Omnipay\PayPal\Message\ExpressTransactionSearchRequest', $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);
}
}
18 changes: 18 additions & 0 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace Omnipay\PayPal\Message;

use Omnipay\Common\ItemBag;
use Omnipay\PayPal\PayPalItemBag;

/**
* PayPal Abstract Request
*
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -329,4 +333,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);
}
}
17 changes: 17 additions & 0 deletions src/Message/ExpressCompleteOrderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal Express Complete Order Request
*/
class ExpressCompleteOrderRequest extends ExpressCompleteAuthorizeRequest
{
public function getData()
{
$data = parent::getData();
$data['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Order';

return $data;
}
}
17 changes: 17 additions & 0 deletions src/Message/ExpressOrderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal Express Order Request
*/
class ExpressOrderRequest extends ExpressAuthorizeRequest
{
public function getData()
{
$data = parent::getData();
$data['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Order';

return $data;
}
}
32 changes: 32 additions & 0 deletions src/PayPalItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Paypal Item
*/

namespace Omnipay\PayPal;

use Omnipay\Common\Item;

/**
* Class PayPalItem
*
* @package Omnipay\PayPal
*/
class PayPalItem extends Item
{
/**
* {@inheritDoc}
*/
public function getCode()
{
return $this->getParameter('code');
}

/**
* Set the item code
*/
public function setCode($value)
{
return $this->setParameter('code', $value);
}
}
32 changes: 32 additions & 0 deletions src/PayPalItemBag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* PayPal Item bag
*/

namespace Omnipay\PayPal;

use Omnipay\Common\ItemBag;

/**
* Class PayPalItemBag
*
* @package Omnipay\PayPal
*/
class PayPalItemBag extends ItemBag
{
/**
* Add an item to the bag
*
* @see Item
*
* @param ItemInterface|array $item An existing item, or associative array of item parameters
*/
public function add($item)
{
if ($item instanceof ItemInterface) {
$this->items[] = $item;
} else {
$this->items[] = new PayPalItem($item);
}
}
}
26 changes: 26 additions & 0 deletions tests/ExpressGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion tests/Message/ExpressAuthorizeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,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),
));

Expand All @@ -158,6 +158,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']);
Expand Down
2 changes: 1 addition & 1 deletion tests/Message/RestCreateSubscriptionRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 8 additions & 0 deletions tests/Mock/ExpressOrderFailure.txt
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions tests/Mock/ExpressOrderSuccess.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3d39ab6

Please sign in to comment.