Skip to content

Added: Checkouts API #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 82 additions & 0 deletions docs/docs/Usage/checkouts.md
Original file line number Diff line number Diff line change
@@ -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();
```
2 changes: 1 addition & 1 deletion docs/docs/Usage/links.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 7
sidebar_position: 8
slug: /links
id: links
---
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/Usage/refunds.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 7
sidebar_position: 9
slug: /refunds
id: refunds
---
Expand Down
13 changes: 13 additions & 0 deletions src/Models/Checkout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Luigel\Paymongo\Models;

use Luigel\Paymongo\Paymongo;

class Checkout extends BaseModel
{
public function expire()
{
return (new Paymongo)->checkout()->expireCheckout($this);
}
}
21 changes: 15 additions & 6 deletions src/Paymongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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';
Expand Down Expand Up @@ -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;
}
}
34 changes: 26 additions & 8 deletions src/Traits/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.
*
Expand Down
35 changes: 35 additions & 0 deletions tests/CheckoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Luigel\Paymongo\Exceptions\NotFoundException;
use Luigel\Paymongo\Facades\Paymongo;
use Luigel\Paymongo\Models\Checkout;

it('can create a checkout session', function () {
$checkout = createCheckout();

expect($checkout)->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');
});
2 changes: 1 addition & 1 deletion tests/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
->currency->toBe('PHP')
->statement_descriptor->toBe('LUIGEL STORE')
->status->toBe('paid');
});
});
53 changes: 47 additions & 6 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php

use Illuminate\Support\Str;
use Luigel\Paymongo\Facades\Paymongo;
use Luigel\Paymongo\Models\Customer;
use Luigel\Paymongo\Models\Link;
use Luigel\Paymongo\Models\Token;
use Luigel\Paymongo\Models\Source;
use Luigel\Paymongo\Models\Payment;
use Luigel\Paymongo\Traits\Request;
use Luigel\Paymongo\Models\Checkout;
use Luigel\Paymongo\Models\Customer;
use Luigel\Paymongo\Facades\Paymongo;
use Luigel\Paymongo\Tests\BaseTestCase;
use Luigel\Paymongo\Models\PaymentIntent;
use Luigel\Paymongo\Models\PaymentMethod;
use Luigel\Paymongo\Models\Source;
use Luigel\Paymongo\Models\Token;
use Luigel\Paymongo\Tests\BaseTestCase;
use Luigel\Paymongo\Traits\Request;

uses(BaseTestCase::class, Request::class)
->in(__DIR__);
Expand Down Expand Up @@ -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,
Expand Down