Fixing Breaking Changes in ItemList

This commit is contained in:
japatel
2014-10-14 16:54:43 -05:00
parent dc2ac0fd63
commit 89855f4624
18 changed files with 632 additions and 75 deletions

View File

@@ -61,11 +61,10 @@ If you do not want to use composer, you can grab the SDK zip that contains Paypa
## Running the sample
* Clone the repo and navigate to the samples folder.
* Samples have dependency on the sdk and you can use [`composer`](http://getcomposer.org) to get the dependencies. Ensure that you have composer installed on your machine, navigate to the samples folder and run `composer update --no-dev` to fetch the SDK.
* Samples have dependency on the sdk and you can use [`composer`](http://getcomposer.org) to get the dependencies. Ensure that you have composer installed on your machine, navigate to the root folder and run `composer update --no-dev` to fetch the SDK.
* Optionally, update the bootstrap.php file with your own client Id and client secret, that you could find from the [developer portal](https://developer.paypal.com)
* Run any of the samples in the 'samples' folder to see what the APIs can do.
## Usage
To write an app that uses the SDK

View File

@@ -12,17 +12,25 @@ use PayPal\Rest\ApiContext;
*
* @package PayPal\Api
*
* @property \PayPal\Api\Item items
* @property \PayPal\Api\Item[] items
* @property \PayPal\Api\ShippingAddress shipping_address
*/
class ItemList extends PPModel
{
/**
* Is this list empty?
*/
public function isEmpty()
{
return empty($this->items);
}
/**
* List of items.
*
*
* @param \PayPal\Api\Item $items
*
*
* @param \PayPal\Api\Item[] $items
*
* @return $this
*/
public function setItems($items)
@@ -43,10 +51,10 @@ class ItemList extends PPModel
/**
* Shipping address.
*
*
*
* @param \PayPal\Api\ShippingAddress $shipping_address
*
*
* @return $this
*/
public function setShippingAddress($shipping_address)
@@ -56,7 +64,34 @@ class ItemList extends PPModel
}
/**
* Shipping address.
* Append an item to the list.
*
* @return \PayPal\Api\Item
*/
public function addItem($item)
{
if (!$this->items) {
return $this->setItems(array($item));
} else {
return $this->setItems(
array_merge($this->items, array($item))
);
}
}
/**
* Remove an item from the list.
* Items are compared using === comparision (PHP.net)
*
* @return \PayPal\Api\Item
*/
public function removeItem($item)
{
return $this->setItems(array_diff($this->getItems(), array($item)));
}
/**
* Get Shipping Address
*
* @return \PayPal\Api\ShippingAddress
*/
@@ -81,6 +116,7 @@ class ItemList extends PPModel
/**
* Shipping address.
*
* @deprecated Instead use getShippingAddress
*
* @return \PayPal\Api\ShippingAddress

View File

@@ -165,12 +165,12 @@ class PPOpenIdTokeninfo extends ResourceModel
$params['grant_type'] = 'authorization_code';
}
if ($apiContext->get('client_id')) {
$clientId = $apiContext->get('client_id');
if (sizeof($apiContext->get($clientId)) > 0) {
$clientId = $apiContext->get($clientId);
}
if ($apiContext->get('client_secret')) {
$clientSecret = $apiContext->get('client_secret');
if (sizeof($apiContext->get($clientSecret)) > 0) {
$clientSecret = $apiContext->get($clientSecret);
}
$json = self::executeCall(
"/v1/identity/openidconnect/tokenservice",

View File

@@ -220,4 +220,14 @@ class PPModel
{
return json_encode($this->toArray(), $options);
}
/**
* Magic Method for toString
*
* @return string
*/
public function __toString()
{
return json_encode($this->toArray());
}
}

View File

@@ -24,8 +24,12 @@ use PayPal\Auth\OAuthTokenCredential;
error_reporting(E_ALL);
// Replace these values by entering your own ClientId and Secret by visiting https://developer.paypal.com/webapps/developer/applications/myapps
$clientId = 'AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS';
$clientSecret = 'EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL';
/** @var \Paypal\Rest\ApiContext $apiContext */
$apiContext = getApiContext();
$apiContext = getApiContext($clientId, $clientSecret);
return $apiContext;
/**
@@ -33,7 +37,7 @@ return $apiContext;
*
* @return PayPal\Rest\ApiContext
*/
function getApiContext()
function getApiContext($clientId, $clientSecret)
{
// ### Api context
@@ -44,8 +48,8 @@ function getApiContext()
$apiContext = new ApiContext(
new OAuthTokenCredential(
'EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM',
'EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM'
$clientId,
$clientSecret
)
);

View File

@@ -2,13 +2,10 @@
require __DIR__ . '/../bootstrap.php';
$clientId = 'AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS';
$clientSecret = 'EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL';
$baseUrl = getBaseUrl() . '/ExecuteAuth.php?success=true';
$baseUrl = getBaseUrl() . '/UserConsentRedirect.php?success=true';
//Get User Consent
// The clientId is stored in the bootstrap file
$redirectUrl = \PayPal\Auth\Openid\PPOpenIdSession::getAuthorizationUrl(
$baseUrl,
array('profile', 'email', 'phone'),

View File

@@ -0,0 +1,23 @@
<?php
require __DIR__ . '/../bootstrap.php';
// You can retrieve the refresh token by executing ObtainUserConsent.php and store the refresh token
$refreshToken = 'yzX4AkmMyBKR4on7vB5he-tDu38s24Zy-kTibhSuqA8kTdy0Yinxj7NpAyULx0bxqC5G8dbXOt0aVMlMmtpiVmSzhcjVZhYDM7WUQLC9KpaXGBHyltJPkLLQkXE';
$params = array(
'refresh_token' => $refreshToken,
'redirect_uri' => getBaseUrl() . '/UserConsentRedirect.php?success=true',
'client_id' => $clientId,
'client_secret' => $clientSecret
);
try {
$tokenInfo = new \PayPal\Auth\Openid\PPOpenIdTokeninfo();
$tokenInfo = $tokenInfo->createFromRefreshToken($params, $apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
exit(1);
}
print_result("Obtained Access Token From Refresh Token", "Access Token", $tokenInfo->getAccessToken(), $tokenInfo);

View File

@@ -9,21 +9,17 @@
require __DIR__ . '/../bootstrap.php';
$clientId = 'AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS';
$clientSecret = 'EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL';
use PayPal\Api\ExecutePayment;
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;
session_start();
if(isset($_GET['success']) && $_GET['success'] == 'true') {
$code = $_GET['code'];
$params = array(
'code' => $code,
'redirect_uri' => getBaseUrl() . '/ExecuteAuth.php?success=true',
'redirect_uri' => getBaseUrl() . '/UserConsentRedirect.php?success=true',
'client_id' => $clientId,
'client_secret' => $clientSecret
);
@@ -34,6 +30,7 @@ if(isset($_GET['success']) && $_GET['success'] == 'true') {
var_dump($ex->getData());
exit(1);
}
echo $accessToken->toJSON(JSON_PRETTY_PRINT);
print_result("Obtained Access Token", "Access Token", $accessToken->getAccessToken(), $accessToken);
}

View File

@@ -1,43 +1,43 @@
## This is an example configuration file for the SDK.
## The sample scripts configure the SDK dynamically
## but you can choose to go for file based configuration
## in simpler apps (See bootstrap.php for more).
;Connection Information
[Http]
http.ConnectionTimeOut = 30
http.Retry = 1
;http.Proxy=http://[username:password]@hostname[:port]
;Service Configuration
[Service]
mode=sandbox ; can be set to sandbox / live
;Logging Information
[Log]
log.LogEnabled=true
; When using a relative path, the log file is created
; relative to the .php file that is the entry point
; for this request. You can also provide an absolute
; path here
log.FileName=../PayPal.log
; Logging level can be one of FINE, INFO, WARN or ERROR
; Logging is most verbose in the 'FINE' level and
; decreases as you proceed towards ERROR
log.LogLevel=FINE
;Validation Configuration
[validation]
; If validation is set to strict, the PPModel would make sure that
; there are proper accessors (Getters and Setters) for each model
; objects. Accepted value is
; 'log' : logs the error message to logger only (default)
; 'strict' : throws a php notice message
; 'disable' : disable the validation
validation.level=strict
## This is an example configuration file for the SDK.
## The sample scripts configure the SDK dynamically
## but you can choose to go for file based configuration
## in simpler apps (See bootstrap.php for more).
;Connection Information
[Http]
http.ConnectionTimeOut = 30
http.Retry = 1
;http.Proxy=http://[username:password]@hostname[:port]
;Service Configuration
[Service]
mode=sandbox ; can be set to sandbox / live
;Logging Information
[Log]
log.LogEnabled=true
; When using a relative path, the log file is created
; relative to the .php file that is the entry point
; for this request. You can also provide an absolute
; path here
log.FileName=../PayPal.log
; Logging level can be one of FINE, INFO, WARN or ERROR
; Logging is most verbose in the 'FINE' level and
; decreases as you proceed towards ERROR
log.LogLevel=FINE
;Validation Configuration
[validation]
; If validation is set to strict, the PPModel would make sure that
; there are proper accessors (Getters and Setters) for each model
; objects. Accepted value is
; 'log' : logs the error message to logger only (default)
; 'strict' : throws a php notice message
; 'disable' : disable the validation
validation.level=strict

View File

@@ -7,8 +7,8 @@ use PayPal\Test\Constants;
class ItemListTest extends \PHPUnit_Framework_TestCase
{
private $items = array();
/** @var ItemList */
private $items;
private static $name = "item name";
private static $price = "1.12";
@@ -19,6 +19,7 @@ class ItemListTest extends \PHPUnit_Framework_TestCase
public static function createItemList()
{
/** @var Item $item */
$item = ItemTest::createItem();
$itemList = new ItemList();
@@ -48,4 +49,35 @@ class ItemListTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($itemList, $this->items);
}
}
public function testAddItemMethod()
{
$item2 = ItemTest::createItem();
$item2->setName("Item2");
$this->items->addItem($item2);
$found = false;
foreach ($this->items->getItems() as $item) {
if ($item->getName() == $item2->getName()) {
$found = true;
}
}
$this->assertTrue($found);
}
public function testRemoveItemMethod()
{
$itemList = new ItemList();
$item1 = ItemTest::createItem();
$item1->setName("Name1");
$item2 = ItemTest::createItem();
$itemList->addItem($item1);
$itemList->addItem($item2);
$itemList->removeItem($item2);
$this->assertEquals(sizeof($itemList->getItems()), 1);
$remainingElements = $itemList->getItems();
$this->assertEquals($remainingElements[0]->getName(), "Name1");
}
}

View File

@@ -49,4 +49,4 @@ class ItemTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($item, $this->item);
}
}
}

View File

@@ -0,0 +1,165 @@
<?php
namespace PayPal\Test\Api;
use PayPal\Api\Patch;
use PayPal\Common\PPModel;
use PayPal\Rest\ApiContext;
use PayPal\Rest\IResource;
use PayPal\Api\CreateProfileResponse;
use PayPal\Transport\PPRestCall;
use PayPal\Api\WebProfile;
/**
* Class WebProfile
*
* @package PayPal\Test\Api
*/
class WebProfileFunctionalTest extends \PHPUnit_Framework_TestCase
{
public $operation;
public $response;
public $mode = 'mock';
public $mockPPRestCall;
public function setUp()
{
$className = $this->getClassName();
$testName = $this->getName();
$operationString = file_get_contents(__DIR__ . "/../resources/$className/$testName.json");
$this->operation = json_decode($operationString, true);
$this->response = true;
if (array_key_exists('body', $this->operation['response'])) {
$this->response = json_encode($this->operation['response']['body']);
}
$this->mode = getenv('REST_MODE') ? getenv('REST_MODE') : 'mock';
if ($this->mode != 'sandbox') {
// Mock PPRest Caller if mode set to mock
$this->mockPPRestCall = $this->getMockBuilder('\PayPal\Transport\PPRestCall')
->disableOriginalConstructor()
->getMock();
$this->mockPPRestCall->expects($this->any())
->method('execute')
->will($this->returnValue(
$this->response
));
}
}
/**
* Returns just the classname of the test you are executing. It removes the namespaces.
* @return string
*/
public function getClassName()
{
return join('', array_slice(explode('\\', get_class($this)), -1));
}
public function testCreate()
{
$request = $this->operation['request']['body'];
$obj = new WebProfile($request);
$obj->setName(uniqid());
$result = $obj->create(null, $this->mockPPRestCall);
$this->assertNotNull($result);
return $result;
}
/**
* @depends testCreate
* @param $createProfileResponse CreateProfileResponse
* @return WebProfile
*/
public function testGet($createProfileResponse)
{
$result = WebProfile::get($createProfileResponse->getId(), null, $this->mockPPRestCall);
$this->assertNotNull($result);
$this->assertEquals($createProfileResponse->getId(), $result->getId());
$this->assertEquals($this->operation['response']['body']['presentation']['logo_image'], $result->getPresentation()->getLogoImage());
$this->assertEquals($this->operation['response']['body']['input_fields']['no_shipping'], $result->getInputFields()->getNoShipping());
$this->assertEquals($this->operation['response']['body']['input_fields']['address_override'], $result->getInputFields()->getAddressOverride());
return $result;
}
/**
* @depends testGet
* @param $webProfile WebProfile
*/
public function testGetList($webProfile)
{
$result = WebProfile::get_list(null, $this->mockPPRestCall);
$this->assertNotNull($result);
$found = false;
$foundObject = null;
foreach ($result as $webProfileObject) {
if ($webProfileObject->getId() == $webProfile->getId()) {
$found = true;
$foundObject = $webProfileObject;
break;
}
}
$this->assertTrue($found, "The Created Web Profile was not found in the get list");
$this->assertEquals($webProfile->getId(), $foundObject->getId());
$this->assertEquals($this->operation['response']['body'][0]['presentation']['logo_image'], $foundObject->getPresentation()->getLogoImage());
$this->assertEquals($this->operation['response']['body'][0]['input_fields']['no_shipping'], $foundObject->getInputFields()->getNoShipping());
$this->assertEquals($this->operation['response']['body'][0]['input_fields']['address_override'], $foundObject->getInputFields()->getAddressOverride());
}
/**
* @depends testGet
* @param $webProfile WebProfile
*/
public function testUpdate($webProfile)
{
$boolValue = $webProfile->getInputFields()->getNoShipping();
$newValue = ($boolValue + 1) % 2;
$webProfile->getInputFields()->setNoShipping($newValue);
$result = $webProfile->update(null, $this->mockPPRestCall);
$this->assertNotNull($result);
$this->assertEquals($webProfile->getInputFields()->getNoShipping(), $newValue);
}
/**
* @depends testGet
* @param $webProfile WebProfile
*/
public function testPartialUpdate($webProfile)
{
$patches = array();
$patches[] = new Patch('{
"op": "add",
"path": "/presentation/brand_name",
"value":"new_brand_name"
}');
$patches[] = new Patch('{
"op": "remove",
"path": "/flow_config/landing_page_type"
}');
$result = $webProfile->partial_update($patches, null, $this->mockPPRestCall);
$this->assertTrue($result);
}
/**
* @depends testGet
* @param $createProfileResponse CreateProfileResponse
*/
public function testDelete($createProfileResponse)
{
$webProfile = new WebProfile();
$webProfile->setId($createProfileResponse->getId());
$result = $webProfile->delete(null, $this->mockPPRestCall);
$this->assertTrue($result);
}
}

View File

@@ -0,0 +1,50 @@
{
"description": "Create a web experience profile.",
"title": "Create profile",
"runnable": true,
"operationId": "web-profile.create",
"user": {
"scopes": [
"https://api.paypal.com/v1/payments/.*"
]
},
"credentials": {
"oauth": {
"clientId": "test-client-01",
"clientSecret": "test_secret_a",
"path": "/v1/oauth2/token"
}
},
"request": {
"path": "v1/payment-experience/web-profiles/",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"PayPal-Request-Id": "abcdefgh123",
"Authorization": "Bearer ECvJ_yBNz_UfMmCvWEbT_2ZWXdzbFFQZ-1Y5K2NGgeHn"
},
"body": {
"name":"someName2",
"presentation":{
"logo_image":"http://www.ebay.com"
},
"input_fields":{
"no_shipping":1,
"address_override":1
},
"flow_config":{
"landing_page_type":"billing",
"bank_txn_pending_url":"http://www.ebay.com"
}
}
},
"response": {
"status": "201",
"headers": {
"Content-Type": "application/json"
},
"body": {
"id": "XP-RFV4-PVD8-AGHJ-8E5J"
}
}
}

View File

@@ -0,0 +1,30 @@
{
"description": "Delete a web experience profile list.",
"title": "Delete profile",
"runnable": true,
"operationId": "web-profile.delete",
"user": {
"scopes": [
"https://api.paypal.com/v1/payments/.*"
]
},
"credentials": {
"oauth": {
"clientId": "test-client-01",
"clientSecret": "test_secret_a",
"path": "/v1/oauth2/token"
}
},
"request": {
"path": "v1/payment-experience/web-profiles/XP-RFV4-PVD8-AGHJ-8E5J",
"method": "DELETE",
"headers": {
"Authorization": "Bearer ECvJ_yBNz_UfMmCvWEbT_2ZWXdzbFFQZ-1Y5K2NGgeHn"
}
},
"response": {
"status": "204",
"headers": {
}
}
}

View File

@@ -0,0 +1,46 @@
{
"description": "Fetch a web experience profile.",
"title": "Get profile",
"runnable": true,
"operationId": "web-profile.get",
"user": {
"scopes": [
"https://api.paypal.com/v1/payments/.*"
]
},
"credentials": {
"oauth": {
"clientId": "test-client-01",
"clientSecret": "test_secret_a",
"path": "/v1/oauth2/token"
}
},
"request": {
"path": "/v1/payment-experience/web-profiles/XP-RFV4-PVD8-AGHJ-8E5J",
"method": "GET",
"headers": {
"Authorization": "Bearer ECvJ_yBNz_UfMmCvWEbT_2ZWXdzbFFQZ-1Y5K2NGgeHn"
}
},
"response": {
"status": "200",
"headers": {
"Content-Type": "application/json"
},
"body": {
"id": "XP-RFV4-PVD8-AGHJ-8E5J",
"name":"someName2",
"presentation":{
"logo_image":"http://www.ebay.com"
},
"input_fields":{
"no_shipping":1,
"address_override":1
},
"flow_config":{
"landing_page_type":"billing",
"bank_txn_pending_url":"http://www.ebay.com"
}
}
}
}

View File

@@ -0,0 +1,77 @@
{
"description": "Fetch web experience profile list for a given merchant.",
"title": "Get profile list",
"runnable": true,
"operationId": "web-profile.get-list",
"user": {
"scopes": [
"https://api.paypal.com/v1/payments/.*"
]
},
"credentials": {
"oauth": {
"clientId": "test-client-01",
"clientSecret": "test_secret_a",
"path": "/v1/oauth2/token"
}
},
"request": {
"path": "/v1/payment-experience/web-profiles",
"method": "GET",
"headers": {
"Authorization": "Bearer ECvJ_yBNz_UfMmCvWEbT_2ZWXdzbFFQZ-1Y5K2NGgeHn"
}
},
"response": {
"status": "200",
"headers": {
"Content-Type": "application/json"
},
"body": [
{
"id": "XP-RFV4-PVD8-AGHJ-8E5J",
"name":"someName2",
"presentation":{
"logo_image":"http://www.ebay.com"
},
"input_fields":{
"no_shipping":1,
"address_override":1
},
"flow_config":{
"landing_page_type":"billing",
"bank_txn_pending_url":"http://www.ebay.com"
}
},
{
"id": "XP-A88A-LYLW-8Y3X-E5ER",
"name": "someName2",
"flow_config": {
"landing_page_type": "billing",
"bank_txn_pending_url": "http://www.ebay.com"
},
"input_fields": {
"no_shipping": 1,
"address_override": 1
},
"presentation": {
"logo_image": "http://www.ebay.com"
}
},
{
"id": "XP-RFV4-PVD8-AGHJ-8E5J",
"name": "someName2",
"flow_config": {
"bank_txn_pending_url": "http://www.ebay.com"
},
"input_fields": {
"no_shipping": 1,
"address_override": 1
},
"presentation": {
"logo_image": "http://www.ebay.com"
}
}
]
}
}

View File

@@ -0,0 +1,45 @@
{
"description": "Partially update a web experience profile.",
"title": "Partially Update profile",
"runnable": true,
"operationId": "web-profile.partial-update",
"user": {
"scopes": [
"https://api.paypal.com/v1/payments/.*"
]
},
"credentials": {
"oauth": {
"clientId": "test-client-01",
"clientSecret": "test_secret_a",
"path": "/v1/oauth2/token"
}
},
"request": {
"path": "v1/payment-experience/web-profiles/XP-RFV4-PVD8-AGHJ-8E5J",
"method": "PATCH",
"headers": {
"Content-Type": "application/json",
"PayPal-Request-Id": "abcdefgh123",
"Authorization": "Bearer ECvJ_yBNz_UfMmCvWEbT_2ZWXdzbFFQZ-1Y5K2NGgeHn"
},
"body": [
{
"op": "add",
"path": "/presentation/brand_name",
"value":"new_brand_name"
},
{
"op": "remove",
"path": "/flow_config/landing_page_type"
}
]
},
"response": {
"status": "204",
"headers": {
"Content-Type": "application/json"
}
}
}

View File

@@ -0,0 +1,46 @@
{
"description": "Update a web experience profile.",
"title": "Update profile",
"runnable": true,
"operationId": "web-profile.update",
"user": {
"scopes": [
"https://api.paypal.com/v1/payments/.*"
]
},
"credentials": {
"oauth": {
"clientId": "test-client-01",
"clientSecret": "test_secret_a",
"path": "/v1/oauth2/token"
}
},
"request": {
"path": "v1/payment-experience/web-profiles/XP-RFV4-PVD8-AGHJ-8E5J",
"method": "PUT",
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer ECvJ_yBNz_UfMmCvWEbT_2ZWXdzbFFQZ-1Y5K2NGgeHn"
},
"body": {
"name":"someName2",
"presentation":{
"logo_image":"http://www.ebay.com"
},
"input_fields":{
"no_shipping":0,
"address_override":1
},
"flow_config":{
"landing_page_type":"billing",
"bank_txn_pending_url":"http://www.ebay.com"
}
}
},
"response": {
"status": "204",
"headers": {
"Content-Type": "application/json"
}
}
}