Skip to content

Commit 324476c

Browse files
committed
Merge pull request #1 from Sohib/master
Invoices and Payments are implemented and fully tested
2 parents c902950 + 1fd4c89 commit 324476c

10 files changed

+189
-68
lines changed

.gitignore

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
### Composer template
33
composer.phar
44
vendor/
5-
5+
.idea
66
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
77
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
88
# composer.lock

Payments.php

-30
This file was deleted.

README.md

100644100755
+10
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
# moyasar-php
2+
3+
4+
## Requirements
5+
6+
- PHP 5.5.0
7+
- To use the PHP stream handler, allow_url_fopen must be enabled in your system's php.ini.
8+
- To use the cURL handler, you must have a recent version of cURL >= 7.19.4 compiled with OpenSSL and zlib.
9+
10+
11+

composer.json

100644100755
+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
{
2-
"require": {
3-
"guzzlehttp/guzzle": "^6.2"
2+
"name": "moyasar/moyasar-php",
3+
"description": "php api wrapper for moyasar.com",
4+
"type": "library",
5+
"authors": [
6+
{
7+
"name": "Sohib H Algotimel",
8+
"email": "s@sgh.sa",
9+
"homepage": "https://sgh.sa"
410
}
11+
],
12+
"minimum-stability": "dev",
13+
"autoload": {
14+
"psr-4": {
15+
"Moyasar\\": "src/Moyasar"
16+
}
17+
},
18+
"require": {
19+
"guzzlehttp/guzzle": "^6.2"
20+
}
521
}

composer.lock

100644100755
File mode changed.

index.php

-26
This file was deleted.

Client.php src/Moyasar/Client.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@ public static function setApiKey($apiKey)
2828

2929
public static function get($request, $options = [])
3030
{
31-
$options = [
32-
'auth' => [self::$apiKey, '']
33-
];
34-
31+
$options[GuzzleHttp\RequestOptions::AUTH] = [self::$apiKey, ''];
3532
$client = new GuzzleHttp\Client();
36-
3733
$response = $client->get($request, $options);
3834
if ($response->getStatusCode() == '200' OR '201') {
3935
return $response->getBody()->getContents();
@@ -45,13 +41,17 @@ public static function get($request, $options = [])
4541

4642
public static function post($request, $options = [])
4743
{
48-
// var_dump(json_encode($options));
4944
$client = new GuzzleHttp\Client();
5045

51-
$options["auth"] = [self::$apiKey, ''];
46+
$data = [
47+
GuzzleHttp\RequestOptions::AUTH => [self::$apiKey, ''],
48+
GuzzleHttp\RequestOptions::FORM_PARAMS => $options,
49+
GuzzleHttp\RequestOptions::DEBUG => false
50+
51+
52+
];
5253

53-
$response = $client->post($request, $options);
54-
// $response = $request->send();
54+
$response = $client->post($request, $data);
5555

5656
if ($response->getStatusCode() == '200' OR '201') {
5757
return $response->getBody()->getContents();
File renamed without changes.

src/Moyasar/Invoices.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Sohib
5+
* Date: 5/19/16
6+
* Time: 9:03 PM
7+
*/
8+
9+
namespace Moyasar;
10+
11+
12+
class Invoices
13+
{
14+
const AMOUNT = "amount";
15+
const CURRENCY = "currency";
16+
const DESCRIPTION = "description";
17+
18+
public static function make($amount, $description, $currency = "SAR")
19+
{
20+
21+
$data = [
22+
self::AMOUNT => $amount,
23+
self::DESCRIPTION => $description,
24+
self::CURRENCY => $currency
25+
];
26+
27+
return json_decode(Client::post("https://api.moyasar.com/v1/invoices", $data));
28+
}
29+
30+
public static function retrieve($id){
31+
return json_decode(Client::get("https://api.moyasar.com/v1/invoices/$id"));
32+
}
33+
34+
public static function all(){
35+
return json_decode(Client::get("https://api.moyasar.com/v1/invoices"));
36+
}
37+
38+
39+
}

src/Moyasar/Payments.php

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Sohib
5+
* Date: 5/9/16
6+
* Time: 10:44 PM
7+
*/
8+
9+
namespace Moyasar;
10+
11+
12+
class Payments
13+
{
14+
const AMOUNT = "amount";
15+
const CURRENCY = "currency";
16+
const DESCRIPTION = "description";
17+
const SOURCE = "source";
18+
const SADAD = "sadad";
19+
const CREDIT_CARD = "creditcard";
20+
21+
public static function make($amount, $source, $description = "", $currency = "SAR")
22+
{
23+
24+
$data = [
25+
self::AMOUNT => $amount,
26+
self::SOURCE => $source,
27+
self::DESCRIPTION => $description,
28+
self::CURRENCY => $currency
29+
];
30+
31+
if (empty($description)) {
32+
unset($data[self::DESCRIPTION]);
33+
}
34+
if ($currency == "SAR") {
35+
unset($data[self::CURRENCY]);
36+
}
37+
38+
39+
self::validate($data);
40+
return json_decode(Client::post("https://api.moyasar.com/v1/payments", $data));
41+
}
42+
43+
private static function validate($data)
44+
{
45+
46+
if (empty($data[self::AMOUNT])) {
47+
throw new \InvalidArgumentException("Amount is empty");
48+
} elseif (empty($data[self::SOURCE])) {
49+
throw new \InvalidArgumentException("Source is empty");
50+
}
51+
52+
$source = $data[self::SOURCE];
53+
54+
if (!is_array($source)) {
55+
throw new \InvalidArgumentException("Source must be an array");
56+
}
57+
58+
if (isset($source["type"])) {
59+
if ($source["type"] == "creditcard") {
60+
61+
if (!isset($source["name"])) {
62+
throw new \Exception("Put Card holder’s name in source[name]");
63+
}
64+
65+
if (!isset($source["number"])) {
66+
throw new \Exception("card number must be in source[number]");
67+
}
68+
69+
if (!isset($source["month"])) {
70+
throw new \Exception("Card expiration month must be in source[month]");
71+
}
72+
73+
if (!isset($source["year"])) {
74+
throw new \Exception("Card expiration year must be in source[year]");
75+
}
76+
77+
78+
} elseif ($source["type"] == "sadad") {
79+
if (!isset($source["username"])) {
80+
throw new \Exception("Put Sadad username in source[username]");
81+
}
82+
83+
} else {
84+
throw new \Exception("source[type] must be sadad or creditcard only");
85+
}
86+
} else {
87+
throw new \Exception("source[type] is missing");
88+
}
89+
90+
}
91+
92+
public static function refund($id, $amount = 0)
93+
{
94+
$data = [
95+
self::AMOUNT => $amount
96+
];
97+
98+
return json_decode(Client::post("https://api.moyasar.com/v1/payments/$id/refund", $data));
99+
}
100+
101+
102+
public static function retrieve($id){
103+
return json_decode(Client::get("https://api.moyasar.com/v1/payments/$id"));
104+
}
105+
106+
public static function all(){
107+
return json_decode(Client::get("https://api.moyasar.com/v1/payments"));
108+
109+
}
110+
111+
112+
}

0 commit comments

Comments
 (0)