Skip to content

Commit

Permalink
Paddle Sandbox environment (#19)
Browse files Browse the repository at this point in the history
* Sandbox URL
* Docs
* Update README.md
  • Loading branch information
pascalbaljet authored Feb 20, 2021
1 parent 5dce528 commit d95d214
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes to `laravel-paddle` will be documented in this file

## 2.2.0 - 2021-02-20

- Support for [Paddle Sandbox](https://developer.paddle.com/getting-started/sandbox)

## 2.1.0 - 2020-10-30

- Support for PHP 8.0

## 2.0.0 - 2020-10-05

- Added support for Laravel 8.0
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This package provides an integration with [Paddle.com](https://paddle.com) for L
* Super easy wrapper around the [Paddle.com API](https://developer.paddle.com/api-reference/intro)
* Built-in support for Webhooks and Event handling
* Blade directive to use [Paddle.js](https://paddle.com/docs/paddle-js-overlay-checkout/) in your front-end
* Support for [Paddle Sandbox](https://developer.paddle.com/getting-started/sandbox)

## Support

Expand All @@ -33,22 +34,31 @@ composer require protonemedia/laravel-paddle

## Configuration

Publish the config and view files:
Publish the config file:

```bash
php artisan vendor:publish --provider="ProtoneMedia\LaravelPaddle\PaddleServiceProvider"
php artisan vendor:publish --provider="ProtoneMedia\LaravelPaddle\PaddleServiceProvider" --tag=config
```

Set your [Vendor ID and Code](https://vendors.paddle.com/authentication) and the [Public Key](https://vendors.paddle.com/public-key) settings in your `.env` file or in the `config/paddle.php` file. The Public Key is used to verify incoming webhooks from Paddle.

```bash
PADDLE_SANBOX=false
PADDLE_VENDOR_ID=123
PADDLE_VENDOR_AUTH_CODE=456
PADDLE_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----"
```

### Paddle Sandbox

As of version 2.2.0, this package supports the [Paddle Sandbox](https://developer.paddle.com/getting-started/sandbox) environment. To use this environment, set the `sandbox_environment` configuration key to `true`. This will configure the API URLs, as well as the Paddle JavaScript library. If you've published the Blade View while using a previous version of this package, make sure you republish the view:

```bash
php artisan vendor:publish --provider="ProtoneMedia\LaravelPaddle\PaddleServiceProvider" --tag=views
```

## Usage

The API calls are available with the `Paddle` facade. Check out the [the documentation](https://developer.paddle.com/api-reference/intro) to learn all about the Paddle API. You can build your API calls fluently or you could simply pass an array which holds the data. This package has some basic validation rules for the given data and this might result in an `InvalidDataException` if your data is invalid. Whenever an API call fails it will throw a `PaddleApiException`.
Expand Down
5 changes: 5 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@

'webhook_uri' => 'paddle/webhook',

/**
* https://developer.paddle.com/getting-started/sandbox
*/
'sandbox_environment' => env('PADDLE_SANBOX', false),

];
3 changes: 3 additions & 0 deletions resources/views/javaScriptSetup.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<script src="https://cdn.paddle.com/paddle/paddle.js"></script>
<script type="text/javascript">
@if(config('paddle.sandbox_environment'))
Paddle.Environment.set('sandbox');
@endif
Paddle.Setup(@json(['vendor' => (int) config('paddle.vendor_id')]));
</script>
8 changes: 7 additions & 1 deletion src/Api/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ public function send()
$data['vendor_auth_code'] = config('paddle.vendor_auth_code');
}

$response = Http::asForm()->$method($this->url(), $data);
$url = $this->url();

if (config('paddle.sandbox_environment')) {
$url = str_replace('https://', 'https://sandbox-', $url);
}

$response = Http::asForm()->$method($url, $data);

if (!$response->successful()) {
throw PaddleApiException::unsuccessfulStatus($response->status());
Expand Down
20 changes: 20 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,24 @@ public function it_has_a_custom_host_for_the_checkouts_request()
$request = (new Api)->checkout()->getPrices();
$this->assertEquals('https://checkout.paddle.com/api/2.0/prices', $request->url());
}

/** @test */
public function it_can_set_the_environment_to_sandbox()
{
config([
'paddle' => [
'sandbox_environment' => true,
],
]);

Http::fake(function (Request $request) {
$this->assertEquals('https://sandbox-vendors.paddle.com/api/2.0/product/generate_pay_link', $request->url());

return Http::response([], 200);
});

(new Api)->product()->generatePayLink([
'product_id' => 10,
])->send();
}
}
50 changes: 50 additions & 0 deletions tests/DirectiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace ProtoneMedia\LaravelPaddle\Tests;

use Illuminate\Support\Facades\View;
use Orchestra\Testbench\TestCase;
use ProtoneMedia\LaravelPaddle\PaddleServiceProvider;

class DirectiveTest extends TestCase
{
protected function getPackageProviders($app)
{
return [PaddleServiceProvider::class];
}

/** @test */
public function it_includes_paddle_js_with_the_vendor_id()
{
config([
'paddle' => [
'vendor_id' => 20,
],
]);

View::addLocation(__DIR__);

$rendered = (string) view('dummy');

$this->assertStringNotContainsString("Paddle.Environment.set('sandbox');", $rendered);
$this->assertStringContainsString('Paddle.Setup({"vendor":20});', $rendered);
}

/** @test */
public function it_includes_paddle_js_with_the_sandbox_environment()
{
config([
'paddle' => [
'vendor_id' => 20,
'sandbox_environment' => true,
],
]);

View::addLocation(__DIR__);

$rendered = (string) view('dummy');

$this->assertStringContainsString("Paddle.Environment.set('sandbox');", $rendered);
$this->assertStringContainsString('Paddle.Setup({"vendor":20});', $rendered);
}
}
1 change: 1 addition & 0 deletions tests/dummy.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@paddle

0 comments on commit d95d214

Please sign in to comment.