lnbits paid articles added

This commit is contained in:
HolgerHatGarKeineNode
2023-03-13 16:35:20 +01:00
parent 9f51c71439
commit 639e17080c
30 changed files with 838 additions and 60 deletions

View File

@@ -24,8 +24,9 @@ class NewsArticleForm extends Component
public array $temporaryUrls = [];
public ?string $fromUrl = '';
public bool $paid = false;
protected $queryString = ['fromUrl' => ['except' => '']];
protected $queryString = ['fromUrl' => ['except' => ''], 'paid' => ['except' => false]];
public function rules()
{
@@ -37,6 +38,7 @@ class NewsArticleForm extends Component
'libraryItem.type' => 'required',
'libraryItem.language_code' => 'required',
'libraryItem.value' => 'required',
'libraryItem.sats' => 'required',
'libraryItem.subtitle' => 'string|nullable',
'libraryItem.excerpt' => 'required',
'libraryItem.main_image_caption' => 'string|nullable',
@@ -50,12 +52,14 @@ class NewsArticleForm extends Component
{
if ($this->libraryItem === null) {
$this->libraryItem = new LibraryItem([
'type' => 'markdown_article',
'value' => '',
'read_time' => 1,
'news' => true,
'language_code' => 'de',
'approved' => auth()
'type' => 'markdown_article',
'value' => '',
'value_to_be_paid' => '',
'read_time' => 1,
'sats' => 21,
'news' => true,
'language_code' => 'de',
'approved' => auth()
->user()
->hasRole('news-editor'),
]);

View File

@@ -3,23 +3,72 @@
namespace App\Http\Livewire\News;
use App\Models\LibraryItem;
use App\Traits\LNBitsTrait;
use Carbon\Carbon;
use Livewire\Component;
use RalphJSmit\Laravel\SEO\Support\SEOData;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
class InternArticleView extends Component
{
use LNBitsTrait;
public LibraryItem $libraryItem;
public $qrCode = '';
public $invoice = '';
public $paymentHash = '';
public $checkid = null;
public $checkThisPaymentHash = '';
public bool $invoicePaid = false;
public bool $alreadyPaid = false;
public function mount()
{
$this->libraryItem->load([
'libraries',
]);
if ($this->libraryItem->libraries->where('is_public', false)
->count() > 0 && ! auth()->check()) {
->count() > 0 && !auth()->check()) {
abort(403, __('Sorry! You are not authorized to perform this action.'));
}
if (auth()
->user()
->paidArticles()
->where('library_item_id', $this->libraryItem->id)
->count() > 0) {
$this->invoicePaid = true;
}
}
public function pay()
{
$invoice = $this->createInvoice(
sats: $this->libraryItem->sats,
memo: 'Payment for: "'.$this->libraryItem->slug.'" on Einundzwanzig Portal.'
);
session('payment_hash_article_'.$this->libraryItem->id, $invoice['payment_hash']);
$this->paymentHash = $invoice['payment_hash'];
$this->qrCode = base64_encode(QrCode::format('png')
->size(300)
->merge($this->libraryItem->lecturer->getFirstMedia('avatar') ? $this->libraryItem->lecturer->getFirstMediaPath('avatar') : '/public/img/einundzwanzig.png',
.3)
->errorCorrection('H')
->generate($invoice['payment_request']));
$this->invoice = $invoice['payment_request'];
$this->checkid = $invoice['checking_id'];
}
public function checkPaymentHash()
{
$invoice = $this->check($this->checkid ?? $this->checkThisPaymentHash);
if ($invoice['paid']) {
$this->invoicePaid = true;
auth()
->user()
->paidArticles()
->syncWithoutDetaching($this->libraryItem->id);
}
}
public function render()

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Http\Livewire\Profile;
use Livewire\Component;
use WireUi\Traits\Actions;
class LNBits extends Component
{
use Actions;
public array $settings = [
'url' => 'https://legend.lnbits.com',
'wallet_id' => '',
'read_key' => '',
];
public function rules()
{
return [
'settings.url' => 'required|url',
'settings.wallet_id' => 'required',
'settings.read_key' => 'required',
];
}
public function mount()
{
if (auth()->user()->lnbits) {
$this->settings = auth()->user()->lnbits;
}
}
public function save()
{
$this->validate();
$user = auth()->user();
$user->lnbits = $this->settings;
$user->save();
$this->notification()
->success(__('LNBits settings saved successfully!'));
}
public function render()
{
return view('livewire.profile.l-n-bits');
}
}

View File

@@ -31,20 +31,22 @@ class LibraryItem extends Model implements HasMedia, Sortable, Feedable
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'id' => 'integer',
'lecturer_id' => 'integer',
'library_id' => 'integer',
'library_id' => 'integer',
];
protected $hidden = [
'value_to_be_paid',
];
public static function getFeedItems()
@@ -63,7 +65,7 @@ class LibraryItem extends Model implements HasMedia, Sortable, Feedable
protected static function booted()
{
static::creating(function ($model) {
if (! $model->created_by) {
if (!$model->created_by) {
$model->created_by = auth()->id();
}
});

View File

@@ -12,6 +12,7 @@ use Laravel\Jetstream\HasTeams;
use Laravel\Sanctum\HasApiTokens;
use ParagonIE\CipherSweet\BlindIndex;
use ParagonIE\CipherSweet\EncryptedRow;
use ParagonIE\CipherSweet\JsonFieldMap;
use QCod\Gamify\Gamify;
use Spatie\Comments\Models\Concerns\InteractsWithComments;
use Spatie\Comments\Models\Concerns\Interfaces\CanComment;
@@ -63,12 +64,18 @@ class User extends Authenticatable implements MustVerifyEmail, CanComment, Ciphe
public static function configureCipherSweet(EncryptedRow $encryptedRow): void
{
$map = (new JsonFieldMap())
->addTextField('url')
->addTextField('read_key')
->addTextField('wallet_id');
$encryptedRow
->addField('public_key')
->addField('lightning_address')
->addField('lnurl')
->addField('node_id')
->addField('email')
->addJsonField('lnbits', $map)
->addBlindIndex('public_key', new BlindIndex('public_key_index'))
->addBlindIndex('lightning_address', new BlindIndex('lightning_address_index'))
->addBlindIndex('lnurl', new BlindIndex('lnurl_index'))
@@ -95,4 +102,9 @@ class User extends Authenticatable implements MustVerifyEmail, CanComment, Ciphe
{
return $this->hasMany(Vote::class);
}
public function paidArticles()
{
return $this->belongsToMany(LibraryItem::class, 'library_item_user', 'user_id', 'library_item_id');
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Traits;
use Illuminate\Support\Facades\Http;
trait LNBitsTrait
{
public function createInvoice($sats, $memo)
{
$lnbits = auth()->user()->lnbits;
$response = Http::withHeaders([
'X-Api-Key' => $lnbits['read_key'],
])
->post($lnbits['url'].'/api/v1/payments', [
'out' => false,
'amount' => $sats,
'memo' => $memo,
]);
return $response->json();
}
public function check($paymentHash)
{
$lnbits = auth()->user()->lnbits;
$response = Http::withHeaders([
'X-Api-Key' => $lnbits['read_key'],
])
->get($lnbits['url'].'/api/v1/payments/' . $paymentHash);
return $response->json();
}
}