diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a5bb20..8a07181 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,12 @@ All notable changes to `laravel-paymongo` will be documented in this file -## 2.4.0 (2022-12-15) +## 2.4.0 (2023-04-30) + +### Added +- Checkouts API + +## 2.3.0 (2022-12-15) ### Added - Links API diff --git a/docs/docs/Usage/checkouts.md b/docs/docs/Usage/checkouts.md new file mode 100644 index 0000000..40a8299 --- /dev/null +++ b/docs/docs/Usage/checkouts.md @@ -0,0 +1,82 @@ +--- +sidebar_position: 10 +slug: /checkout-sessions +id: checkout-sessions +--- + +# Checkouts + +## Create Checkout + +Creates a checkout session. A checkout session is a customizable checkout page from Paymongo. + +### Payload + +Refer to [Paymongo documentation](https://developers.paymongo.com/reference/checkout-session-resource) for payload guidelines. + +### Sample + +```php +use Luigel\Paymongo\Facades\Paymongo; + +$checkout = Paymongo::checkout()->create([ + 'cancel_url' => 'https://paymongo.rigelkentcarbonel.com/', + 'billing' => [ + 'name' => 'Juan Doe', + 'email' => 'juan@doe.com', + 'phone' => '+639123456789', + ], + 'description' => 'My checkout session description', + 'line_items' => [ + [ + 'amount' => 10000, + 'currency' => 'PHP', + 'description' => 'Something of a product.', + 'images' => [ + 'https://images.unsplash.com/photo-1613243555988-441166d4d6fd?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80' + ], + 'name' => 'A payment card', + 'quantity' => 1 + ] + ], + 'payment_method_types' => [ + 'atome', + 'billease', + 'card', + 'dob', + 'dob_ubp', + 'gcash', + 'grab_pay', + 'paymaya' + ], + 'success_url' => 'https://paymongo.rigelkentcarbonel.com/', + 'statement_descriptor' => 'Laravel Paymongo Library', + 'metadata' => [ + 'Key' => 'Value' + ] +]); +``` + +## Get Checkout + +Retrieve a checkout session by passing the id to the `find($id)` method. + +### Sample + +```php +use Luigel\Paymongo\Facades\Paymongo; + +$checkout = Paymongo::checkout()->find('cs_CbFCTDfxvMFNjwjVi26Uzhtj'); +``` + +## Expire Checkout + +Expire a checkout session by using the `find($id)` method and chaining the `->expire()` method. + +### Sample + +```php +use Luigel\Paymongo\Facades\Paymongo; + +$checkout = Paymongo::checkout()->find('cs_CbFCTDfxvMFNjwjVi26Uzhtj')->expire(); +``` \ No newline at end of file diff --git a/docs/docs/Usage/links.md b/docs/docs/Usage/links.md index dbffc53..c32c9e2 100644 --- a/docs/docs/Usage/links.md +++ b/docs/docs/Usage/links.md @@ -1,5 +1,5 @@ --- -sidebar_position: 7 +sidebar_position: 8 slug: /links id: links --- diff --git a/docs/docs/Usage/refunds.md b/docs/docs/Usage/refunds.md index 8163524..1847904 100644 --- a/docs/docs/Usage/refunds.md +++ b/docs/docs/Usage/refunds.md @@ -1,5 +1,5 @@ --- -sidebar_position: 7 +sidebar_position: 9 slug: /refunds id: refunds --- diff --git a/src/Models/Checkout.php b/src/Models/Checkout.php new file mode 100644 index 0000000..d3e5c13 --- /dev/null +++ b/src/Models/Checkout.php @@ -0,0 +1,13 @@ +checkout()->expireCheckout($this); + } +} diff --git a/src/Paymongo.php b/src/Paymongo.php index 4c2321f..f313205 100644 --- a/src/Paymongo.php +++ b/src/Paymongo.php @@ -2,17 +2,17 @@ namespace Luigel\Paymongo; -use Luigel\Paymongo\Models\Customer; use Luigel\Paymongo\Models\Link; -use Luigel\Paymongo\Models\Payment; -use Luigel\Paymongo\Models\PaymentIntent; -use Luigel\Paymongo\Models\PaymentMethod; use Luigel\Paymongo\Models\Refund; use Luigel\Paymongo\Models\Source; -use Luigel\Paymongo\Models\Token; +use Luigel\Paymongo\Models\Payment; use Luigel\Paymongo\Models\Webhook; -use Luigel\Paymongo\Traits\HasToggleWebhook; use Luigel\Paymongo\Traits\Request; +use Luigel\Paymongo\Models\Checkout; +use Luigel\Paymongo\Models\Customer; +use Luigel\Paymongo\Models\PaymentIntent; +use Luigel\Paymongo\Models\PaymentMethod; +use Luigel\Paymongo\Traits\HasToggleWebhook; class Paymongo { @@ -32,6 +32,7 @@ class Paymongo protected const ENDPOINT_REFUND = 'refunds/'; protected const ENDPOINT_LINK = 'links/'; protected const ENDPOINT_CUSTOMER = 'customers/'; + protected const ENDPOINT_CHECKOUT = 'checkout_sessions/'; public const SOURCE_GCASH = 'gcash'; public const SOURCE_GRAB_PAY = 'grab_pay'; public const AMOUNT_TYPE_FLOAT = 'float'; @@ -128,4 +129,12 @@ public function customer(): self return $this; } + + public function checkout(): self + { + $this->apiUrl = self::BASE_API.self::ENDPOINT_CHECKOUT; + $this->returnModel = Checkout::class; + + return $this; + } } diff --git a/src/Traits/Request.php b/src/Traits/Request.php index d156eb3..80e120b 100644 --- a/src/Traits/Request.php +++ b/src/Traits/Request.php @@ -4,18 +4,19 @@ use Exception; use GuzzleHttp\Client; -use GuzzleHttp\Exception\ClientException; +use Luigel\Paymongo\Models\Link; use Illuminate\Support\Collection; -use Luigel\Paymongo\Exceptions\AmountTypeNotSupportedException; -use Luigel\Paymongo\Exceptions\BadRequestException; +use Luigel\Paymongo\Models\Webhook; +use Luigel\Paymongo\Models\Checkout; +use Luigel\Paymongo\Models\Customer; +use Luigel\Paymongo\Models\BaseModel; +use GuzzleHttp\Exception\ClientException; +use Luigel\Paymongo\Models\PaymentIntent; use Luigel\Paymongo\Exceptions\NotFoundException; +use Luigel\Paymongo\Exceptions\BadRequestException; use Luigel\Paymongo\Exceptions\PaymentErrorException; use Luigel\Paymongo\Exceptions\UnauthorizedException; -use Luigel\Paymongo\Models\BaseModel; -use Luigel\Paymongo\Models\Customer; -use Luigel\Paymongo\Models\Link; -use Luigel\Paymongo\Models\PaymentIntent; -use Luigel\Paymongo\Models\Webhook; +use Luigel\Paymongo\Exceptions\AmountTypeNotSupportedException; trait Request { @@ -238,6 +239,23 @@ public function getPaymentMethods(Customer $customer) return $this->request(); } + /** + * + */ + public function expireCheckout(Checkout $checkout) { + $this->method = 'POST'; + $this->apiUrl = $this->apiUrl.$checkout->id.'/expire'; + + $this->setOptions([ + 'headers' => [ + 'Accept' => 'application/json', + ], + 'auth' => [config('paymongo.secret_key'), ''], + ]); + + return $this->request(); + } + /** * Send request to API. * diff --git a/tests/CheckoutTest.php b/tests/CheckoutTest.php new file mode 100644 index 0000000..51dbb6f --- /dev/null +++ b/tests/CheckoutTest.php @@ -0,0 +1,35 @@ +toBeInstanceOf(Checkout::class); +}); + +it('can not retrieve a checkout session with invalid id', function () { + $this->expectException(NotFoundException::class); + + Paymongo::checkout() + ->find('test'); +}); + +it('can retrieve a checkout session by id', function () { + $checkout = createCheckout(); + + $retrieve = Paymongo::checkout() + ->find($checkout->id); + + expect($checkout->id)->toBe($retrieve->id); +}); + +it('can expire a checkout session', function () { + $checkout = createCheckout(); + + $checkout = $checkout->expire(); + + expect($checkout->status)->toBe('expired'); +}); diff --git a/tests/PaymentTest.php b/tests/PaymentTest.php index 648ac10..f3e34a5 100644 --- a/tests/PaymentTest.php +++ b/tests/PaymentTest.php @@ -36,4 +36,4 @@ ->currency->toBe('PHP') ->statement_descriptor->toBe('LUIGEL STORE') ->status->toBe('paid'); -}); +}); \ No newline at end of file diff --git a/tests/Pest.php b/tests/Pest.php index adfa3dc..a887100 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,16 +1,17 @@ in(__DIR__); @@ -160,6 +161,46 @@ function createCustomer(): Customer ]); } +function createCheckout(): Checkout +{ + return Paymongo::checkout()->create([ + 'cancel_url' => 'https://paymongo.rigelkentcarbonel.com/', + 'billing' => [ + 'name' => 'Gringiemar Felix', + 'email' => 'gringiemar@felix.com', + 'phone' => '+6391234'.rand(10000, 99999), + ], + 'description' => 'My checkout session description', + 'line_items' => [ + [ + 'amount' => 10000, + 'currency' => 'PHP', + 'description' => 'Something of a product.', + 'images' => [ + 'https://images.unsplash.com/photo-1613243555988-441166d4d6fd?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80' + ], + 'name' => 'A payment card', + 'quantity' => 1 + ] + ], + 'payment_method_types' => [ + 'atome', + 'billease', + 'card', + 'dob', + 'dob_ubp', + 'gcash', + 'grab_pay', + 'paymaya' + ], + 'success_url' => 'https://paymongo.rigelkentcarbonel.com/', + 'statement_descriptor' => 'Laravel Paymongo Library', + 'metadata' => [ + 'Key' => 'Value' + ] + ]); +} + function createRequest( $method, $content,