<?php
namespace Srmklive\PayPal\Tests\Unit\Client;
use GuzzleHttp\Utils;
use PHPUnit\Framework\TestCase;
use Srmklive\PayPal\Tests\MockClientClasses;
use Srmklive\PayPal\Tests\MockRequestPayloads;
use Srmklive\PayPal\Tests\MockResponsePayloads;
class SubscriptionsTest extends TestCase
{
use MockClientClasses;
use MockRequestPayloads;
use MockResponsePayloads;
/** @test */
public function it_can_create_a_subscription()
{
$expectedResponse = $this->mockCreateSubscriptionResponse();
$expectedEndpoint = 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions';
$expectedParams = [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization' => 'Bearer some-token',
],
'json' => $this->mockCreateSubscriptionParams(),
];
$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$this->assertEquals($expectedResponse, Utils::jsonDecode($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
/** @test */
public function it_can_update_a_subscription()
{
$expectedResponse = '';
$expectedEndpoint = 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G';
$expectedParams = [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization' => 'Bearer some-token',
],
'json' => $this->mockUpdateSubscriptionParams(),
];
$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'patch');
$this->assertEquals($expectedResponse, Utils::jsonDecode($mockHttpClient->patch($expectedEndpoint, $expectedParams)->getBody(), true));
}
/** @test */
public function it_can_show_details_for_a_subscription()
{
$expectedResponse = $this->mockGetSubscriptionDetailsResponse();
$expectedEndpoint = 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G';
$expectedParams = [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization' => 'Bearer some-token',
],
];
$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'get');
$this->assertEquals($expectedResponse, Utils::jsonDecode($mockHttpClient->get($expectedEndpoint, $expectedParams)->getBody(), true));
}
/** @test */
public function it_can_activate_a_subscription()
{
$expectedResponse = '';
$expectedEndpoint = 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G/activate';
$expectedParams = [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization' => 'Bearer some-token',
],
'json' => $this->mockActivateSubscriptionParams(),
];
$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$this->assertEquals($expectedResponse, Utils::jsonDecode($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
/** @test */
public function it_can_cancel_a_subscription()
{
$expectedResponse = '';
$expectedEndpoint = 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G/cancel';
$expectedParams = [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization' => 'Bearer some-token',
],
'json' => $this->mockCancelSubscriptionParams(),
];
$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$this->assertEquals($expectedResponse, Utils::jsonDecode($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
/** @test */
public function it_can_suspend_a_subscription()
{
$expectedResponse = '';
$expectedEndpoint = 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G/suspend';
$expectedParams = [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization' => 'Bearer some-token',
],
'json' => $this->mockSuspendSubscriptionParams(),
];
$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$this->assertEquals($expectedResponse, Utils::jsonDecode($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
/** @test */
public function it_can_capture_payment_for_a_subscription()
{
$expectedResponse = '';
$expectedEndpoint = 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G/capture';
$expectedParams = [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization' => 'Bearer some-token',
],
'json' => $this->mockCaptureSubscriptionPaymentParams(),
];
$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$this->assertEquals($expectedResponse, Utils::jsonDecode($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
/** @test */
public function it_can_update_quantity_or_product_for_a_subscription()
{
$expectedResponse = $this->mockUpdateSubscriptionItemsResponse();
$expectedEndpoint = 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G/revise';
$expectedParams = [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization' => 'Bearer some-token',
],
'json' => $this->mockUpdateSubscriptionItemsParams(),
];
$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'post');
$this->assertEquals($expectedResponse, Utils::jsonDecode($mockHttpClient->post($expectedEndpoint, $expectedParams)->getBody(), true));
}
/** @test */
public function it_can_list_transactions_for_a_subscription()
{
$expectedResponse = $this->mockListSubscriptionTransactionsResponse();
$expectedEndpoint = 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions/I-BW452GLLEP1G/transactions?start_time=2018-01-21T07:50:20.940Z&end_time=2018-08-21T07:50:20.940Z';
$expectedParams = [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization' => 'Bearer some-token',
],
];
$mockHttpClient = $this->mock_http_request(Utils::jsonEncode($expectedResponse), $expectedEndpoint, $expectedParams, 'get');
$this->assertEquals($expectedResponse, Utils::jsonDecode($mockHttpClient->get($expectedEndpoint, $expectedParams)->getBody(), true));
}
}
Initiates a new payment transaction.
create-order
| Parameter | Type | Details |
|---|---|---|
| amount | decimal | Your Amount , Must be rounded at 2 precision. |
| currency | string | Currency Code, Must be in Upper Case (Alpha-3 code) |
| success_url | string | Enter your return or success URL |
| cancel_url | string (optional) | Enter your cancel or failed URL |
Request Example (guzzle)
<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $base_url.'create-order', [
'headers' => [
'Authorization' => 'Bearer '. $authorizationToken,
'accept' => 'application/json',
'content-type' => 'application/json',
],
'form_params' => [
'amount' => '$amount',
'currency' => 'currency',
'success_url' => 'success_url',
'cancel_url' => 'cancel_url',
],
]);
echo $response->getBody();
**Response: SUCCESS (200 OK)**
{
"message": {
"success": [
"Order created successfully."
]
},
"data": {
"redirect_url":"https://example.com/login/OISADFDFSDFSF",
"order_details":{
"amount" : "10",
"fixed_charge" : 2,
"percent_charge" : 1,
"total_charge" : 3,
"total_payable" : 13,
"currency" : "USD",
"expiry_time": "2024-04-25T06:48:35.984285Z",
"success_url": "http://127.0.0.1/nfcpay/user/transaction/success",
"cancel_url": "http://127.0.0.1/nfcpay/user/transaction/cancel"
}
},
"type": "success"
}
**Response: ERROR (400 FAILED)**
{
"message": {
"error": [
"Invalid token."
]
},
"data": null,
"type": "error"
}