Files
wticreatorstudio/Modules/RegistrationFormSteps/Services/StripePaymentService.php
T
Leonard Biano cc0b7fc15b Registration Module Update
Set company name on stripe
2024-06-06 19:12:21 +08:00

108 lines
4.0 KiB
PHP

<?php
namespace Modules\RegistrationFormSteps\Services;
use Modules\Subscription\Entities\SubscriptionPlan;
use Modules\Subscription\Repositories\SubscriptionCouponsRepository;
use Carbon\Carbon;
class StripePaymentService
{
protected $stripePayment;
protected $currecyCode;
private $subscriptionCouponsRepository;
public function __construct()
{
$credential = getPaymentInfoViaSellerId(1, 4);
$this->stripePayment = new \Stripe\StripeClient([
"api_key" => @$credential->perameter_3,
"stripe_version" => "2022-11-15"
]);
$this->currecyCode = getCurrencyCode();
$this->subscriptionCouponsRepository = new SubscriptionCouponsRepository();
}
public function payment($data, $planPrice)
{
try {
$customer = $this->stripePayment->customers->create([
"email" => $data['email'],
"name" => $data['company_name'],
"source" => $data['stripe_token'],
"metadata" => array(
"full_name" => $data['first_name'].' '.$data['last_name'],
"phone" => $data['phone']
),
]);
if($planPrice->billing_type == 'Recurring'){
//if($data['payment_mode'] == 'monthly'){
if($planPrice->payment == 'monthly'){
$price = $planPrice->price / $planPrice->custom_interval;
} else {
$price = $planPrice->price;
}
$subscriptionData = [
'customer' => $customer['id'],
'items' => [
[
'price_data' => [
'unit_amount_decimal' => round($price * 100,2),
'currency' => 'usd',
'product' => $planPrice->plan->stripe_product_id,
'recurring' => [
'interval' => $planPrice->payment == 'monthly' ? 'month' : 'year'
],
]
],
],
//'cancel_at' => Carbon::now()->addMonths($planPrice->custom_interval)->addDays($planPrice->trial_period ? $planPrice->trial_duration : 7)->timestamp,
'trial_period_days' => $planPrice->trial_period ? $planPrice->trial_duration : 7
];
// } else {
// $subscriptionData = [
// 'customer' => $customer['id'],
// 'items' => [
// ['price' => $planPrice->stripe_price_id],
// ],
// 'trial_period_days' => $planPrice->trial_period ? $planPrice->trial_duration : 7
// ];
// }
$promoCode = $this->subscriptionCouponsRepository->validate($data['promo_code'], $data['storefront']);
if(isset($promoCode->stripe_coupon_id) && in_array($planPrice->id, $promoCode->subscription_plan_price_id)){
$subscriptionData['coupon'] = $promoCode->stripe_coupon_id;
}
$stripeResponse = $this->stripePayment->subscriptions->create($subscriptionData);
} else {
$stripeResponse = $this->stripePayment->charges->create([
"amount" => round($plan->price * 100),
"currency" => $this->currecyCode,
'customer' => $customer['id'],
"description" => "Payment for ". $plan->title,
"capture" => false
]);
}
return $stripeResponse;
} catch(Exception $e){
session()->put('payment_error', $e->getMessage());
return false;
}
}
}