Skip to content

Commit

Permalink
Merge branch 'feat/paratest' of github.com:lunarphp/lunar into feat/p…
Browse files Browse the repository at this point in the history
…aratest
  • Loading branch information
alecritson committed Sep 22, 2023
2 parents 4a17d35 + cccd4f3 commit 3015011
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 41 deletions.
83 changes: 48 additions & 35 deletions docs/core/extending/payments.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Overview

Lunar provides an easy way for you to add your own payment drivers, by default, there is a basic `OfflinePayment` driver that ships with Lunar, additional providers should be added to your Storefront via addons.
Lunar provides an easy way for you to add your own payment drivers, by default, there is a basic `OfflinePayment` driver
that ships with Lunar, additional providers should be added to your Storefront via addons.

Below is a list of available payment drivers.

Expand Down Expand Up @@ -85,7 +86,8 @@ class CustomPayment extends AbstractPayment
}
```

This is the most basic implementation of a driver, you can see we are extending an `AbstractPayment`. This is a class which is provided by Lunar and contains some useful helpers you can utilise in your own driver.
This is the most basic implementation of a driver, you can see we are extending an `AbstractPayment`. This is a class
which is provided by Lunar and contains some useful helpers you can utilise in your own driver.

[See available methods](#abstract-class-methods)

Expand All @@ -95,19 +97,25 @@ This is the most basic implementation of a driver, you can see we are extending
public function authorize();
```

This is where you'd check the payment details which have been passed in, create any transactions for the order and return the response.
This is where you'd check the payment details which have been passed in, create any transactions for the order and
return the response.

If you're not taking payment straight away, you should set any transactions to the type of `intent`. When you then later capture the payment, we would recommend creating another transaction that is related to that via the `parent_transaction_id`.
If you're not taking payment straight away, you should set any transactions to the type of `intent`. When you then later
capture the payment, we would recommend creating another transaction that is related to that via
the `parent_transaction_id`.

#### Capturing payments

```php
public function capture(Transaction $transaction, $amount = 0): PaymentCapture
```

When you have a transaction that has a type of `intent` the Staff member who is logged into the hub can then decide to capture it so the card used gets charged the amount that has been authorised.
When you have a transaction that has a type of `intent` the Staff member who is logged into the hub can then decide to
capture it so the card used gets charged the amount that has been authorised.

You can pass an optional amount, but be cautious as you generally cannot capture an amount that exceeds the original amount on the `intent` transaction. If you capture an amount less, services like Stripe will treat that as a partial refund and no further captures can take place on that order.
You can pass an optional amount, but be cautious as you generally cannot capture an amount that exceeds the original
amount on the `intent` transaction. If you capture an amount less, services like Stripe will treat that as a partial
refund and no further captures can take place on that order.

Here you should create an additional transaction against the order to show how much has been captured.

Expand All @@ -117,19 +125,20 @@ Here you should create an additional transaction against the order to show how m
public function refund(Transaction $transaction, int $amount = 0, $notes = null): PaymentRefund
```

When refunding a transaction, you can only do so to one that's been captured. If you need to refund an order that hasn't been captured you should instead capture an amount less to what's been authorised.
When refunding a transaction, you can only do so to one that's been captured. If you need to refund an order that hasn't
been captured you should instead capture an amount less to what's been authorised.

You should only refund transactions with the type `capture`.


<a name="abstract-class-methods"></a>

## The AbstractPayment class

### Available methods

- [`cart`](#cart)
- [`order`](#order)
- [`setData`](#setdata)
- [`withData`](#withData)
- [`setConfig`](#setconfig)

#### `cart`
Expand All @@ -138,7 +147,8 @@ You should only refund transactions with the type `capture`.
public function cart(Cart $cart): self
```

Sets the `$cart` property on the payment driver. When using the `release` method we recommend expecting a `$cart` instance and checking for the existence of an order.
Sets the `$cart` property on the payment driver. When using the `release` method we recommend expecting a `$cart`
instance and checking for the existence of an order.

#### `order`

Expand All @@ -154,7 +164,8 @@ Sets the `$order` property on the payment driver.
public function withData(array $data): self
```

This method allows you to add any additional data to the payment driver, this can be anything that the payment driver needs to function, for example.
This method allows you to add any additional data to the payment driver, this can be anything that the payment driver
needs to function, for example.

```php
Payments::driver('stripe')->withData([
Expand All @@ -168,44 +179,45 @@ Payments::driver('stripe')->withData([
public function setConfig(array $config): self
```

Here you can set up any additional config for this payment driver. By default, this will be called when you register your payment driver and will take any values which are set in `config/lunar/payments.php` for that type.

Here you can set up any additional config for this payment driver. By default, this will be called when you register
your payment driver and will take any values which are set in `config/lunar/payments.php` for that type.

## Creating transactions

Depending on how your driver works, you're likely going to need to create some transactions depending on different scenarios.
Depending on how your driver works, you're likely going to need to create some transactions depending on different
scenarios.

### Database Schema

```
Lunar\Models\Transaction
```

|Field|Description|Example|
|:-|:-|:-|
|id||
|parent_transaction_id|The ID of the related transaction, nullable|
|order_id|The ID of the order this transaction relates to|
|success| Whether or not the transaction was successful|1
|type|Whether `intent`,`capture` or `refund`|`intent`
|driver|The driver used i.e. `stripe`|`stripe`
|amount|The amount for the transaction in cents|`10000`
|reference|The reference for the driver to use|`STRIPE_123456`
|status|Usually populated from the payment provider|`success`
|notes|Any additional notes for the transaction|
|card_type|The card type| `visa`
|last_four|The last four digits of the card used|`1234`
|captured_at| The DateTime the transaction was captured|
|meta| Any additional meta info for the transaction| `{"cvc_check": "pass", "address_line1_check": "pass", "address_postal_code_check": "pass"}`
|created_at||
|updated_at||

| Field | Description | Example |
|:----------------------|:------------------------------------------------|:--------------------------------------------------------------------------------------------|
| id | |
| parent_transaction_id | The ID of the related transaction, nullable |
| order_id | The ID of the order this transaction relates to |
| success | Whether or not the transaction was successful | 1
| type | Whether `intent`,`capture` or `refund` | `intent`
| driver | The driver used i.e. `stripe` | `stripe`
| amount | The amount for the transaction in cents | `10000`
| reference | The reference for the driver to use | `STRIPE_123456`
| status | Usually populated from the payment provider | `success`
| notes | Any additional notes for the transaction |
| card_type | The card type | `visa`
| last_four | The last four digits of the card used | `1234`
| captured_at | The DateTime the transaction was captured |
| meta | Any additional meta info for the transaction | `{"cvc_check": "pass", "address_line1_check": "pass", "address_postal_code_check": "pass"}`
| created_at | |
| updated_at | |

### Best Practices

#### Releasing

When releasing a payment, if you're not charging the card straight away, you should create a transaction with type `intent`. This tells Lunar you intend to charge the card at a later date.
When releasing a payment, if you're not charging the card straight away, you should create a transaction with
type `intent`. This tells Lunar you intend to charge the card at a later date.

```php
Transaction::create([
Expand All @@ -229,7 +241,8 @@ Transaction::create([
If you're already charging the card, you can skip this as you already have payment. 🥳
:::

When capturing a transaction, you should create an additional transaction with the amount that's been captured. Even if this is the same amount as the `intent` transaction.
When capturing a transaction, you should create an additional transaction with the amount that's been captured. Even if
this is the same amount as the `intent` transaction.

```php
$intent = Transaction::whereType('intent')->first();
Expand Down
15 changes: 9 additions & 6 deletions docs/core/reference/payments.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Payments

:::tip
If you're looking for a guide on how to create your own Payment Driver, or for a more in-depth look at how they work, head over to the [extending section](/core/extending/payments).
If you're looking for a guide on how to create your own Payment Driver, or for a more in-depth look at how they work,
head over to the [extending section](/core/extending/payments).
:::

## Overview

Lunar takes a driver based approach with Payments, meaning you are free to use either add ons to support the provider you wish to use, or you can create your own to meet your exact needs.
Lunar takes a driver based approach with Payments, meaning you are free to use either add ons to support the provider
you wish to use, or you can create your own to meet your exact needs.

## Configuration

All configuration for payments is located in `config/lunar/payments.php`. Here you can specify different types of payments and the driver each one should use.
All configuration for payments is located in `config/lunar/payments.php`. Here you can specify different types of
payments and the driver each one should use.

```php
<?php
Expand All @@ -33,7 +36,8 @@ return [

## Usage

To use a payment driver, you need to pass the type of payment you wish to use, this will then return an instance of the driver.
To use a payment driver, you need to pass the type of payment you wish to use, this will then return an instance of the
driver.

```php
$driver = \Lunar\Facades\Payments::driver('card');
Expand All @@ -48,7 +52,7 @@ $driver->cart(\Lunar\Models\Cart $cart);
Set any additional data that the driver may need.

```php
$driver->setData([
$driver->withData([
'payment_token' => $token,
]);
```
Expand All @@ -59,7 +63,6 @@ Finally, we can authorize the payment.
$driver->authorize();
```


<!-- As you'd expect, orders on an online system show what users have purchased. They are linked to a Cart and you can only have 1 order per cart in the database.
```php
Expand Down

0 comments on commit 3015011

Please sign in to comment.