Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API support #21

Merged
merged 11 commits into from
Jan 7, 2015
Merged
85 changes: 85 additions & 0 deletions src/Message/AbstractRestRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Omnipay\PayPal\Message;

use Guzzle\Http\EntityBody;

/**
* PayPal Abstract Request
*/
abstract class AbstractRestRequest extends \Omnipay\Common\Message\AbstractRequest
{
const API_VERSION = 'v1';

protected $liveEndpoint = 'https://api.paypal.com';
protected $testEndpoint = 'https://api.sandbox.paypal.com';

public function getClientId()
{
return $this->getParameter('clientId');
}

public function setClientId($value)
{
return $this->setParameter('clientId', $value);
}

public function getSecret()
{
return $this->getParameter('secret');
}

public function setSecret($value)
{
return $this->setParameter('secret', $value);
}

public function getToken()
{
return $this->getParameter('token');
}

public function setToken($value)
{
return $this->setParameter('token', $value);
}

protected function getHttpMethod()
{
return 'POST';
}

protected function getEndpoint()
{
$base = $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
return $base . '/' . self::API_VERSION;
}

public function sendData($data)
{
// don't throw exceptions for 4xx errors
$this->httpClient->getEventDispatcher()->addListener(
'request.error',
function ($event) {
if ($event['response']->isClientError()) {
$event->stopPropagation();
}
}
);

$httpRequest = $this->httpClient->createRequest(
$this->getHttpMethod(),
$this->getEndpoint(),
array(
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $this->getToken(),
'Content-type' => 'application/json',
),
json_encode($data)
);

$httpResponse = $httpRequest->send();

return $this->response = new RestResponse($this, $httpResponse->json(), $httpResponse->getStatusCode());
}
}
95 changes: 95 additions & 0 deletions src/Message/RestAuthorizeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal REST Authorize Request
*/
class RestAuthorizeRequest extends AbstractRestRequest
{
public function getData()
{
$data = array(
'intent' => 'authorize',
'payer' => array(
'payment_method' => 'credit_card',
'funding_instruments' => array()
),
'transactions' => array(
array(
'description' => $this->getDescription(),
'amount' => array(
'total' => $this->getAmount(),
'currency' => $this->getCurrency(),
),
)
)
);

if ($this->getCardReference()) {
$this->validate('amount');

$data['payer']['funding_instruments'][] = array(
'credit_card_token' => array(
'credit_card_id' => $this->getCardReference(),
),
);
} else {
$this->validate('amount', 'card');
$this->getCard()->validate();

$data['payer']['funding_instruments'][] = array(
'credit_card' => array(
'number' => $this->getCard()->getNumber(),
'type' => $this->getCard()->getBrand(),
'expire_month' => $this->getCard()->getExpiryMonth(),
'expire_year' => $this->getCard()->getExpiryYear(),
'cvv2' => $this->getCard()->getCvv(),
'first_name' => $this->getCard()->getFirstName(),
'last_name' => $this->getCard()->getLastName(),
'billing_address' => array(
'line1' => $this->getCard()->getAddress1(),
//'line2' => $this->getCard()->getAddress2(),
'city' => $this->getCard()->getCity(),
'state' => $this->getCard()->getState(),
'postal_code' => $this->getCard()->getPostcode(),
'country_code' => strtoupper($this->getCard()->getCountry()),
)
)
);

// There's currently a quirk with the REST API that requires line2 to be
// non-empty if it's present. Jul 14, 2014
$line2 = $this->getCard()->getAddress2();
if (!empty($line2)) {
$data['payer']['funding_instruments'][0]['credit_card']['billing_address']['line2'] = $line2;
}
}

return $data;
}

/**
* The REST API does not currently have support for passing an invoice number
* or transaction ID.
* @return string
*/
public function getDescription()
{
$id = $this->getTransactionId();
$desc = parent::getDescription();

if (empty($id)) {
return $desc;
} elseif (empty($desc)) {
return $id;
} else {
return "$id : $desc";
}
}

protected function getEndpoint()
{
return parent::getEndpoint() . '/payments/payment';
}
}
27 changes: 27 additions & 0 deletions src/Message/RestCaptureRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal REST Capture Request
*/
class RestCaptureRequest extends AbstractRestRequest
{
public function getData()
{
$this->validate('transactionReference', 'amount');

return array(
'amount' => array(
'currency' => $this->getCurrency(),
'total' => $this->getAmount(),
),
'is_final_capture' => true,
);
}

public function getEndpoint()
{
return parent::getEndpoint() . '/payments/authorization/' . $this->getTransactionReference() . '/capture';
}
}
47 changes: 47 additions & 0 deletions src/Message/RestCreateCardRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal REST Create Card Request
*/
class RestCreateCardRequest extends AbstractRestRequest
{
public function getData()
{
$this->validate('card');
$this->getCard()->validate();

$data = array(
'number' => $this->getCard()->getNumber(),
'type' => $this->getCard()->getBrand(),
'expire_month' => $this->getCard()->getExpiryMonth(),
'expire_year' => $this->getCard()->getExpiryYear(),
'cvv2' => $this->getCard()->getCvv(),
'first_name' => $this->getCard()->getFirstName(),
'last_name' => $this->getCard()->getLastName(),
'billing_address' => array(
'line1' => $this->getCard()->getAddress1(),
//'line2' => $this->getCard()->getAddress2(),
'city' => $this->getCard()->getCity(),
'state' => $this->getCard()->getState(),
'postal_code' => $this->getCard()->getPostcode(),
'country_code' => strtoupper($this->getCard()->getCountry()),
)
);

// There's currently a quirk with the REST API that requires line2 to be
// non-empty if it's present. Jul 14, 2014
$line2 = $this->getCard()->getAddress2();
if (!empty($line2)) {
$data['billing_address']['line2'] = $line2;
}

return $data;
}

protected function getEndpoint()
{
return parent::getEndpoint() . '/vault/credit-card';
}
}
25 changes: 25 additions & 0 deletions src/Message/RestDeleteCardRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal REST Delete Card Request
*/
class RestDeleteCardRequest extends AbstractRestRequest
{
public function getHttpMethod()
{
return 'DELETE';
}

public function getData()
{
$this->validate('cardReference');
return array();
}

public function getEndpoint()
{
return parent::getEndpoint() . '/vault/credit-card/' . $this->getCardReference();
}
}
20 changes: 20 additions & 0 deletions src/Message/RestFetchTransactionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal REST Fetch Transaction Request
*/
class RestFetchTransactionRequest extends AbstractRestRequest
{
public function getData()
{
$this->validate('transactionReference');
return array();
}

public function getEndpoint()
{
return parent::getEndpoint() . '/payments/sale/' . $this->getTransactionReference();
}
}
16 changes: 16 additions & 0 deletions src/Message/RestPurchaseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal REST Purchase Request
*/
class RestPurchaseRequest extends RestAuthorizeRequest
{
public function getData()
{
$data = parent::getData();
$data['intent'] = 'sale';
return $data;
}
}
32 changes: 32 additions & 0 deletions src/Message/RestRefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Omnipay\PayPal\Message;

/**
* PayPal REST Refund Request
* TODO: There might be a problem here, in that refunding a capture requires a different URL.
*/
class RestRefundRequest extends AbstractRestRequest
{
public function getData()
{
$this->validate('transactionReference');

if ($this->getAmount() > 0) {
return array(
'amount' => array(
'currency' => $this->getCurrency(),
'total' => $this->getAmount(),
),
'description' => $this->getDescription(),
);
} else {
return array();
}
}

public function getEndpoint()
{
return parent::getEndpoint() . '/payments/sale/' . $this->getTransactionReference() . '/refund';
}
}