Files
wticreatorstudio/Modules/RegistrationFormSteps/Services/PaypalPaymentService.php
Fritz Ramirez 10d0c477c8 Initial commit
2024-02-12 22:54:20 -05:00

281 lines
10 KiB
PHP

<?php
namespace Modules\RegistrationFormSteps\Services;
use Modules\Subscription\Entities\SubscriptionPlan;
use Modules\Subscription\Entities\SubscriptionPlanPricePaypal;
use Modules\Subscription\Repositories\SubscriptionCouponsRepository;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Carbon\Carbon;
class PaypalPaymentService
{
protected $apiBaseUrl;
protected $accessToken;
protected $subscriptionCouponsRepository;
public function __construct()
{
$this->apiBaseUrl = 'https://api-m.paypal.com'; // live
//$this->apiBaseUrl = 'https://api-m.sandbox.paypal.com'; // sandbox
$this->subscriptionCouponsRepository = new SubscriptionCouponsRepository();
}
private function makeAccessToken() {
$credential = getPaymentInfoViaSellerId(1, 3);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_USERPWD, $credential->perameter_2 . ':' . $credential->perameter_3);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
if (!empty($result->access_token)) {
$this->accessToken = $result->access_token;
return true;
}
return false;
}
public function payment($data, $planPrice)
{
if(!$this->makeAccessToken()){
return false;
}
$cache_key = Str::uuid();
Cache::put($cache_key,$data);
$subscription_data = [
'quantity' => 1,
'subscriber' => [
'email_address' => $data['email'],
'name' => [
'given_name' => $data['first_name'],
'surname' => $data['last_name']
]
],
'application_context' => [
'brand_name' => 'WTI',
'shipping_preference' => 'NO_SHIPPING',
'user_action' => 'CONTINUE',
'payment_method' => [
'payer_selected' => 'PAYPAL',
'payee_preferred' => 'IMMEDIATE_PAYMENT_REQUIRED'
],
'return_url' => url('/register/paypal'),
'cancel_url' => url('/register')
]
];
$promoCode = $this->subscriptionCouponsRepository->validate($data['promo_code'], $data['storefront']);
if(isset($promoCode->stripe_coupon_id) && in_array($planPrice->id, $promoCode->subscription_plan_price_id)){
$payment_details = $planPrice->plan->title;
$price_monthly = $planPrice->price / $planPrice->custom_interval;
if ($promoCode->type == 'Amount') {
$discounted_price_monthly = number_format($price_monthly - $promoCode->discount,2);
$discounted_price_all = number_format($planPrice->price - $promoCode->discount,2);
} else {
$discounted_price_monthly = number_format($price_monthly - (($promoCode->discount / 100) * $price_monthly),2);
$discounted_price_all = number_format($planPrice->price - (($promoCode->discount / 100) * $planPrice->price),2);
}
if($planPrice->payment == 'monthly'){
$payment_details .= $promoCode->duration == 'Once' ? ' - $'.number_format($planPrice->price / $planPrice->custom_interval,2).' for '.$planPrice->custom_interval.' Months ($'.$discounted_price_monthly.' 1st month only)' : ' - '.$planPrice->custom_interval.' Months ($'.$discounted_price_monthly.' per month)';
} else {
$payment_details .= '($'.$discounted_price_all.' for '.$planPrice->custom_interval.' Months)';
}
//lets create new plan price with discount
$response = $this->createProductPriceWithDiscount($planPrice,$promoCode->duration,$discounted_price_monthly,$discounted_price_all,$payment_details);
if(isset($response->id)){
$subscription_data['plan_id'] = $response->id;
$subscription_data['custom_id'] = json_encode([
'cache_key' => $cache_key,
'coupon_id' => $promoCode->stripe_coupon_id
]);
$paypalPlanPrice = new SubscriptionPlanPricePaypal;
$paypalPlanPrice->subscription_plan_price_id = $planPrice->id;
$paypalPlanPrice->paypal_price_id = $response->id;
$paypalPlanPrice->save();
} else {
return response()->json([
'message' => 'Something went wrong, please try again later.'
], 404);
}
} else {
$subscription_data['plan_id'] = $planPrice->paypal_price_id;
$subscription_data['custom_id'] = json_encode([
'cache_key' => $cache_key
]);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/billing/subscriptions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($subscription_data,true));
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$this->accessToken;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = json_decode(curl_exec($ch));
if (curl_errno($ch)) {
$response->msg = curl_error($ch);
curl_close($ch);
return $response;
}
curl_close($ch);
return $result;
}
public function showSubscription($subscription_id)
{
if(!$this->makeAccessToken()){
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/billing/subscriptions/'.$subscription_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$this->accessToken;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = json_decode(curl_exec($ch));
if (curl_errno($ch)) {
$response->msg = curl_error($ch);
curl_close($ch);
return $response;
}
curl_close($ch);
return $result;
}
public function createProductPriceWithDiscount($planPrice, $duration, $discounted_price_monthly, $discounted_price_all, $payment_details)
{
if(!$this->makeAccessToken()){
return false;
}
$custom_intervals = [
'Months' => 'Month',
'Years' => 'Year'
];
$post_data = [
'product_id' => $planPrice->plan->paypal_product_id,
'name' => $payment_details,
'status' => 'ACTIVE',
'payment_preferences' => [
'setup_fee_failure_action' => 'CONTINUE'
]
];
if($duration == 'Once' && $planPrice->payment == 'monthly'){
$post_data['billing_cycles'] = [
[
'frequency' => [
'interval_unit' => $custom_intervals[$planPrice->custom_interval_type],
'interval_count' => ($planPrice->payment != 'upfront' ? 1 : $planPrice->custom_interval)
],
'tenure_type' => 'TRIAL',
'sequence' => 1,
'total_cycles' => 1,
'pricing_scheme' => [
'fixed_price' => [
'currency_code' => getCurrencyCode(),
'value' => (float)($planPrice->payment == 'monthly' ? $discounted_price_monthly : $discounted_price_all)
]
],
],
[
'frequency' => [
'interval_unit' => $custom_intervals[$planPrice->custom_interval_type],
'interval_count' => ($planPrice->payment != 'upfront' ? 1 : $planPrice->custom_interval)
],
'tenure_type' => 'REGULAR',
'sequence' => 2,
'total_cycles' => ($planPrice->custom_interval - 1),
'pricing_scheme' => [
'fixed_price' => [
'currency_code' => getCurrencyCode(),
'value' => (float)($planPrice->price / $planPrice->custom_interval)
]
],
]
];
} else {
$post_data['billing_cycles'] = [
[
'frequency' => [
'interval_unit' => $custom_intervals[$planPrice->custom_interval_type],
'interval_count' => ($planPrice->payment != 'upfront' ? 1 : $planPrice->custom_interval)
],
'tenure_type' => 'REGULAR',
'sequence' => 1,
'total_cycles' => ($planPrice->payment == 'upfront' ? 1 : $planPrice->custom_interval),
'pricing_scheme' => [
'fixed_price' => [
'currency_code' => getCurrencyCode(),
'value' => ($planPrice->payment == 'monthly' ? $discounted_price_monthly : $discounted_price_all)
]
],
]
];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/billing/plans');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data,true));
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$this->accessToken;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = json_decode(curl_exec($ch));
if (curl_errno($ch)) {
$response->msg = curl_error($ch);
curl_close($ch);
return $response;
}
curl_close($ch);
return $result;
}
}