Skip to content
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to `laravel-paymongo` will be documented in this file

## 2.4.0 (2022-12-15)

### Added
- Links API
- Customers API

### Fixed
- Failing tests

## 1.3.0 (2020-10-31)

### Added
Expand Down
85 changes: 85 additions & 0 deletions docs/docs/Usage/customers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
sidebar_position: 7
slug: /customers
id: customers
---

# Customers

## Create Customer

Creates a customer record that holds billing information and is useful for card vaulting purposes.

### Payload

Refer to [Paymongo documentation](https://developers.paymongo.com/reference/customer-resource) for payload guidelines.

### Sample

```php
use Luigel\Paymongo\Facades\Paymongo;

$customer = Paymongo::customer()->create([
'first_name' => 'Juan',
'last_name' => 'Doe',
'phone' => '+639123456789',
'email' => 'customer@email.com',
'default_device' => 'phone'
]);
```

## Get Customer

Retrieve a customer by passing the customer id to the `find($id)` method.

### Sample

```php
use Luigel\Paymongo\Facades\Paymongo;

$customer = Paymongo::customer()->find('cus_b9ENKVqcHBfQQmv26uDYDCsD');
```

## Edit Customer

Edit a customer by using the `find($id)` method and chaining the `->update()` method. Or by chaning `->update()` to your existing instance.

### Sample

```php
use Luigel\Paymongo\Facades\Paymongo;

$customer = Paymongo::customer()
->find('cus_b9ENKVqcHBfQQmv26uDYDCsD')
->update([
'first_name' => 'Jane'
]);
```

## Delete Customer

Delete a customer by using the `find($id)` method and chaining the `->delete()` method. Or by chaning `->delete()` to your existing instance.

### Sample

```php
use Luigel\Paymongo\Facades\Paymongo;

$link = Paymongo::customer()
->find('cus_b9ENKVqcHBfQQmv26uDYDCsD')
->delete();
```

## Retrieve Customer's Payment Methods

Retrieve the customer's payment methods by using the `find($id)` method and chaining the `->paymentMethods()` method. Or by chaning `->paymentMethods()` to your existing instance.

### Sample

```php
use Luigel\Paymongo\Facades\Paymongo;

$link = Paymongo::customer()
->find('cus_b9ENKVqcHBfQQmv26uDYDCsD')
->paymentMethods();
```
65 changes: 65 additions & 0 deletions docs/docs/Usage/links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
sidebar_position: 7
slug: /links
id: links
---

# Links

## Create Link

Creates a payment link. A payment link that can be used for one-time payments.

### Payload

Refer to [Paymongo documentation](https://developers.paymongo.com/reference/links-resource) for payload guidelines.

### Sample

```php
use Luigel\Paymongo\Facades\Paymongo;

$link = Paymongo::link()->create([
'amount' => 100.00,
'description' => 'Link Test',
'remarks' => 'laravel-paymongo'
]);
```

## Get Link

Retrieve a payment link by passing the id or the reference number to the `find($id)` method.

### Sample

```php
use Luigel\Paymongo\Facades\Paymongo;

$link = Paymongo::link()->find('link_wWaibr22CzEnficNhQNPUdoo');
$linkbyReference = Paymongo::link()->find('WTmSJbV');
```

## Archive Link

Archive a payment link by using the `find($id)` method and chaining the `->archive()` method.

### Sample

```php
use Luigel\Paymongo\Facades\Paymongo;

$link = Paymongo::link()->find('link_wWaibr22CzEnficNhQNPUdoo')->archive();
```

## Unarchive Link

Unarchive a payment link by using the `find($id)` method and chaining the `->unarchive()` method.

### Sample

```php
use Luigel\Paymongo\Facades\Paymongo;

$link = Paymongo::link()->find('link_wWaibr22CzEnficNhQNPUdoo')->unarchive();
```

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: 8
sidebar_position: 7
slug: /refunds
id: refunds
---
Expand Down
24 changes: 24 additions & 0 deletions src/Models/Customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Luigel\Paymongo\Models;

use Luigel\Paymongo\Paymongo;
use Illuminate\Support\Collection;

class Customer extends BaseModel
{
public function update(array $payload): BaseModel
{
return (new Paymongo)->customer()->updateCustomer($this, $payload);
}

public function delete(): BaseModel
{
return (new Paymongo)->customer()->deleteCustomer($this);
}

public function paymentMethods(): BaseModel|Collection
{
return (new Paymongo)->customer()->getPaymentMethods($this);
}
}
18 changes: 18 additions & 0 deletions src/Models/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Luigel\Paymongo\Models;

use Luigel\Paymongo\Paymongo;

class Link extends BaseModel
{
public function archive(): BaseModel
{
return (new Paymongo)->link()->archive($this);
}

public function unarchive(): BaseModel
{
return (new Paymongo)->link()->unarchive($this);
}
}
30 changes: 25 additions & 5 deletions src/Paymongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace Luigel\Paymongo;

use Luigel\Paymongo\Models\Payment;
use Luigel\Paymongo\Models\PaymentIntent;
use Luigel\Paymongo\Models\PaymentMethod;
use Luigel\Paymongo\Models\Link;
use Luigel\Paymongo\Models\Token;
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\Customer;
use Luigel\Paymongo\Models\PaymentIntent;
use Luigel\Paymongo\Models\PaymentMethod;
use Luigel\Paymongo\Traits\HasToggleWebhook;

class Paymongo
{
Expand All @@ -28,6 +30,8 @@ class Paymongo
protected const ENDPOINT_PAYMENT = 'payments/';
protected const ENDPOINT_TOKEN = 'tokens/';
protected const ENDPOINT_REFUND = 'refunds/';
protected const ENDPOINT_LINK = 'links/';
protected const ENDPOINT_CUSTOMER = 'customers/';
public const SOURCE_GCASH = 'gcash';
public const SOURCE_GRAB_PAY = 'grab_pay';
public const AMOUNT_TYPE_FLOAT = 'float';
Expand Down Expand Up @@ -108,4 +112,20 @@ public function refund(): self

return $this;
}

public function link(): self
{
$this->apiUrl = self::BASE_API.self::ENDPOINT_LINK;
$this->returnModel = Link::class;

return $this;
}

public function customer(): self
{
$this->apiUrl = self::BASE_API.self::ENDPOINT_CUSTOMER;
$this->returnModel = Customer::class;

return $this;
}
}
102 changes: 96 additions & 6 deletions src/Traits/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

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\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\PaymentIntent;
use Luigel\Paymongo\Models\Webhook;
use Luigel\Paymongo\Exceptions\AmountTypeNotSupportedException;

trait Request
{
Expand Down Expand Up @@ -143,6 +145,94 @@ public function attach(PaymentIntent $intent, string $paymentMethodId, string|nu
return $this->request();
}

/**
* Archives the link
*/
public function archive(Link $link){
$this->method = 'POST';
$this->apiUrl = $this->apiUrl.$link->id.'/archive';

$this->setOptions([
'headers' => [
'Accept' => 'application/json',
],
'auth' => [config('paymongo.secret_key'), ''],
]);

return $this->request();
}

/**
* Unarchives the link
*/
public function unarchive(Link $link){
$this->method = 'POST';
$this->apiUrl = $this->apiUrl . $link->id . '/unarchive';

$this->setOptions([
'headers' => [
'Accept' => 'application/json',
],
'auth' => [config('paymongo.secret_key'), ''],
]);

return $this->request();
}

/**
* Update the customer information
*/
public function updateCustomer(Customer $customer, array $payload){
$this->method = 'PATCH';
$this->apiUrl = $this->apiUrl . $customer->id;
$this->payload = $payload;

$this->formRequestData();
$this->setOptions([
'headers' => [
'Accept' => 'application/json',
],
'json' => $this->data,
'auth' => [config('paymongo.secret_key'), ''],
]);

return $this->request();
}

/**
* Delete the customer
*/
public function deleteCustomer(Customer $customer){
$this->method = 'DELETE';
$this->apiUrl = $this->apiUrl . $customer->id;

$this->setOptions([
'headers' => [
'Accept' => 'application/json',
],
'auth' => [config('paymongo.secret_key'), ''],
]);

return $this->request();
}

/**
* Get Customer's Payment Methods
*/
public function getPaymentMethods(Customer $customer){
$this->method = 'GET';
$this->apiUrl = $this->apiUrl . $customer->id . '/payment_methods';

$this->setOptions([
'headers' => [
'Accept' => 'application/json',
],
'auth' => [config('paymongo.secret_key'), ''],
]);

return $this->request();
}

/**
* Send request to API.
*
Expand Down
Loading