Skip to content

Commit

Permalink
Merge pull request #17 from Drewdan/feature/15-handle-webhooks
Browse files Browse the repository at this point in the history
Add ability to create, webhooks, search webhooks and parse webhook ev…
  • Loading branch information
Drewdan authored Mar 20, 2024
2 parents 6574814 + 4e4925d commit 0d22425
Show file tree
Hide file tree
Showing 92 changed files with 1,279 additions and 109 deletions.
Empty file modified .github/workflows/php.yml
100644 → 100755
Empty file.
6 changes: 4 additions & 2 deletions .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/vendor/
.phpunit.result.cache
.idea/
.idea
.idea/*
.idea*
ray.php
.phpunit.cache
.env
Empty file modified .phpunit.result.cache
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified ROADMAP.md
100644 → 100755
Empty file.
Empty file modified composer.json
100644 → 100755
Empty file.
Empty file modified composer.lock
100644 → 100755
Empty file.
Empty file modified config/paypal.php
100644 → 100755
Empty file.
Empty file modified phpunit.xml
100644 → 100755
Empty file.
Empty file modified ray.php
100644 → 100755
Empty file.
33 changes: 25 additions & 8 deletions src/Client/PaypalClient.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

namespace Drewdan\Paypal\Client;

use JsonMapper;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\Response;
use Illuminate\Http\Client\PendingRequest;
use Drewdan\Paypal\Builders\PaymentSource\Paypal;
use Drewdan\Paypal\Exceptions\InvalidClientException;
use Drewdan\Paypal\Exceptions\InvalidRequestException;
use Drewdan\Paypal\Exceptions\MissingCredentialsException;

class PaypalClient {

const VERSION = '/v2/';
private string $version = '/v2/';

const SANDBOX_URL = 'https://api-m.sandbox.paypal.com';

Expand All @@ -28,19 +25,39 @@ class PaypalClient {
/**
* @throws \Drewdan\Paypal\Exceptions\MissingCredentialsException
*/
public function __construct(public bool $responseAsArray = false) {
public function __construct(public bool $responseAsArray = false, public bool $useV1 = false) {
if (
Str::of(config('paypal.client_id'))->trim()->isEmpty() ||
Str::of(config('paypal.secret'))->trim()->isEmpty()) {
throw new MissingCredentialsException('You have not set your Paypal Credentials');
}

if ($this->useV1) {
$this->version = '/v1/';
}

$this->client = Http::withBasicAuth(config('paypal.client_id'), config('paypal.secret'))
->asJson()
->baseUrl($this->generateBaseUrl());
}

public static function make(bool $responseAsArray = false): static {
return App::make(PaypalClient::class, ['responseAsArray' => $responseAsArray]);
public static function make(bool $responseAsArray = false, bool $useV1 = false): static {
return App::make(
PaypalClient::class,
[
'responseAsArray' => $responseAsArray,
'useV1' => $useV1,
]
);
}

public function getClient(): PendingRequest {
return $this->client;
}

public function withQuery(array $query): static {
$this->client->withQuery($query);
return $this;
}

/**
Expand Down Expand Up @@ -76,7 +93,7 @@ public function __call(string $name, array $arguments = []) {
public function generateBaseUrl(): string {
return (config('paypal.environment') === 'LIVE'
? self::LIVE_URL
: self::SANDBOX_URL) . self::VERSION;
: self::SANDBOX_URL) . $this->version;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/BuildsPayload.php → src/Common/Contracts/BuildsPayload.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Contracts;
namespace Drewdan\Paypal\Common\Contracts;

interface BuildsPayload {

Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/FromArray.php → src/Common/Contracts/FromArray.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Contracts;
namespace Drewdan\Paypal\Common\Contracts;

use Illuminate\Support\Collection;

Expand Down
9 changes: 9 additions & 0 deletions src/Common/Contracts/FromResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Drewdan\Paypal\Common\Contracts;

interface FromResponse {

public static function fromResponse(array $response): static;

}
4 changes: 2 additions & 2 deletions src/Models/Link.php → src/Common/Models/Link.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Drewdan\Paypal\Models;
namespace Drewdan\Paypal\Common\Models;

use Drewdan\Paypal\Contracts\FromArray;
use Drewdan\Paypal\Common\Contracts\FromArray;

class Link implements FromArray {

Expand Down
4 changes: 2 additions & 2 deletions src/Models/Links.php → src/Common/Models/Links.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Drewdan\Paypal\Models;
namespace Drewdan\Paypal\Common\Models;

use Illuminate\Support\Collection;
use Drewdan\Paypal\Contracts\FromArray;
use Drewdan\Paypal\Common\Contracts\FromArray;

class Links implements FromArray {

Expand Down
Empty file modified src/Dtos/BaseDto.php
100644 → 100755
Empty file.
Empty file modified src/Dtos/Capture.php
100644 → 100755
Empty file.
Empty file modified src/Dtos/Link.php
100644 → 100755
Empty file.
Empty file modified src/Dtos/Order.php
100644 → 100755
Empty file.
Empty file modified src/Exceptions/InvalidClientException.php
100644 → 100755
Empty file.
Empty file modified src/Exceptions/InvalidRequestException.php
100644 → 100755
Empty file.
Empty file modified src/Exceptions/MissingCredentialsException.php
100644 → 100755
Empty file.
Empty file modified src/Helpers/Helper.php
100644 → 100755
Empty file.
18 changes: 8 additions & 10 deletions src/Builders/OrderBuilder.php → src/Orders/Builders/OrderBuilder.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace Drewdan\Paypal\Builders;
namespace Drewdan\Paypal\Orders\Builders;

use Drewdan\Paypal\Models\Order;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Drewdan\Paypal\Orders\Models\Order;
use Drewdan\Paypal\Client\PaypalClient;
use Drewdan\Paypal\Models\PurchaseUnit;
use Drewdan\Paypal\Enums\PaymentIntentEnum;
use Drewdan\Paypal\Contracts\BuildsPayload;
use Drewdan\Paypal\Builders\PaymentSource\PaymentSource;
use Drewdan\Paypal\Orders\Models\PurchaseUnit;
use Drewdan\Paypal\Orders\Enums\PaymentIntentEnum;
use Drewdan\Paypal\Common\Contracts\BuildsPayload;
use Drewdan\Paypal\Orders\Builders\PaymentSource\PaymentSource;

class OrderBuilder implements BuildsPayload {

Expand All @@ -25,9 +25,7 @@ public static function make(): static {
return App::make(static::class);
}

public function __construct(

) {
public function __construct() {
$this->purchaseUnits = collect();
$this->client = PaypalClient::make(true);
}
Expand Down Expand Up @@ -61,7 +59,7 @@ public function setPaymentSource(PaymentSource $paymentSource): static {

public function buildPayload(): array {
return [
'purchase_units' => $this->purchaseUnits->map(fn($unit) => $unit->buildPayload())->toArray(),
'purchase_units' => $this->purchaseUnits->map(fn ($unit) => $unit->buildPayload())->toArray(),
'intent' => $this->intent->value,
'payment_source' => $this->paymentSource->buildPaymentSource(),
];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Builders\PaymentSource;
namespace Drewdan\Paypal\Orders\Builders\PaymentSource;

use Illuminate\Support\Facades\App;

Expand Down
14 changes: 7 additions & 7 deletions src/Builders/PaymentSource/Paypal.php → src/Orders/Builders/PaymentSource/Paypal.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

namespace Drewdan\Paypal\Builders\PaymentSource;
namespace Drewdan\Paypal\Orders\Builders\PaymentSource;

use Illuminate\Support\Arr;
use Drewdan\Paypal\Helpers\Helper;
use Drewdan\Paypal\Enums\UserActionEnum;
use Drewdan\Paypal\Enums\LandingPageEnum;
use Drewdan\Paypal\Models\ExperienceContext;
use Drewdan\Paypal\Enums\ShippingPreferenceEnum;
use Drewdan\Paypal\Contracts\BuildsPaymentSource;
use Drewdan\Paypal\Enums\PaymentMethodPreferenceEnum;
use Drewdan\Paypal\Orders\Enums\UserActionEnum;
use Drewdan\Paypal\Orders\Enums\LandingPageEnum;
use Drewdan\Paypal\Orders\Models\ExperienceContext;
use Drewdan\Paypal\Orders\Enums\ShippingPreferenceEnum;
use Drewdan\Paypal\Orders\Contracts\BuildsPaymentSource;
use Drewdan\Paypal\Orders\Enums\PaymentMethodPreferenceEnum;

class Paypal extends PaymentSource implements BuildsPaymentSource {

Expand Down
5 changes: 2 additions & 3 deletions src/Builders/PaymentSource/Token.php → src/Orders/Builders/PaymentSource/Token.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

namespace Drewdan\Paypal\Builders\PaymentSource;
namespace Drewdan\Paypal\Orders\Builders\PaymentSource;

use Illuminate\Support\Facades\App;
use Drewdan\Paypal\Contracts\BuildsPaymentSource;
use Drewdan\Paypal\Orders\Contracts\BuildsPaymentSource;

class Token extends PaymentSource implements BuildsPaymentSource {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Contracts;
namespace Drewdan\Paypal\Orders\Contracts;

interface BuildsPaymentSource {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Enums;
namespace Drewdan\Paypal\Orders\Enums;

enum AuthorizationStatusEnum: string {

Expand Down
2 changes: 1 addition & 1 deletion src/Enums/LandingPageEnum.php → src/Orders/Enums/LandingPageEnum.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Enums;
namespace Drewdan\Paypal\Orders\Enums;

enum LandingPageEnum: string {

Expand Down
2 changes: 1 addition & 1 deletion src/Enums/OrderStatusEnum.php → src/Orders/Enums/OrderStatusEnum.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Enums;
namespace Drewdan\Paypal\Orders\Enums;

enum OrderStatusEnum: string {

Expand Down
2 changes: 1 addition & 1 deletion src/Enums/PaymentIntentEnum.php → src/Orders/Enums/PaymentIntentEnum.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Enums;
namespace Drewdan\Paypal\Orders\Enums;

enum PaymentIntentEnum: string {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Enums;
namespace Drewdan\Paypal\Orders\Enums;

enum PaymentMethodPreferenceEnum: string {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Enums;
namespace Drewdan\Paypal\Orders\Enums;

enum PurchaseUnitItemCategoryEnum: string {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Enums;
namespace Drewdan\Paypal\Orders\Enums;

enum SellerProtectionStatusEnum: string {

Expand Down
2 changes: 1 addition & 1 deletion src/Enums/ShippingPreferenceEnum.php → src/Orders/Enums/ShippingPreferenceEnum.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Enums;
namespace Drewdan\Paypal\Orders\Enums;

enum ShippingPreferenceEnum: string {

Expand Down
2 changes: 1 addition & 1 deletion src/Enums/UserActionEnum.php → src/Orders/Enums/UserActionEnum.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drewdan\Paypal\Enums;
namespace Drewdan\Paypal\Orders\Enums;

enum UserActionEnum: string {

Expand Down
4 changes: 2 additions & 2 deletions src/Models/Amount.php → src/Orders/Models/Amount.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Drewdan\Paypal\Models;
namespace Drewdan\Paypal\Orders\Models;

use Illuminate\Support\Facades\App;
use Drewdan\Paypal\Contracts\BuildsPayload;
use Drewdan\Paypal\Common\Contracts\BuildsPayload;

class Amount implements BuildsPayload {

Expand Down
6 changes: 3 additions & 3 deletions src/Models/Authorization.php → src/Orders/Models/Authorization.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Drewdan\Paypal\Models;
namespace Drewdan\Paypal\Orders\Models;

use Drewdan\Paypal\Contracts\FromArray;
use Drewdan\Paypal\Enums\AuthorizationStatusEnum;
use Drewdan\Paypal\Common\Contracts\FromArray;
use Drewdan\Paypal\Orders\Enums\AuthorizationStatusEnum;

class Authorization implements FromArray {

Expand Down
4 changes: 2 additions & 2 deletions src/Models/Capture.php → src/Orders/Models/Capture.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Drewdan\Paypal\Models;
namespace Drewdan\Paypal\Orders\Models;

use Illuminate\Support\Arr;
use Drewdan\Paypal\Contracts\FromArray;
use Drewdan\Paypal\Client\PaypalClient;
use Drewdan\Paypal\Common\Contracts\FromArray;

class Capture implements FromArray {

Expand Down
12 changes: 6 additions & 6 deletions src/Models/ExperienceContext.php → src/Orders/Models/ExperienceContext.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace Drewdan\Paypal\Models;
namespace Drewdan\Paypal\Orders\Models;

use Illuminate\Support\Facades\App;
use Drewdan\Paypal\Enums\UserActionEnum;
use Drewdan\Paypal\Enums\LandingPageEnum;
use Drewdan\Paypal\Contracts\BuildsPayload;
use Drewdan\Paypal\Enums\ShippingPreferenceEnum;
use Drewdan\Paypal\Enums\PaymentMethodPreferenceEnum;
use Drewdan\Paypal\Orders\Enums\UserActionEnum;
use Drewdan\Paypal\Orders\Enums\LandingPageEnum;
use Drewdan\Paypal\Common\Contracts\BuildsPayload;
use Drewdan\Paypal\Orders\Enums\ShippingPreferenceEnum;
use Drewdan\Paypal\Orders\Enums\PaymentMethodPreferenceEnum;

class ExperienceContext implements BuildsPayload {

Expand Down
16 changes: 9 additions & 7 deletions src/Models/Order.php → src/Orders/Models/Order.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php

namespace Drewdan\Paypal\Models;
namespace Drewdan\Paypal\Orders\Models;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Drewdan\Paypal\Client\PaypalClient;
use Drewdan\Paypal\Builders\OrderBuilder;
use Drewdan\Paypal\Enums\OrderStatusEnum;
use Drewdan\Paypal\Contracts\BuildsPaymentSource;
use Drewdan\Paypal\Builders\PaymentSource\PaymentSource;
use Drewdan\Paypal\Common\Models\Links;
use Drewdan\Paypal\Orders\Builders\OrderBuilder;
use Drewdan\Paypal\Orders\Enums\OrderStatusEnum;
use Drewdan\Paypal\Common\Contracts\FromResponse;
use Drewdan\Paypal\Orders\Contracts\BuildsPaymentSource;
use Drewdan\Paypal\Orders\Builders\PaymentSource\PaymentSource;

class Order {
class Order implements FromResponse {

private PaypalClient $client;

Expand All @@ -29,7 +31,7 @@ public function __construct(
$this->client = PaypalClient::make(true);
}

public static function fromResponse(array $response): Order {
public static function fromResponse(array $response): static {
return new Order(
create_time: $response['create_time'] ?? null,
update_time: $response['update_time'] ?? null,
Expand Down
Loading

0 comments on commit 0d22425

Please sign in to comment.