Working on subscriptions

This commit is contained in:
= 2021-04-04 22:14:33 +10:00
parent f1f8201683
commit 858c99e09d
2 changed files with 53 additions and 13 deletions

View File

@ -20,6 +20,16 @@ class Subscription extends BaseModel
{ {
use HasFactory, SoftDeletes; use HasFactory, SoftDeletes;
protected $hidden = [
'id',
'user_id',
'assigned_user_id',
'company_id',
'product_ids',
'recurring_product_ids',
'group_id',
];
protected $fillable = [ protected $fillable = [
'assigned_user_id', 'assigned_user_id',
'product_ids', 'product_ids',

View File

@ -27,6 +27,7 @@ use App\Models\SystemLog;
use App\Repositories\InvoiceRepository; use App\Repositories\InvoiceRepository;
use App\Repositories\RecurringInvoiceRepository; use App\Repositories\RecurringInvoiceRepository;
use App\Repositories\SubscriptionRepository; use App\Repositories\SubscriptionRepository;
use App\Utils\Ninja;
use App\Utils\Traits\CleanLineItems; use App\Utils\Traits\CleanLineItems;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use GuzzleHttp\RequestOptions; use GuzzleHttp\RequestOptions;
@ -70,7 +71,13 @@ class SubscriptionService
->save(); ->save();
//execute any webhooks //execute any webhooks
$this->triggerWebhook();
$context = [
'context' => 'recurring_purchase',
'recurring_invoice' => $recurring_invoice->hashed_id,
];
$this->triggerWebhook($context);
if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1) if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1)
return redirect($this->subscription->webhook_configuration['post_purchase_url']); return redirect($this->subscription->webhook_configuration['post_purchase_url']);
@ -80,8 +87,13 @@ class SubscriptionService
else else
{ {
$context = [
'context' => 'single_purchase',
'invoice' => $this->encodePrimaryKey($payment_hash->fee_invoice_id),
];
//execute any webhooks //execute any webhooks
$this->triggerWebhook(); $this->triggerWebhook($context);
if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1) if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1)
return redirect($this->subscription->webhook_configuration['post_purchase_url']); return redirect($this->subscription->webhook_configuration['post_purchase_url']);
@ -124,8 +136,13 @@ class SubscriptionService
->start() ->start()
->save(); ->save();
$context = [
'context' => 'trial',
'recurring_invoice' => $recurring_invoice->hashed_id,
];
//execute any webhooks //execute any webhooks
$this->triggerWebhook(); $this->triggerWebhook($context);
if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1) if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1)
return redirect($this->subscription->webhook_configuration['post_purchase_url']); return redirect($this->subscription->webhook_configuration['post_purchase_url']);
@ -208,22 +225,35 @@ class SubscriptionService
// } // }
//@todo - need refactor //@todo - need refactor
public function triggerWebhook() public function triggerWebhook($context)
{ {
//context = 'trial, recurring_purchase, single_purchase'
//hit the webhook to after a successful onboarding //hit the webhook to after a successful onboarding
// $body = [ $body = [
// 'subscription' => $this->subscription, 'subscription' => $this->subscription->toArray(),
// 'client_subscription' => $this->client_subscription, 'client' => $this->client_subscription->client->toArray(),
// 'client' => $this->client_subscription->client->toArray(), ];
// ];
$body = array_merge($body, $context);
if(Ninja::isHosted())
{
$hosted = [
'company' => $this->subscription->company,
];
// $client = new \GuzzleHttp\Client(['headers' => $this->subscription->webhook_configuration->post_purchase_headers]); $body = array_merge($body, $hosted);
}
// $response = $client->{$this->subscription->webhook_configuration->post_purchase_rest_method}($this->subscription->post_purchase_url,[ $client = new \GuzzleHttp\Client(
// RequestOptions::JSON => ['body' => $body] [
// ]); 'headers' => $this->subscription->webhook_configuration['post_purchase_headers']
]);
$response = $client->{$this->subscription->webhook_configuration['post_purchase_rest_method']($this->subscription['post_purchase_url'],[
RequestOptions::JSON => ['body' => $body]
]);
// SystemLogger::dispatch( // SystemLogger::dispatch(
// $body, // $body,