Skip to content

Commit

Permalink
Fix the cancel and update buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyiliev committed Mar 25, 2024
1 parent 4a841ba commit e0148f1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="flex flex-col">
<h5 class="mb-2 text-xl font-bold text-gray-700">Modify Payment Information</h5>
<p>Click the button below to update your default payment method</p>
<button data-url="{{ auth()->user()->subscription->update_url }}" class="inline-flex self-start justify-center w-auto px-4 py-2 mt-5 text-sm font-medium text-white transition duration-150 ease-in-out border border-transparent rounded-md checkout-update bg-wave-600 hover:bg-wave-500 focus:outline-none focus:border-wave-700 focus:shadow-outline-wave active:bg-wave-700">Update Payment Info</button>
<a href="{{ auth()->user()->subscription->update_url }}" class="inline-flex self-start justify-center w-auto px-4 py-2 mt-5 text-sm font-medium text-white transition duration-150 ease-in-out border border-transparent rounded-md checkout-update bg-wave-600 hover:bg-wave-500 focus:outline-none focus:border-wave-700 focus:shadow-outline-wave active:bg-wave-700">Update Payment Info</a>
</div>

<hr class="my-8 border-gray-200">
Expand Down
151 changes: 71 additions & 80 deletions wave/src/Http/Controllers/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,102 +86,93 @@ private function cancelSubscription(){
}

public function checkout(Request $request){
$response = Http::withToken($this->vendor_auth_code)->get($this->paddle_url . '/transactions/' . $request->checkout_id);
$retryCount = 5;
$initialDelay = 2;
$transaction = null;
$status = 0;
$message = '';
$guest = (auth()->guest()) ? 1 : 0;

if($response->successful()){
$resBody = json_decode($response->body());

if(isset($resBody->data->status)){
$transaction = $resBody->data;
if (is_null($transaction->subscription_id)) {
sleep(4);
$response = Http::withToken($this->vendor_auth_code)->get($this->paddle_url . '/transactions/' . $request->checkout_id);
$resBody = json_decode($response->body());
for ($i = 0; $i < $retryCount; $i++) {
$response = Http::withToken($this->vendor_auth_code)->get($this->paddle_url . '/transactions/' . $request->checkout_id);

if ($response->successful()) {
$resBody = json_decode($response->body());
if (isset($resBody->data->status) && !is_null($resBody->data->subscription_id)) {
$transaction = $resBody->data;
break;
}
$plans = Plan::all();

if($transaction->origin === "web" && $plans->contains('plan_id', $transaction->items[0]->price->id)){
$subscriptionUser = Http::withToken($this->vendor_auth_code)->get($this->paddle_url . '/subscriptions/' . $transaction->subscription_id);
$subscriptionData = json_decode($subscriptionUser->body());
if (isset($resBody->data)) {
$subscription = $subscriptionData->data;
} else {
// Retry fetching the subscription data
sleep(4);
$subscriptionUser = Http::withToken($this->vendor_auth_code)->get($this->paddle_url . '/subscriptions/' . $transaction->subscription_id);
$subscriptionData = json_decode($subscriptionUser->body());
$subscription = $subscriptionData->data;
}

// Fetch customer's details using the customer_id
$customerResponse = Http::withToken($this->vendor_auth_code)->get($this->paddle_url . '/customers/' . $subscription->customer_id);
$customerData = json_decode($customerResponse->body());
$customerEmail = $customerData->data->email;
$customerName = $customerData->data->name;
// Check if the name is null or empty
if (empty($customerName)) {
// Extract the part of the email address before the '@' symbol
$nameParts = explode('@', $customerEmail);
$customerName = $nameParts[0];
}

if(auth()->guest()){
if(User::where('email', $customerEmail)->exists()){
$user = User::where('email', $customerEmail)->first();
} else {
// create a new user
$registration = new \Wave\Http\Controllers\Auth\RegisterController;
$user_data = [
'name' => $customerName,
'email' => $customerEmail,
'password' => Hash::make(uniqid())
];
$user = $registration->create($user_data);
Auth::login($user);
}
}

sleep($initialDelay * (2 ** $i));
}

if ($transaction) {
// Proceed with processing the transaction
$plans = Plan::all();
if($transaction->origin === "web" && $plans->contains('plan_id', $transaction->items[0]->price->id)){
$subscriptionUser = Http::withToken($this->vendor_auth_code)->get($this->paddle_url . '/subscriptions/' . $transaction->subscription_id);
$subscriptionData = json_decode($subscriptionUser->body());
$subscription = $subscriptionData->data;

$customerResponse = Http::withToken($this->vendor_auth_code)->get($this->paddle_url . '/customers/' . $subscription->customer_id);
$customerData = json_decode($customerResponse->body());
$customerEmail = $customerData->data->email;
$customerName = $customerData->data->name;
if (empty($customerName)) {
$nameParts = explode('@', $customerEmail);
$customerName = $nameParts[0];
}

if($guest){
if(User::where('email', $customerEmail)->exists()){
$user = User::where('email', $customerEmail)->first();
} else {
$user = auth()->user();
$registration = new \Wave\Http\Controllers\Auth\RegisterController;
$user_data = [
'name' => $customerName,
'email' => $customerEmail,
'password' => Hash::make(uniqid())
];
$user = $registration->create($user_data);
Auth::login($user);
}

$plan = Plan::where('plan_id', $transaction->items[0]->price->id)->first();

// add associated role to user
$user->role_id = $plan->role_id;
$user->save();

$subscription = PaddleSubscription::create([
'subscription_id' => $transaction->subscription_id,
'plan_id' => $transaction->items[0]->price->product_id,
'user_id' => $user->id,
'status' => $subscription->status,
'last_payment_at' => $subscription->first_billed_at,
'next_payment_at' => $subscription->next_billed_at,
'cancel_url' => $subscription->management_urls->cancel,
'update_url' => $subscription->management_urls->update_payment_method
]);

$status = 1;
} else {
$message = 'Error locating that subscription product id. Please contact us if you think this is incorrect.';
$user = auth()->user();
}

$plan = Plan::where('plan_id', $transaction->items[0]->price->id)->first();

// Update user role based on plan
$user->role_id = $plan->role_id;
$user->save();

// Create or update subscription details
$subscriptionRecord = PaddleSubscription::create([
'subscription_id' => $transaction->subscription_id,
'plan_id' => $transaction->items[0]->price->product_id,
'user_id' => $user->id,
'status' => $subscription->status,
'last_payment_at' => $subscription->first_billed_at,
'next_payment_at' => $subscription->next_billed_at,
'cancel_url' => $subscription->management_urls->cancel,
'update_url' => $subscription->management_urls->update_payment_method
]);

$status = 1;
} else {
$message = 'Error locating that order. Please contact us if you think this is incorrect.';
$message = 'Error locating that subscription product id. Please contact us if you think this is incorrect.';
}
} else {
$message = $response->serverError();
$message = 'Error processing the transaction. Please try again.';
}

return response()->json([
'status' => $status,
'message' => $message,
'guest' => $guest
]);
}

'status' => $status,
'message' => $message,
'guest' => $guest
]);
}

public function invoices(User $user){

Expand Down

0 comments on commit e0148f1

Please sign in to comment.