Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bill for non-catalog items #263

Merged
merged 31 commits into from
Jul 5, 2024
Merged

Bill for non-catalog items #263

merged 31 commits into from
Jul 5, 2024

Conversation

AcTiv3MineD
Copy link
Contributor

@AcTiv3MineD AcTiv3MineD commented Jun 5, 2024

This PR covers the functionality needed on this issue: #230

NEW PR DESCRIPTION

Product Checkout

// Charges 10.00
$user->charge(1000, 'T-shirt');

Specify quantity

// Charges 30.00 (10 * 3)
$user->charge(1000, 'T-shirt', ['quantity' => 3]);

Product subscription

// Charges 30.00 (10 * 3 seats) each year
$user->newSubscription(1000, 'Gym subscription')
    ->yearly()
    ->quantity(3) // Seats
    ->checkout();

The default currency is USD, to change it, you can either:
1 - Set CASHIER_CURRENCY=currency on your .env
2 - Pass currency as an option, ex:

// Charges 50.00 (10 * 5)
$user->charge(1000, 'T-shirt', ['price' => ['unit_price' => ['currency_code' => 'EUR']], 'quantity' => 5]);

OLD PR DESCRIPTION

Notes:

1- Right now, only the option of calling the create transaction api is supported, adding the new structure of items as a attribute on the html does not work. I have contacted Paddle's team and they say that they don't have a date set for this functionality.

This has a drawback, as:
A- Every time we render the button, it creates an incomplete transaction on Paddle.
B- Each time we call the button component, an api request is made to paddle, thus, it would slow the rendering time.

2-This solution allows to use the exiting cashier functionality, plus create custom transactions, allowing to create custom pricing subscriptions and checkout, ex:

<?php

Route::get('/buy', function (Request $request) {
    $checkout = $request->user()->checkoutFromTransaction([
               'items' => [
                    [
                        'price' => [
                            'description' => 'New custom product price',
                            'name' => 'My custom price',
                            'unit_price' => [
                                'amount' => '1099',
                                'currency_code' => 'USD',
                            ],
                            'product' => [
                                'name' => 'Custom product',
                                'tax_category' => 'standard',
                                'description' => 'Start the game with 20 extra seconds play time!',
                            ],
                        ],
                        'quantity' => 1,
                    ],
                ],
        ])
        ->returnTo(route('dashboard'));
 
    return view('buy', ['checkout' => $checkout]);
})->name('checkout');

Copy link

github-actions bot commented Jun 5, 2024

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@AcTiv3MineD AcTiv3MineD changed the title Add support for non-catalog items Create Transaction Jun 21, 2024
@AcTiv3MineD AcTiv3MineD marked this pull request as ready for review June 21, 2024 03:37
@driesvints driesvints marked this pull request as draft June 21, 2024 07:25
@driesvints
Copy link
Member

Thanks @AcTiv3MineD. Drafting this until I get a chance to review.

@driesvints driesvints linked an issue Jul 2, 2024 that may be closed by this pull request
@driesvints
Copy link
Member

@AcTiv3MineD finally found some time. I adjusted the API a little bit. Now you can do like in previous Cashier Paddle v1:

$user->charge(1000, 'T-shirt', ['quantity' => 5]);

I didn't test this and will need your help verifying this will work.

@AcTiv3MineD
Copy link
Contributor Author

@driesvints Thanks for the changes, I like the new structure, I will be testing it during the week.

I have 2 comments about the new changes:

1 - About the charge name, I think it might create confusion with the existing method on src/Subscription.php.
Would you agree to find a different name?

2 - These changes remove the option of creating subscriptions for non-catalog items.
Looking at the code there is a workaround by using chargeMany which gives complete control over the array structure, allowing to specify billing_cycle key on the item's price.

Should we add a method called nonCatalogSubscription() to allow for subscriptions?

Let me know if it is a good idea, or maybe the subscription part should be addressed in a different PR as we would need more testing for compatibility for swap, pause, cancel, etc.. or maybe document that some subscription operations might not be compatible with standard subscription model.

@driesvints
Copy link
Member

driesvints commented Jul 4, 2024

@AcTiv3MineD

  1. No I think this is fine. It's the same as in Cashier Paddle v1 and in Cashier Stripe.
  2. I re-added this with a brand new API. Here's an example:
$user->newSubscription(1000, 'Gym subscription')
    ->yearly()
    ->quantity(3) // Seats
    ->checkout();

Could you try this all out and let me know if it works?

@driesvints driesvints changed the title Create Transaction Bill for non-catalog items Jul 4, 2024
@AcTiv3MineD
Copy link
Contributor Author

@driesvints Did some minor fixes.

1 - Price description is required and must be filled
2 - Transaction was missing the ['data'] key on the button

I have tested locally both subscription and one-off. It works as expected.

@driesvints
Copy link
Member

driesvints commented Jul 5, 2024

Thanks @AcTiv3MineD. I've further refined this and made the API a bit more simpler. Description is now optional again. Here's your updated description:

Product Checkout

// Charges 10.00
$user->charge(1000, 'T-shirt', ['quantity' => 5]);

Product subscription

// Charges 10.00 each year
$user->newSubscription(1000, 'Gym subscription')
    ->yearly()
    ->quantity(3) // Seats
    ->checkout();

The default currency is USD, to change it, you can either:

1 - Set CASHIER_CURRENCY=currency on your .env
2 - Pass currency as an option, ex:

// Charges 10.00
$user->charge(1000, 'T-shirt', ['price' => ['unit_price' => ['currency_code' => 'EUR']]], quantity' => 5]);

@driesvints
Copy link
Member

I tested this and it works fine. Let me know if you have any further thoughts @AcTiv3MineD.

@AcTiv3MineD
Copy link
Contributor Author

@driesvints Just tested on my side, looks good to me 👍

@driesvints driesvints marked this pull request as ready for review July 5, 2024 13:08
@taylorotwell taylorotwell merged commit b87ad6b into laravel:2.x Jul 5, 2024
2 of 7 checks passed
@driesvints
Copy link
Member

Released now. Thanks a bunch for your help @AcTiv3MineD. If you're up for it, we'd also appreciate a PR to the docs!

@AcTiv3MineD
Copy link
Contributor Author

Sure!, I can work on that @driesvints

@AcTiv3MineD
Copy link
Contributor Author

The documentation is ready for review:
laravel/docs#9749

@driesvints

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bill for non-catalog items
3 participants