forked from LiveCarta/PayPal-PHP-SDK
Enabled Billing Plans and Agreements APIs
- Added API Classes, Samples, and Tests - Updated Functional Tests - Updated Documentation with new SDK Name - Updated Few Samples to use newer nicer result page
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Test\Functional\Api;
|
||||
|
||||
use PayPal\Api\Agreement;
|
||||
use PayPal\Api\AgreementStateDescriptor;
|
||||
use PayPal\Api\Currency;
|
||||
use PayPal\Api\Patch;
|
||||
use PayPal\Api\PatchRequest;
|
||||
use PayPal\Api\Plan;
|
||||
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 Billing Agreements
|
||||
*
|
||||
* @package PayPal\Test\Api
|
||||
*/
|
||||
class BillingAgreementsFunctionalTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public $operation;
|
||||
|
||||
public $response;
|
||||
|
||||
public $mode = 'mock';
|
||||
|
||||
public $mockPPRestCall;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$className = $this->getClassName();
|
||||
$testName = $this->getName();
|
||||
$this->setupTest($className, $testName);
|
||||
}
|
||||
|
||||
public function setupTest($className, $testName)
|
||||
{
|
||||
$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));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Agreement
|
||||
*/
|
||||
public function testCreatePayPalAgreement()
|
||||
{
|
||||
$plan = BillingPlansFunctionalTest::getPlan();
|
||||
$request = $this->operation['request']['body'];
|
||||
$agreement = new Agreement($request);
|
||||
// Update the Schema to use a working Plan
|
||||
$agreement->getPlan()->setId($plan->getId());
|
||||
$result = $agreement->create(null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreatePayPalAgreement
|
||||
* @param $agreement Agreement
|
||||
* @return Agreement
|
||||
*/
|
||||
public function testExecute($agreement)
|
||||
{
|
||||
if ($this->mode == 'sandbox') {
|
||||
$this->markTestSkipped('Not executable on sandbox environment. Needs human interaction');
|
||||
}
|
||||
$links = $agreement->getLinks();
|
||||
$url = parse_url($links[0]->getHref(), 6);
|
||||
parse_str($url, $result);
|
||||
$paymentToken = $result['token'];
|
||||
$this->assertNotNull($paymentToken);
|
||||
$this->assertNotEmpty($paymentToken);
|
||||
$result = $agreement->execute($paymentToken, null, $this->mockPPRestCall);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Agreement
|
||||
*/
|
||||
public function testCreateCCAgreement()
|
||||
{
|
||||
$plan = BillingPlansFunctionalTest::getPlan();
|
||||
$request = $this->operation['request']['body'];
|
||||
$agreement = new Agreement($request);
|
||||
// Update the Schema to use a working Plan
|
||||
$agreement->getPlan()->setId($plan->getId());
|
||||
$result = $agreement->create(null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreateCCAgreement
|
||||
* @param $agreement Agreement
|
||||
* @return Plan
|
||||
*/
|
||||
public function testGet($agreement)
|
||||
{
|
||||
$result = Agreement::get($agreement->getId(), null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
$this->assertEquals($agreement->getId(), $result->getId());
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGet
|
||||
* @param $agreement Agreement
|
||||
*/
|
||||
public function testUpdate($agreement)
|
||||
{
|
||||
/** @var Patch[] $request */
|
||||
$request = $this->operation['request']['body'][0];
|
||||
$patch = new Patch();
|
||||
$patch->setOp($request['op']);
|
||||
$patch->setPath($request['path']);
|
||||
$patch->setValue($request['value']);
|
||||
$patches = array();
|
||||
$patches[] = $patch;
|
||||
$patchRequest = new PatchRequest();
|
||||
$patchRequest->setPatches($patches);
|
||||
$result = $agreement->update($patchRequest, null, $this->mockPPRestCall);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGet
|
||||
* @param $agreement Agreement
|
||||
* @return Agreement
|
||||
*/
|
||||
public function testSetBalance($agreement)
|
||||
{
|
||||
$this->markTestSkipped('Skipped as the fix is on the way.');
|
||||
$currency = new Currency($this->operation['request']['body']);
|
||||
$result = $agreement->setBalance($currency, null, $this->mockPPRestCall);
|
||||
$this->assertTrue($result);
|
||||
return $agreement;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGet
|
||||
* @param $agreement Agreement
|
||||
* @return Agreement
|
||||
*/
|
||||
public function testBillBalance($agreement)
|
||||
{
|
||||
$this->markTestSkipped('Skipped as the fix is on the way.');
|
||||
$agreementStateDescriptor = new AgreementStateDescriptor($this->operation['request']['body']);
|
||||
$result = $agreement->billBalance($agreementStateDescriptor, null, $this->mockPPRestCall);
|
||||
$this->assertTrue($result);
|
||||
return $agreement;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGet
|
||||
* @param $agreement Agreement
|
||||
* @return Agreement
|
||||
*/
|
||||
public function testGetTransactions($agreement)
|
||||
{
|
||||
$this->markTestSkipped('Skipped as the fix is on the way.');
|
||||
$result = Agreement::transactions($agreement->getId(), null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGet
|
||||
* @param $agreement Agreement
|
||||
* @return Agreement
|
||||
*/
|
||||
public function testSuspend($agreement)
|
||||
{
|
||||
$agreementStateDescriptor = new AgreementStateDescriptor($this->operation['request']['body']);
|
||||
$result = $agreement->suspend($agreementStateDescriptor, null, $this->mockPPRestCall);
|
||||
$this->setupTest($this->getClassName(), 'testGetSuspended');
|
||||
$get = $this->testGet($agreement);
|
||||
$this->assertTrue($result);
|
||||
$this->assertEquals('Suspended', $get->getState());
|
||||
return $get;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testSuspend
|
||||
* @param $agreement Agreement
|
||||
* @return Agreement
|
||||
*/
|
||||
public function testReactivate($agreement)
|
||||
{
|
||||
$agreementStateDescriptor = new AgreementStateDescriptor($this->operation['request']['body']);
|
||||
$result = $agreement->reActivate($agreementStateDescriptor, null, $this->mockPPRestCall);
|
||||
$this->assertTrue($result);
|
||||
$this->setupTest($this->getClassName(), 'testGet');
|
||||
$get = $this->testGet($agreement);
|
||||
$this->assertEquals('Active', $get->getState());
|
||||
return $get;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testReactivate
|
||||
* @param $agreement Agreement
|
||||
* @return Agreement
|
||||
*/
|
||||
public function testCancel($agreement)
|
||||
{
|
||||
$agreementStateDescriptor = new AgreementStateDescriptor($this->operation['request']['body']);
|
||||
$result = $agreement->cancel($agreementStateDescriptor, null, $this->mockPPRestCall);
|
||||
$this->assertTrue($result);
|
||||
$this->setupTest($this->getClassName(), 'testGetCancelled');
|
||||
$get = $this->testGet($agreement);
|
||||
$this->assertEquals('Cancelled', $get->getState());
|
||||
return $get;
|
||||
}
|
||||
|
||||
}
|
||||
217
tests/PayPal/Test/Functional/Api/BillingPlansFunctionalTest.php
Normal file
217
tests/PayPal/Test/Functional/Api/BillingPlansFunctionalTest.php
Normal file
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Test\Functional\Api;
|
||||
|
||||
use PayPal\Api\Patch;
|
||||
use PayPal\Api\PatchRequest;
|
||||
use PayPal\Api\Plan;
|
||||
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 Billing Plans
|
||||
*
|
||||
* @package PayPal\Test\Api
|
||||
*/
|
||||
class BillingPlansFunctionalTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public static $obj;
|
||||
|
||||
public $operation;
|
||||
|
||||
public $response;
|
||||
|
||||
public $mode = 'mock';
|
||||
|
||||
public $mockPPRestCall;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$className = $this->getClassName();
|
||||
$testName = $this->getName();
|
||||
$this->setupTest($className, $testName);
|
||||
}
|
||||
|
||||
public function setupTest($className, $testName)
|
||||
{
|
||||
$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
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get a Plan object in Active State
|
||||
*
|
||||
* @return Plan
|
||||
*/
|
||||
public static function getPlan()
|
||||
{
|
||||
if (!self::$obj) {
|
||||
$test = new self();
|
||||
// Creates a Plan
|
||||
$test->setupTest($test->getClassName(), 'testCreate');
|
||||
self::$obj = $test->testCreate();
|
||||
// Updates the Status to Active
|
||||
$test->setupTest($test->getClassName(), 'testUpdateChangingState');
|
||||
self::$obj = $test->testUpdateChangingState(self::$obj);
|
||||
}
|
||||
return self::$obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 Plan($request);
|
||||
$result = $obj->create(null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
self::$obj = $result;
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function testCreateWithNOChargeModel()
|
||||
{
|
||||
$request = $this->operation['request']['body'];
|
||||
$obj = new Plan($request);
|
||||
$result = $obj->create(null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreate
|
||||
* @param $plan Plan
|
||||
* @return Plan
|
||||
*/
|
||||
public function testGet($plan)
|
||||
{
|
||||
$result = Plan::get($plan->getId(), null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
$this->assertEquals($plan->getId(), $result->getId());
|
||||
$this->assertEquals($plan, $result, "", 0, 10, true);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGet
|
||||
* @param $plan Plan
|
||||
*/
|
||||
public function testGetList($plan)
|
||||
{
|
||||
$result = Plan::all(array('page_size' => '20', 'total_required' => 'yes'), null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
$totalPages = $result->getTotalPages();
|
||||
$found = false;
|
||||
$foundObject = null;
|
||||
do {
|
||||
foreach ($result->getPlans() as $obj) {
|
||||
if ($obj->getId() == $plan->getId()) {
|
||||
$found = true;
|
||||
$foundObject = $obj;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
$result = Plan::all(array('page' => --$totalPages, 'page_size' => '20', 'total_required' => 'yes'), null, $this->mockPPRestCall);
|
||||
|
||||
}
|
||||
} while ($totalPages > 0 && $found == false);
|
||||
$this->assertTrue($found, "The Created Plan was not found in the get list");
|
||||
$this->assertEquals($plan->getId(), $foundObject->getId());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGet
|
||||
* @param $plan Plan
|
||||
*/
|
||||
public function testUpdateChangingMerchantPreferences($plan)
|
||||
{
|
||||
/** @var Patch[] $request */
|
||||
$request = $this->operation['request']['body'][0];
|
||||
$patch = new Patch();
|
||||
$patch->setOp($request['op']);
|
||||
$patch->setPath($request['path']);
|
||||
$patch->setValue($request['value']);
|
||||
$patches = array();
|
||||
$patches[] = $patch;
|
||||
$patchRequest = new PatchRequest();
|
||||
$patchRequest->setPatches($patches);
|
||||
$result = $plan->update($patchRequest, null, $this->mockPPRestCall);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGet
|
||||
* @param $plan Plan
|
||||
*/
|
||||
public function testUpdateChangingPD($plan)
|
||||
{
|
||||
/** @var Patch[] $request */
|
||||
$request = $this->operation['request']['body'][0];
|
||||
$patch = new Patch();
|
||||
$patch->setOp($request['op']);
|
||||
$paymentDefinitions = $plan->getPaymentDefinitions();
|
||||
$patch->setPath('/payment-definitions/' . $paymentDefinitions[0]->getId());
|
||||
$patch->setValue($request['value']);
|
||||
$patches = array();
|
||||
$patches[] = $patch;
|
||||
$patchRequest = new PatchRequest();
|
||||
$patchRequest->setPatches($patches);
|
||||
$result = $plan->update($patchRequest, null, $this->mockPPRestCall);
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGet
|
||||
* @param $plan Plan
|
||||
* @return Plan
|
||||
*/
|
||||
public function testUpdateChangingState($plan)
|
||||
{
|
||||
/** @var Patch[] $request */
|
||||
$request = $this->operation['request']['body'][0];
|
||||
$patch = new Patch();
|
||||
$patch->setOp($request['op']);
|
||||
$patch->setPath($request['path']);
|
||||
$patch->setValue($request['value']);
|
||||
$patches = array();
|
||||
$patches[] = $patch;
|
||||
$patchRequest = new PatchRequest();
|
||||
$patchRequest->setPatches($patches);
|
||||
$result = $plan->update($patchRequest, null, $this->mockPPRestCall);
|
||||
$this->assertTrue($result);
|
||||
return Plan::get($plan->getId(), null, $this->mockPPRestCall);
|
||||
}
|
||||
}
|
||||
146
tests/PayPal/Test/Functional/Api/PaymentsFunctionalTest.php
Normal file
146
tests/PayPal/Test/Functional/Api/PaymentsFunctionalTest.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Test\Functional\Api;
|
||||
|
||||
use PayPal\Api\Amount;
|
||||
use PayPal\Api\Patch;
|
||||
use PayPal\Api\PatchRequest;
|
||||
use PayPal\Api\Payment;
|
||||
use PayPal\Api\PaymentExecution;
|
||||
use PayPal\Api\Refund;
|
||||
use PayPal\Api\Sale;
|
||||
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 PaymentsFunctionalTest 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 Payment($request);
|
||||
$result = $obj->create(null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function testCreateWallet()
|
||||
{
|
||||
$request = $this->operation['request']['body'];
|
||||
$obj = new Payment($request);
|
||||
$result = $obj->create(null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCreate
|
||||
* @param $payment Payment
|
||||
* @return Payment
|
||||
*/
|
||||
public function testGet($payment)
|
||||
{
|
||||
$result = Payment::get($payment->getId(), null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
$this->assertEquals($payment->getId(), $result->getId());
|
||||
$this->assertEquals($payment, $result, "", 0, 10, true);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGet
|
||||
* @param $payment Payment
|
||||
* @return Sale
|
||||
*/
|
||||
public function testGetSale($payment)
|
||||
{
|
||||
$transactions = $payment->getTransactions();
|
||||
$transaction = $transactions[0];
|
||||
$relatedResources = $transaction->getRelatedResources();
|
||||
$resource = $relatedResources[0];
|
||||
$result = Sale::get($resource->getSale()->getId(), null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
$this->assertEquals($resource->getSale()->getId(), $result->getId());
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGetSale
|
||||
* @param $sale Sale
|
||||
* @return Sale
|
||||
*/
|
||||
public function testRefundSale($sale)
|
||||
{
|
||||
$refund = new Refund($this->operation['request']['body']);
|
||||
$result = $sale->refund($refund, null, $this->mockPPRestCall);
|
||||
$this->assertNotNull($result);
|
||||
$this->assertEquals('completed', $result->getState());
|
||||
$this->assertEquals($sale->getId(), $result->getSaleId());
|
||||
$this->assertEquals($sale->getParentPayment(), $result->getParentPayment());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGet
|
||||
* @param $payment Payment
|
||||
* @return Payment
|
||||
*/
|
||||
public function testExecute($payment)
|
||||
{
|
||||
if ($this->mode == 'sandbox') {
|
||||
$this->markTestSkipped('Not executable on sandbox environment. Needs human interaction');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Test\Api;
|
||||
namespace PayPal\Test\Functional\Api;
|
||||
|
||||
use PayPal\Api\Patch;
|
||||
use PayPal\Common\PPModel;
|
||||
Reference in New Issue
Block a user