Skip to content

Commit 142e814

Browse files
committed
init
0 parents  commit 142e814

9 files changed

Lines changed: 165 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
.idea
3+
/vendor

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# BrainBlocks
2+
An integration helper for taking Nano payments via BrainBlocks.io
3+
4+
This package adds a few helpers when adding a BrainBlocks button with Laravel
5+
6+
# Installation
7+
8+
```
9+
composer require binarycabin/brainblocks
10+
```

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "binarycabin/brainblocks",
3+
"description": "An integration helper for taking Nano payments via BrainBlocks",
4+
"type": "library",
5+
"require": {
6+
"guzzlehttp/guzzle": "^6.3"
7+
},
8+
"license": "MIT",
9+
"authors": [
10+
{
11+
"name": "Jeff Kilroy",
12+
"email": "jeff@binarycabin.com"
13+
}
14+
],
15+
"autoload": {
16+
"psr-4": {
17+
"BinaryCabin\\BrainBlocks\\": "src/"
18+
}
19+
},
20+
"extra": {
21+
"laravel": {
22+
"providers": [
23+
"BinaryCabin\\BrainBlocks\\Providers\\BrainBlocksServiceProvider"
24+
],
25+
"aliases": {
26+
"BrainBlocks": "BinaryCabin\\BrainBlocks\\Facades\\BrainBlocks"
27+
}
28+
}
29+
}
30+
}

src/BrainBlocks.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace BinaryCabin\BrainBlocks;
4+
5+
class BrainBlocks{
6+
7+
public static function getTokenResponse($token){
8+
$client = new \GuzzleHttp\Client();
9+
$request = $client->request('GET', 'https://brainblocks.io/api/session/'.$token.'/verify', []);
10+
$contents = $request->getBody()->getContents();
11+
return json_decode($contents);
12+
}
13+
14+
public static function validateResponse($response,$rules=[]){
15+
$defaultRules = [
16+
'fulfilled' => true,
17+
'destination' => config('brainblocks.receive-address')
18+
];
19+
$rules = array_merge($defaultRules, $rules);
20+
foreach($rules as $ruleProperty => $ruleValue){
21+
if(empty($response->$ruleProperty) || $response->$ruleProperty != $ruleValue){
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
28+
}

src/Facades/BrainBlocks.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace BinaryCabin\BrainBlocks\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class BrainBlocks extends Facade
8+
{
9+
protected static function getFacadeAccessor() {
10+
return 'brainblocks';
11+
}
12+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace BinaryCabin\BrainBlocks\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use BinaryCabin\BrainBlocks\BrainBlocks;
7+
8+
class BrainBlocksServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Bootstrap the application services.
12+
*
13+
* @return void
14+
*/
15+
public function boot()
16+
{
17+
$this->publishes([
18+
__DIR__.'/../config/brainblocks.php' => config_path('brainblocks.php'),
19+
]);
20+
$this->loadViewsFrom(__DIR__.'/../resources/views', 'brainblocks');
21+
}
22+
23+
/**
24+
* Register the application services.
25+
*
26+
* @return void
27+
*/
28+
public function register()
29+
{
30+
$this->registerBrainBlocks();
31+
$this->mergeConfigFrom(
32+
__DIR__.'/../config/brainblocks.php', 'brainblocks'
33+
);
34+
}
35+
36+
public function registerBrainBlocks(){
37+
$this->app->bind('brainblocks',function() {
38+
return new BrainBlocks();
39+
});
40+
}
41+
42+
public function provides()
43+
{
44+
return array('brainblocks', BrainBlocks::class);
45+
}
46+
47+
48+
49+
}

src/config/brainblocks.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
return [
3+
'receive-address' => env('BRAINBLOCKS_RECEIVE_ADDRESS'),
4+
];
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php $brainBlocksButtonId = 'BB'.rand(100000,999999); ?>
2+
<div rel="brainblocks-button" data-id="{{ $brainBlocksButtonId }}" data-amount-rai="{{ $amountRai }}" id="{{ $brainBlocksButtonId }}-button"></div>
3+
<form action="{{ $action }}" method="POST" id="{{ $brainBlocksButtonId }}-form">
4+
<input type="hidden" name="brainblocks_token" id="{{ $brainBlocksButtonId }}-token" value="" />
5+
{{ csrf_field() }}
6+
</form>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script src="https://brainblocks.io/brainblocks.min.js"></script>
2+
<script>
3+
var brainBlocksElements = document.querySelectorAll("[rel='brainblocks-button']");
4+
if(brainBlocksElements.length > 0){
5+
console.log(brainBlocksElements);
6+
for (i = 0; i < brainBlocksElements.length; ++i) {
7+
var brainBlocksElement = brainBlocksElements[i];
8+
var brainBlocksButtonId = brainBlocksElement.dataset.id;
9+
var brainBlocksButtonAmountRai = brainBlocksElement.dataset.amountRai;
10+
var brainBlocksRenderer = brainblocks.Button.render({
11+
payment: {
12+
destination: "{{ config('brainblocks.receive-address') }}",
13+
currency: 'rai',
14+
amount: brainBlocksButtonAmountRai
15+
},
16+
onPayment: function (data) {
17+
document.getElementById(brainBlocksButtonId+"-token").value = data.token;
18+
document.getElementById(brainBlocksButtonId+"-form").submit();
19+
}
20+
}, '#'+brainBlocksButtonId+'-button');
21+
}
22+
}
23+
</script>

0 commit comments

Comments
 (0)