-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement Confirm Order Payment API. * Add helper method to set Experience Context as suggested in #554.
- Loading branch information
Showing
8 changed files
with
382 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Srmklive\PayPal\Traits\PayPalAPI\Orders; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Support\Str; | ||
use Throwable; | ||
|
||
trait Helpers | ||
{ | ||
/** | ||
* Confirm payment for an order. | ||
* | ||
* @param string $order_id | ||
* @param string $processing_instruction | ||
* | ||
* @throws Throwable | ||
* | ||
* @return array|\Psr\Http\Message\StreamInterface|string | ||
*/ | ||
public function setupOrderConfirmation(string $order_id, string $processing_instruction = '') | ||
{ | ||
$body = [ | ||
'processing_instruction' => $processing_instruction, | ||
'application_context' => $this->experience_context, | ||
'payment_source' => $this->payment_source, | ||
]; | ||
|
||
return $this->confirmOrder($order_id, $body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Srmklive\PayPal\Traits; | ||
|
||
trait PayPalExperienceContext | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $experience_context = []; | ||
|
||
/** | ||
* Set Brand Name when setting experience context for payment. | ||
* @param string $brand | ||
* | ||
* @return \Srmklive\PayPal\Services\PayPal | ||
*/ | ||
public function setBrandName(string $brand): \Srmklive\PayPal\Services\PayPal | ||
{ | ||
$this->experience_context = array_merge($this->experience_context, [ | ||
'brand_name' => $brand, | ||
]); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set return & cancel urls. | ||
* | ||
* @param string $return_url | ||
* @param string $cancel_url | ||
* | ||
* @return \Srmklive\PayPal\Services\PayPal | ||
*/ | ||
public function setReturnAndCancelUrl(string $return_url, string $cancel_url): \Srmklive\PayPal\Services\PayPal | ||
{ | ||
$this->experience_context = array_merge($this->experience_context, [ | ||
'return_url' => $return_url, | ||
'cancel_url' => $cancel_url, | ||
]); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set stored payment source. | ||
* | ||
* @param string $initiator | ||
* @param string $type | ||
* @param string $usage | ||
* @param bool $previous_reference | ||
* @param string|null $previous_transaction_id | ||
* @param string|null $previous_transaction_date | ||
* @param string|null $previous_transaction_reference_number | ||
* @param string|null $previous_transaction_network | ||
* | ||
* @return \Srmklive\PayPal\Services\PayPal | ||
*/ | ||
public function setStoredPaymentSource(string $initiator, string $type, string $usage, bool $previous_reference = false, string $previous_transaction_id = null, string $previous_transaction_date = null, string $previous_transaction_reference_number = null, string $previous_transaction_network = null): \Srmklive\PayPal\Services\PayPal | ||
{ | ||
$this->experience_context = array_merge($this->experience_context, [ | ||
'stored_payment_source' => [ | ||
'payment_initiator' => $initiator, | ||
'payment_type' => $type, | ||
'usage' => $usage, | ||
], | ||
]); | ||
|
||
if ($previous_reference === true) { | ||
$this->experience_context['stored_payment_source']['previous_network_transaction_reference'] = [ | ||
'id' => $previous_transaction_id, | ||
'date' => $previous_transaction_date, | ||
'acquirer_reference_number' => $previous_transaction_reference_number, | ||
'network' => $previous_transaction_network, | ||
]; | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Feature; | ||
|
||
namespace Srmklive\PayPal\Tests\Feature; | ||
|
||
use Carbon\Carbon; | ||
use PHPUnit\Framework\TestCase; | ||
use Srmklive\PayPal\Services\PayPal as PayPalClient; | ||
use Srmklive\PayPal\Tests\MockClientClasses; | ||
use Srmklive\PayPal\Tests\MockRequestPayloads; | ||
use Srmklive\PayPal\Tests\MockResponsePayloads; | ||
|
||
class AdapterExperienceContextTest extends TestCase | ||
{ | ||
use MockClientClasses; | ||
use MockRequestPayloads; | ||
use MockResponsePayloads; | ||
|
||
/** @var string */ | ||
protected static $access_token = ''; | ||
|
||
/** @var \Srmklive\PayPal\Services\PayPal */ | ||
protected $client; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->client = new PayPalClient($this->getApiCredentials()); | ||
|
||
$this->client->setClient( | ||
$this->mock_http_client( | ||
$this->mockAccessTokenResponse() | ||
) | ||
); | ||
$response = $this->client->getAccessToken(); | ||
|
||
self::$access_token = $response['access_token']; | ||
|
||
parent::setUp(); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_set_payment_experience_context_before_performing_api_call() | ||
{ | ||
$this->client->setAccessToken([ | ||
'access_token' => self::$access_token, | ||
'token_type' => 'Bearer', | ||
]); | ||
|
||
$start_date = Carbon::now()->addDay()->toDateString(); | ||
|
||
$this->client = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel') | ||
->setBrandName('Test Brand') | ||
->addProductById('PROD-XYAB12ABSB7868434') | ||
->addBillingPlanById('P-5ML4271244454362WXNWU5NQ'); | ||
|
||
$this->client->setClient( | ||
$this->mock_http_client( | ||
$this->mockCreateSubscriptionResponse() | ||
) | ||
); | ||
|
||
$response = $this->client->setupSubscription('John Doe', '[email protected]', $start_date); | ||
|
||
$this->assertNotEmpty($response); | ||
$this->assertArrayHasKey('id', $response); | ||
$this->assertArrayHasKey('plan_id', $response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Srmklive\PayPal\Tests\Feature; | ||
|
||
use Carbon\Carbon; | ||
use PHPUnit\Framework\TestCase; | ||
use Srmklive\PayPal\Services\PayPal as PayPalClient; | ||
use Srmklive\PayPal\Tests\MockClientClasses; | ||
use Srmklive\PayPal\Tests\MockRequestPayloads; | ||
use Srmklive\PayPal\Tests\MockResponsePayloads; | ||
|
||
class AdapterOrdersHelperTest extends TestCase | ||
{ | ||
use MockClientClasses; | ||
use MockRequestPayloads; | ||
use MockResponsePayloads; | ||
|
||
/** @var string */ | ||
protected static $access_token = ''; | ||
|
||
/** @var \Srmklive\PayPal\Services\PayPal */ | ||
protected $client; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->client = new PayPalClient($this->getApiCredentials()); | ||
|
||
$this->client->setClient( | ||
$this->mock_http_client( | ||
$this->mockAccessTokenResponse() | ||
) | ||
); | ||
$response = $this->client->getAccessToken(); | ||
|
||
self::$access_token = $response['access_token']; | ||
|
||
parent::setUp(); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_confirm_payment_for_an_order() | ||
{ | ||
$this->client->setAccessToken([ | ||
'access_token' => self::$access_token, | ||
'token_type' => 'Bearer', | ||
]); | ||
|
||
$start_date = Carbon::now()->subDays(10)->toDateString(); | ||
|
||
$this->client = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel') | ||
->setBrandName('Test Brand') | ||
->setStoredPaymentSource( | ||
'MERCHANT', | ||
'RECURRING', | ||
'SUBSEQUENT', | ||
true, | ||
'5TY05013RG002845M', | ||
$start_date, | ||
'Invoice-005', | ||
'VISA' | ||
); | ||
|
||
$this->client->setClient( | ||
$this->mock_http_client( | ||
$this->mockConfirmOrderResponse() | ||
) | ||
); | ||
|
||
$response = $this->client->setupOrderConfirmation('5O190127TN364715T', 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL'); | ||
|
||
$this->assertNotEmpty($response); | ||
$this->assertArrayHasKey('id', $response); | ||
} | ||
} |
Oops, something went wrong.