Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support vendors in blade directive and Request #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/views/javaScriptSetup.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
@if(config('paddle.sandbox_environment'))
Paddle.Environment.set('sandbox');
@endif
Paddle.Setup(@json(['vendor' => (int) config('paddle.vendor_id')]));
Paddle.Setup(@json(['vendor' => (int) $vendorId]));
</script>
4 changes: 3 additions & 1 deletion src/Api/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ public function send()

$method = $this->method;

$data = $this->getData() + ['vendor_id' => config('paddle.vendor_id')];
$data = ['vendor_id' => config('paddle.vendor_id')];

if ($method === static::METHOD_POST) {
$data['vendor_auth_code'] = config('paddle.vendor_auth_code');
}

$data = array_merge($data, $this->getData());

$url = $this->url();

if (config('paddle.sandbox_environment')) {
Expand Down
11 changes: 9 additions & 2 deletions src/PaddleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ public function boot()

$this->loadViewsFrom(__DIR__ . '/../resources/views', 'paddle');

Blade::directive('paddle', function () {
return "<?php echo view('paddle::javaScriptSetup'); ?>";

Blade::directive('paddle', function ($vendorId = false) {
if (!$vendorId) {
$vendorId = config('paddle.vendor_id');
}

return "<?php
echo view('paddle::javaScriptSetup', ['vendorId' => " . $vendorId . "]);
?>";
});
}

Expand Down
15 changes: 15 additions & 0 deletions tests/DirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,19 @@ public function it_includes_paddle_js_with_the_sandbox_environment()
$this->assertStringContainsString("Paddle.Environment.set('sandbox');", $rendered);
$this->assertStringContainsString('Paddle.Setup({"vendor":20});', $rendered);
}

/** @test */
public function it_includes_paddle_js_with_vendor_id()
{
View::addLocation(__DIR__);
config([
'paddle' => [
'vendor_id' => 20,
],
]);

$rendered = (string) view('dummy-vendor', ['vendor' => 50]);

$this->assertStringContainsString('Paddle.Setup({"vendor":50});', $rendered);
}
}
27 changes: 27 additions & 0 deletions tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,31 @@ public function it_throws_an_exception_if_the_given_data_is_invalid()

$this->fail('Should have thrown InvalidDataException');
}

/** @test */
public function it_can_set_the_vendor_keys_into_payload_by_array()
{
config([
'paddle' => [
'vendor_id' => 20,
'vendor_auth_code' => '123',
],
]);
$request = (new Api)->product()->generatePayLink([
'product_id' => 10,
'customer_email' => '[email protected]',
'passthrough' => ['team_id' => 20],
'vendor_id' => 50,
'vendor_auth_code' => 'efg'
]);

$this->assertEquals([
'product_id' => 10,
'customer_email' => '[email protected]',
'passthrough' => json_encode(['team_id' => 20]),
'vendor_id' => 50,
'vendor_auth_code' => 'efg'

], $request->getData());
}
}
1 change: 1 addition & 0 deletions tests/dummy-vendor.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@paddle($vendor)