mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-07-15 11:07:53 -04:00
89 lines
2.6 KiB
PHP
89 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Subscription\Repositories;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\Subscription\Entities\SubscriptionCoupon;
|
|
use Modules\Subscription\Entities\SubscriptionCouponRedemption;
|
|
use Modules\Subscription\Services\StripePaymentService;
|
|
|
|
class SubscriptionCouponsRepository
|
|
{
|
|
private $stripePayment;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->stripePayment = new StripePaymentService();
|
|
}
|
|
|
|
public function all() {
|
|
return SubscriptionCoupon::with('redemptions.user')->withCount('redemptions')->where('active',1)->latest()->get();
|
|
}
|
|
|
|
public function create($request) {
|
|
$data = $request->except('_token');
|
|
|
|
$stripeResponse = $this->stripePayment->createCoupon($data);
|
|
$data['stripe_coupon_id'] = $stripeResponse['id'];
|
|
|
|
//create the promo code
|
|
// $stripeResponse = $this->stripePayment->createPromoCode($stripeResponse['id'], $data);
|
|
// $data['stripe_promo_id'] = $stripeResponse['id'];
|
|
|
|
unset($data['redemptions']);
|
|
unset($data['expiration']);
|
|
|
|
$coupon = new SubscriptionCoupon();
|
|
$coupon->fill($data);
|
|
$coupon->save();
|
|
|
|
return true;
|
|
}
|
|
|
|
public function validate($code, $storefront) {
|
|
|
|
$coupon = SubscriptionCoupon::withCount('redemptions')->where('code',$code)->where('active',1)->first();
|
|
if ($coupon) {
|
|
|
|
$redeemed = SubscriptionCouponRedemption::where('subscription_coupon_id',$coupon->id)->whereHas('user', function( $query ) use ( $storefront ){
|
|
$query->where('wti_data', 'LIKE', '%'.$storefront.'%');
|
|
})->first();
|
|
|
|
if(!$redeemed){
|
|
|
|
$stripeResponse = $this->stripePayment->retrieveCoupon($coupon->stripe_coupon_id);
|
|
|
|
if(isset($stripeResponse['valid']) && $stripeResponse['valid']){
|
|
return $coupon;
|
|
} else {
|
|
return false;
|
|
}
|
|
} else {
|
|
return 'already_redeemed_once';
|
|
}
|
|
}
|
|
|
|
return 'not_found';
|
|
|
|
}
|
|
|
|
|
|
public function delete($id) {
|
|
|
|
$coupon = SubscriptionCoupon::withCount('redemptions')->where('id',$id)->first();
|
|
|
|
if ($coupon) {
|
|
if ($coupon->redemptions_count > 0) {
|
|
return 'in_use';
|
|
} else {
|
|
$this->stripePayment->deleteCoupon($coupon->stripe_coupon_id);
|
|
$coupon->update(['active' => 0]);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|