Skip to content

Commit cd32feb

Browse files
committed
Initial setup
0 parents  commit cd32feb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3364
-0
lines changed

.editorconfig

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
9+
[**.php]
10+
indent_size = 4
11+
continuation_indent_size = 4
12+
insert_final_newline = true
13+
14+
[**.xml]
15+
indent_size = 4
16+
17+
[**.yml]
18+
indent_size = 2
19+
20+
[*.{js,json}]
21+
indent_size = 2
22+
23+
[composer.{json,lock}]
24+
indent_size = 4

.gitattributes

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/test export-ignore
2+
.editorconfig export-ignore
3+
.gitattributes export-ignore
4+
.gitignore export-ignore
5+
.php_cs export-ignore
6+
phpunit.xml.dist export-ignore

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.php_cs.cache
2+
composer.lock
3+
phpunit.xml
4+
vendor/

.php_cs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
return Symfony\CS\Config\Config::create()
4+
->setUsingCache(true)
5+
->fixers(
6+
array(
7+
'-concat_without_spaces',
8+
'-empty_return',
9+
'-phpdoc_indent',
10+
'-phpdoc_no_empty_return',
11+
'-phpdoc_params',
12+
'-phpdoc_to_comment',
13+
'-unneeded_control_parentheses',
14+
'long_array_syntax',
15+
'concat_with_spaces',
16+
'ereg_to_preg',
17+
'ordered_use',
18+
)
19+
)
20+
->finder(
21+
Symfony\CS\Finder\DefaultFinder::create()
22+
->in('src')
23+
->in('test')
24+
)
25+
;

LICENSE.md

+674
Large diffs are not rendered by default.

README.md

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# Aplazame PHP SDK
2+
3+
[![Aplazame](docs/banner-728-white-php.png)](https://aplazame.com)
4+
5+
[Aplazame](https://aplazame.com), a consumer credit company, offers a payment system that can be
6+
used by online buyers to receive funding for their purchases.
7+
8+
9+
## Installation
10+
11+
You can use [Composer](https://getcomposer.org):
12+
13+
```bash
14+
composer require aplazame/aplazame-api-sdk
15+
```
16+
17+
18+
## Usage
19+
20+
21+
### Checkout
22+
23+
This SDK provides a tree of objects for guide you about to craft the checkout model.
24+
25+
```php
26+
/*
27+
* Merchant model
28+
*/
29+
$merchant = new Aplazame\BusinessModel\Merchant(
30+
"/confirm", // url that the JS client sent to confirming the order.
31+
"/cancel", // url that the customer is sent to if there is an error in the checkout.
32+
"/success" // url that the customer is sent to after confirming their order.
33+
);
34+
$merchant->checkout_url = "/checkout"; // url that the customer is sent to if the customer chooses to back to the e-commerce, by default is /.
35+
36+
37+
/*
38+
* Article model
39+
*/
40+
$article = new Aplazame\BusinessModel\Article(
41+
"89793238462643383279", // The article ID.
42+
"Reloj en oro blanco de 18 quilates y diamantes", // Article name.
43+
"http://shop.example.com/product.html", // Article url.
44+
"http://shop.example.com/product_image.png", // Article image url.
45+
2, // Article quantity.
46+
Aplazame\BusinessModel\Decimal::fromFloat(4020.00) // Article price (tax is not included). (4,020.00 €)
47+
);
48+
$article->description = "Movimiento de cuarzo de alta precisión"; // Article description.
49+
$article->tax_rate = Aplazame\BusinessModel\Decimal::fromFloat(21.00); // Article tax rate. (21.00%)
50+
$article->discount = Aplazame\BusinessModel\Decimal::fromFloat(5.00); // The discount amount of the article. (5.00 €)
51+
$article->discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(2.00); // The rate discount of the article. (2.00 %)
52+
53+
// ... rest of articles in the shopping cart.
54+
55+
/*
56+
* Articles collection
57+
*/
58+
$articles = array( $article, ... );
59+
60+
61+
/*
62+
* Order model
63+
*/
64+
$order = new Aplazame\BusinessModel\Order(
65+
"28475648233786783165", // Your order ID.
66+
"EUR", // Currency code of the order.
67+
Aplazame\BusinessModel\Decimal::fromFloat(21.00), // Order tax rate. (21.00%)
68+
Aplazame\BusinessModel\Decimal::fromFloat(4620.00), // Order total amount. (4,620.00 €)
69+
$articles // Articles in cart.
70+
);
71+
$order->discount = Aplazame\BusinessModel\Decimal::fromFloat(160.00); // The discount amount of the order. (160.00 €)
72+
$order->discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(2.00); // The rate discount of the order. (2.00 %)
73+
$order->cart_discount = Aplazame\BusinessModel\Decimal::fromFloat(0.50); // The discount amount of the cart. (0.50 €)
74+
$order->cart_discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(3.00); // The rate discount of the cart. (3.00 %)
75+
76+
/*
77+
* Customer address model
78+
*/
79+
$customerAddress = new Aplazame\BusinessModel\Address(
80+
"John", // Address first name.
81+
"Coltrane", // Address last name.
82+
"Plaza del Angel nº10", // Address street.
83+
"Madrid", // Address city.
84+
"Madrid", // Address state.
85+
"ES", // Address country code.
86+
"28012" // Address postcode.
87+
);
88+
$customerAddress->phone = "616123456"; // Address phone number.
89+
$customerAddress->alt_phone = "+34917909930"; // Address alternative phone.
90+
$customerAddress->address_addition = "Cerca de la plaza Santa Ana"; // Address addition.
91+
92+
/*
93+
* Customer model
94+
*/
95+
$customer = new Aplazame\BusinessModel\Customer(
96+
"1618", // Customer ID.
97+
"[email protected]", // The customer email.
98+
Aplazame\BusinessModel\Customer::TYPE_EXISTING, // Customer type. Other options are: TYPE_GUEST and TYPE_NEW.
99+
Aplazame\BusinessModel\Customer::GENDER_UNKNOWN, // Customer gender. Other options are: GENDER_MALE, GENDER_FEMALE and GENDER_NOT_APPLICABLE.
100+
);
101+
$customer->first_name = "John"; // Customer first name.
102+
$customer->last_name = "Coltrane"; // Customer last name.
103+
$customer->birthday = DateTime::createFromFormat(DateTime::ISO8601, "1990-08-21T13:56:45+0000"); // Customer birthday.
104+
$customer->language = "es"; // Customer language preferences.
105+
$customer->date_joined = DateTime::createFromFormat(DateTime::ISO8601, "2014-08-21T13:56:45+0000"); // A datetime designating when the customer account was created.
106+
$customer->last_login = DateTime::createFromFormat(DateTime::ISO8601, "2014-08-27T19:57:56+0000"); // A datetime of the customer last login.
107+
$customer->address = $customerAddress; // Customer address.
108+
109+
110+
/*
111+
* Billing address model
112+
*/
113+
$billingBilling = new Aplazame\BusinessModel\Address(
114+
"Bill", // Billing first name.
115+
"Evans", // Billing last name.
116+
"Calle de Las Huertas 22", // Billing street.
117+
"Madrid", // Billing city.
118+
"Madrid", // Billing state.
119+
"ES", // Billing country code.
120+
"28014" // Billing postcode.
121+
);
122+
$billingAddress->phone = "+34914298407"; // Billing phone number.
123+
$billingAddress->alt_phone = null; // Billing alternative phone.
124+
$billingAddress->address_addition = "Cerca de la pizzería"; // Billing address addition.
125+
126+
127+
/*
128+
* Shipping info model
129+
*/
130+
$shippingInfo = new Aplazame\BusinessModel\Address(
131+
"Django", // Shipping first name.
132+
"Reinhard", // Shipping last name.
133+
"Plaza del Angel nº10", // Shipping street.
134+
"Madrid", // Shipping city.
135+
"Madrid", // Shipping state.
136+
"ES", // Shipping country code.
137+
"28012", // Shipping postcode.
138+
"Planet Express", // Shipping name.
139+
Aplazame\BusinessModel\Decimal::fromFloat(5.00), // Shipping price (tax is not included). (5.00 €)
140+
);
141+
$shippingInfo->phone = "616123456"; // Shipping phone number.
142+
$shippingInfo->alt_phone = "+34917909930"; // Shipping alternative phone.
143+
$shippingInfo->address_addition = "Cerca de la plaza Santa Ana"; // Shipping address addition.
144+
$shippingInfo->tax_rate = Aplazame\BusinessModel\Decimal::fromFloat(21.00); // Shipping tax rate. (21.00%)
145+
$shippingInfo->discount = Aplazame\BusinessModel\Decimal::fromFloat(1.00); // The discount amount of the shipping. (1.00 €)
146+
$shippingInfo->discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(2.00); // The rate discount of the shipping. (2.00 %)
147+
148+
149+
/*
150+
* Checkout model
151+
*/
152+
$checkout = new Aplazame\BusinessModel\Checkout(
153+
true, // TOC
154+
$merchant,
155+
$order,
156+
$customer
157+
);
158+
$checkout->billing = $billingAddress;
159+
$checkout->shipping = $shippingInfo;
160+
```
161+
162+
In your view you will need to put an snippet similar to this one.
163+
```html
164+
<script>
165+
aplazame.checkout( <?php echo $checkout->jsonSerialize(); ?> );
166+
</script>
167+
```
168+
169+
### API Calls
170+
171+
This SDK assist you for craft a well formatted API request and decode the response back to PHP.
172+
173+
```php
174+
$apiBaseUri = 'http://api.aplazame.com';
175+
$environment = Aplazame\Api\Client::ENVIRONMENT_SANDBOX; // When you are ready use Aplazame\Api\Client::ENVIRONMENT_PRODUCTION
176+
$accessToken = 'api private key';
177+
178+
$aplazameApiClient = new Aplazame\Api\Client($apiBaseUri, $environment, $accessToken);
179+
try {
180+
$result = $aplazameApiClient->get('/me');
181+
} catch (Aplazame\Api\ApiCommunicationException $apiCommunicationException) {
182+
// A network error has occurred while sending the request or receiving the response.
183+
184+
// Retry
185+
} catch (Aplazame\Api\DeserializeException $deserializationException) {
186+
// Nobody knows when this happen, may an HTTP Proxy on our side or on your side started to return HTML responses with errors.
187+
188+
// Retry
189+
} catch (Aplazame\Api\ApiServerException $apiServerException) {
190+
// Our server has crashed. We promise to fix it ASAP.
191+
192+
echo 'Error type', $apiServerException->getType(), PHP_EOL;
193+
echo 'Error message', $apiServerException->getMessage(), PHP_EOL;
194+
195+
$rawErrorWithErrorDetails = $apiServerException->getError();
196+
197+
} catch (Aplazame\Api\ApiClientException $apiClientException) {
198+
// Your client has sent an invalid request. Please check your code.
199+
200+
echo 'Error type', $apiClientException->getType(), PHP_EOL;
201+
echo 'Error message', $apiClientException->getMessage(), PHP_EOL;
202+
203+
$rawErrorWithErrorDetails = $apiClientException->getError();
204+
}
205+
print_r($result);
206+
```
207+
208+
209+
## Documentation
210+
211+
Documentation is available at http://docs.aplazame.com.

composer.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "aplazame/aplazame-api-sdk",
3+
"description": "Aplazame API SDK",
4+
"keywords": [
5+
"aplazame"
6+
],
7+
"license": "GPL-3.0",
8+
"require": {
9+
"php": ">= 5.3"
10+
},
11+
"require-dev": {
12+
"friendsofphp/php-cs-fixer": "1.11.*",
13+
"phpunit/phpunit": "^4.0.0"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Aplazame\\": "src/"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"Aplazame\\": "test/"
23+
}
24+
}
25+
}

docs/banner-728-white-php.png

15.3 KB
Loading

phpunit.xml.dist

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
colors="true"
4+
forceCoversAnnotation="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd">
7+
<testsuites>
8+
<testsuite name="library">
9+
<directory suffix="Test.php">test/</directory>
10+
</testsuite>
11+
</testsuites>
12+
<filter>
13+
<whitelist>
14+
<directory suffix=".php">./src</directory>
15+
</whitelist>
16+
</filter>
17+
</phpunit>

src/Api/ApiClientException.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Aplazame\Api;
4+
5+
use Aplazame\Http\ResponseInterface;
6+
use LogicException;
7+
8+
/**
9+
* Exception thrown for HTTP 4xx client errors.
10+
*/
11+
class ApiClientException extends LogicException implements AplazameExceptionInterface
12+
{
13+
/**
14+
* @param ResponseInterface $response
15+
*
16+
* @return self
17+
*/
18+
public static function fromResponse(ResponseInterface $response)
19+
{
20+
$responseBody = (string) $response->getBody();
21+
if (empty($responseBody)) {
22+
return new self($response->getReasonPhrase(), $response->getStatusCode());
23+
}
24+
25+
$decodedBody = json_decode($responseBody, true);
26+
if (!isset($decodedBody['error'])) {
27+
return new self($response->getReasonPhrase(), $response->getStatusCode());
28+
}
29+
30+
$error = $decodedBody['error'];
31+
32+
return new self($error['type'], $error['message'], $error);
33+
}
34+
35+
/**
36+
* @var string
37+
*/
38+
private $type;
39+
40+
/**
41+
* @var array
42+
*/
43+
private $error;
44+
45+
/**
46+
* @param string $type
47+
* @param string $message
48+
* @param array $error
49+
*/
50+
public function __construct($type, $message, array $error = array())
51+
{
52+
parent::__construct($message);
53+
54+
$this->type = $type;
55+
$this->error = $error;
56+
}
57+
58+
/**
59+
* @return string
60+
*/
61+
public function getType()
62+
{
63+
return $this->type;
64+
}
65+
66+
/**
67+
* @return array
68+
*/
69+
public function getError()
70+
{
71+
return $this->error;
72+
}
73+
}

0 commit comments

Comments
 (0)