Files
wticreatorstudio/Modules/Subscription/Repositories/SubscriptionPlanPricesRepository.php
T
2024-02-12 22:54:20 -05:00

83 lines
2.2 KiB
PHP

<?php
namespace Modules\Subscription\Repositories;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Modules\Subscription\Entities\SubscriptionPlan;
use Modules\Subscription\Entities\SubscriptionPlanPrice;
use Modules\Subscription\Services\StripePaymentService;
use Modules\Subscription\Services\PaypalPaymentService;
class SubscriptionPlanPricesRepository
{
private $stripePayment;
private $paypalPayment;
public function __construct()
{
$this->stripePayment = new StripePaymentService();
$this->paypalPayment = new PaypalPaymentService();
}
public function create($plan, $request) {
$data = $request->except('_token');
$paypalResponse = $this->paypalPayment->createProductPrice($plan,$data);
$data['paypal_price_id'] = $paypalResponse->id;
$stripeResponse = $this->stripePayment->createProductPrice($plan->stripe_product_id,$data);
$data['stripe_price_id'] = $stripeResponse['id'];
$planPrice = new SubscriptionPlanPrice();
$planPrice->fill($data);
$planPrice->save();
return true;
}
public function update($request) {
$data = $request->except('_token','subscription_plan_id','created_at','updated_at');
$price = SubscriptionPlanPrice::find($data['id']);
if ($price) {
$paypalResponse = $this->paypalPayment->updateProductPrice($price,$data);
$data['paypal_price_id'] = $paypalResponse->id;
$stripeResponse = $this->stripePayment->updateProductPrice($price,$data);
$data['stripe_price_id'] = $stripeResponse['id'];
$data['subscription_plan_id'] = $price->subscription_plan_id;
//soft delete
$price->delete();
$planPrice = new SubscriptionPlanPrice();
$planPrice->fill($data);
$planPrice->save();
return true;
}
return false;
}
public function delete($id) {
$price = SubscriptionPlanPrice::find($id);
if ($price) {
$this->stripePayment->archivePrice($price->stripe_price_id);
$price->delete();
return true;
}
return false;
}
}