Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com-lunar:lunar/payments-api-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
ionutcalaralunar committed Oct 16, 2023
2 parents 581a58b + 36436f5 commit 8e7bd57
Show file tree
Hide file tree
Showing 20 changed files with 1,431 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
118 changes: 116 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,119 @@
# payments-api-sdk
# Lunar payments client (PHP)

php wrapper for the lunar payments API

Scaffolded from [backstage-template-empty-repository](https://github.com/lunarway/backstage-template-empty-repository)
## Requirements

PHP 7.1 and later.

## Install

You can install the package via [Composer](http://getcomposer.org/). Run the following command:

```bash
composer require lunar/payments-api-sdk
```

## Api documentation
To find the api arguments you can go to URL HERE

## Dependencies

The bindings require the following extension in order to work properly:

- [`curl`](https://secure.php.net/manual/en/book.curl.php)

If you use Composer, these dependencies should be handled automatically. If you install manually, you'll want to make sure that these extensions are available.
If you don't want to use curl, you can create your own client to extend from `HttpClientInterface` and send that as a parameter when instantiating the `Lunar` class.

## Examples

```php
$lunar = new \Lunar\Lunar($private_secret_key);

$payment_intent_id = $lunar->payments()->create( $args );

// fetch a payment intent
$result = $lunar->payments()->fetch($payment_intent_id);

// capture a transaction
$payments = $lunar->payments();
$transaction = $payments->capture($transaction_id, [
'amount' => [
'decimal'=> '10',
'currency' => 'EUR'
]
]);


// void a transaction
$payments = $lunar->payments();
$transaction = $payments->cancel($transaction_id, [
'amount' => [
'decimal'=> '10',
'currency' => 'EUR'
]
]);


// refund a transaction
$payments = $lunar->payments();
$transaction = $payments->refund($transaction_id, [
'amount' => [
'decimal'=> '10',
'currency' => 'EUR'
]
]);

```

## Error handling

The api wrapper will throw errors when things do not fly. All errors inherit from
`ApiException`. A very verbose example of catching all types of errors:

```php
$lunar = new \Lunar\Lunar($private_secret_key);
try {
$payments = $lunar->transactions();
$payments->capture($transaction_id, [
'amount' => [
'decimal'=> '10',
'currency' => 'EUR'
]
]);
} catch (\Lunar\Exception\NotFound $e) {
// The transaction was not found
} catch (\Lunar\Exception\InvalidRequest $e) {
// Bad (invalid) request - see $e->getJsonBody() for the error
} catch (\Lunar\Exception\Forbidden $e) {
// You are correctly authenticated but do not have access.
} catch (\Lunar\Exception\Unauthorized $e) {
// You need to provide credentials (an app's API key)
} catch (\Lunar\Exception\Conflict $e) {
// Everything you submitted was fine at the time of validation, but something changed in the meantime and came into conflict with this (e.g. double-capture).
} catch (\Lunar\Exception\ApiConnection $e) {
// Network error on connecting via cURL
} catch (\Lunar\Exception\ApiException $e) {
// Unknown api error
}
```

In most cases catching `NotFound` and `InvalidRequest` as client errors
and logging `ApiException` would suffice.

## Development

Install dependencies:

``` bash
composer install
```

## Tests

Install dependencies as mentioned above (which will resolve [PHPUnit](http://packagist.org/packages/phpunit/phpunit)), then you can run the test suite:

```bash
./vendor/bin/phpunit
```
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "lunar/payments-api-sdk",
"description": "PHP SDK to communicate with the Lunar HTTP API",
"version": "1.0.0",
"license": "MIT",
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Lunar\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Lunar\\Tests\\": "tests/"
}
},
"require": {
"php": ">=7.1",
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.6.3"
}
}
7 changes: 7 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<phpunit bootstrap="./vendor/autoload.php" backupGlobals="false" colors="true">
<testsuites>
<testsuite name="Lunar PHP Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
26 changes: 26 additions & 0 deletions src/Endpoint/Endpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Lunar\Endpoint;

/**
* Class Endpoint
*
* @package Lunar\Endpoint
*/
abstract class Endpoint
{
/**
* @var \Lunar\Lunar
*/
protected $lunar;

/**
* Endpoint constructor.
*
* @param $lunar
*/
function __construct($lunar)
{
$this->lunar = $lunar;
}
}
140 changes: 140 additions & 0 deletions src/Endpoint/Payments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace Lunar\Endpoint;

use Lunar\Utils\Cursor;

/**
* Class Payments
* @link https://github.com/lunarway/merchant-payments/blob/master/api/swagger.yaml
* @link https://github.com/lunarway/squad-juno-docs/blob/master/docs/payments/hosted-checkout.md
* @package Lunar\Endpoint
*/
class Payments extends Endpoint
{
/**
*
* @param $args array
*
* @return string
*/
public function create($args)
{
$url = 'payments';

$api_response = $this->lunar->client->request('POST', $url, $args);

return $api_response->json['paymentId'];
}

/**
*
* @param $payment_id
*
* @return array
*/
public function fetch($payment_id)
{
$url = 'payments/' . $payment_id;

$api_response = $this->lunar->client->request('GET', $url);

return $api_response->json['payment'];
}

/**
*
* @param $payment_id
*
* @return array
*/
public function put($payment_id, $args)
{
$url = 'payments/' . $payment_id;

$api_response = $this->lunar->client->request('PUT', $url, $args);

return $api_response->json['paymentId'];
}

/**
*
* @param $payment_id
*
* @return array
*/
public function intent($payment_id)
{
$url = 'payments/' . $payment_id.'/intent';

$api_response = $this->lunar->client->request('GET', $url);

return $api_response->json;
}

public function submit($payment_id, $args)
{
$url = 'payments/' . $payment_id.'/submit';

$api_response = $this->lunar->client->request('POST', $url, $args);

return $api_response->json;
}

public function capture($payment_id, $args)
{
$url = 'payments/' . $payment_id.'/capture';

$api_response = $this->lunar->client->request('POST', $url, $args);

return $api_response->json;
}

public function methods($payment_id)
{
$url = 'payments/' . $payment_id.'/methods';

$api_response = $this->lunar->client->request('GET', $url);

return $api_response->json;
}

public function challenge($payment_id, $challenge_id)
{
$url = 'payments/' . $payment_id.'/challenge/'.$challenge_id;

$api_response = $this->lunar->client->request('POST', $url);

return $api_response->json;
}

public function hints($payment_id, $args)
{
$url = 'payments/' . $payment_id.'/hints';

$api_response = $this->lunar->client->request('POST', $url, $args);

return $api_response->json;
}

public function refund($payment_id, $args)
{
$url = 'payments/' . $payment_id.'/refund';

$api_response = $this->lunar->client->request('POST', $url, $args);

return $api_response->json;
}


public function cancel($payment_id, $args)
{
$url = 'payments/' . $payment_id.'/cancel';

$api_response = $this->lunar->client->request('POST', $url, $args);

return $api_response->json;
}


}
13 changes: 13 additions & 0 deletions src/Exception/ApiConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lunar\Exception;

/**
* Class ApiConnection
*
* @package Lunar\Exception
*/
class ApiConnection extends ApiException
{

}
50 changes: 50 additions & 0 deletions src/Exception/ApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Lunar\Exception;

/**
* Class ApiError
*
* @package Lunar\Exception
*/
class ApiException extends \Exception
{
public $http_status;
public $http_body;
public $json_body;
public $http_headers;

public function __construct(
$message,
$http_status = null,
$http_body = null,
$json_body = null,
$http_headers = null
) {
parent::__construct($message);
$this->http_status = $http_status;
$this->http_body = $http_body;
$this->json_body = $json_body;
$this->http_headers = $http_headers;
}

public function getHttpStatus()
{
return $this->http_status;
}

public function getHttpBody()
{
return $this->http_body;
}

public function getJsonBody()
{
return $this->json_body;
}

public function getHttpHeaders()
{
return $this->http_headers;
}
}
Loading

0 comments on commit 8e7bd57

Please sign in to comment.