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

One-Time charge: order items orderable_id / model #122

Open
divdax opened this issue Aug 22, 2022 · 10 comments
Open

One-Time charge: order items orderable_id / model #122

divdax opened this issue Aug 22, 2022 · 10 comments
Labels
major release The issue describes something that will be implemented in a future major release

Comments

@divdax
Copy link

divdax commented Aug 22, 2022

It would be great we could set the orderable morph relationship for one time charges like so:

$product = App\Models\SomethingOrderable::find(1);
$result = $user->newCharge();

$item = new ChargeItemBuilder($user);
$item->quantity($data->qty);
$item->unitPrice(money($data->price*100,'EUR'));
$item->description($data->name);
$item->taxPercentage(19);
$tiem->orderableModel($product); // <---------- new method

$result->addItem($item->make());

$result = $result->create();

Currently orderable_id & oderable_type in table order_items is always empty.

@sandervanhooft
Copy link
Collaborator

How about this?

$item = new \Laravel\Cashier\Charge\ChargeItemBuilder($user, $orderable);

@divdax
Copy link
Author

divdax commented Aug 23, 2022

@sandervanhooft yeah... much better! :)

@sandervanhooft
Copy link
Collaborator

Would be even cooler to do something like this and fill everything automatically:

class ChargeItemBuilder
{
    // ...

    public static function forChargeable(Chargeable $chargeable, Model $billable)
    {
        
        $result = new static($billable);
        $result->orderable = $chargeable->orderable();
        $result->unitPrice($chargeable->unitPrice());
        $result->description($chargeable->description());
        $result->taxPercentage($billable->taxPercentage());

        return $result;
    }
}
interface Chargeable
{
    public function orderable(): Model;
    public function unitPrice(): Money;
    public function description(): string;
}

This would require some additional modifications around handlePaymentFailed/Paid on the orderable model (also see InteractsWithOrderItems. 🤔

Need to investigate if this is possible without breaking change.

@sandervanhooft
Copy link
Collaborator

For clarity, this would allow you to do stuff like

$itemA = ChargeItemBuilder::forChargeable($chargeableA, $billable)->make();
$itemB = ChargeItemBuilder::forChargeable($chargeableB, $billable)
    ->taxPercentage(5.5) // overrides allowed
    ->description('Some other description')  // overrides allowed
    ->quantity(3)
    ->make();

$result = $user->newCharge()
    ->addItem($itemA)
    ->addItem($itemB)
    ->setRedirectUrl('https://www.example.com')
    ->create();

@divdax
Copy link
Author

divdax commented Aug 23, 2022

This is awesome! 😍

@sandervanhooft
Copy link
Collaborator

sandervanhooft commented Aug 23, 2022

Thanks @divdax .

This is going to involve several steps, need to be careful to not break any existing installations. First impressions:

  1. Split InteractsWithOrderItems into the following interfaces and apply as such:

    • HandlesPaymentStatusUpdates
    • ProcessesOrderItem
    • PreprocessesOrderItems (already exists)
  2. Add checks to Order.handlePaymentPaid etc to check if the handlePaymentFailed method exists on the item before calling it.

  3. Add Chargeable interface.

  4. Add ChargeItemBuilder::forChargeable()

@divdax
Copy link
Author

divdax commented Sep 6, 2022

Do you think it's possible?

@sandervanhooft
Copy link
Collaborator

We have put the first steps towards this on our backlog 🤞

@divdax
Copy link
Author

divdax commented Jan 5, 2023

First of all happy new year! 🎉 Sorry for asking, but do you have any news here?

@tobischulz
Copy link

Hey, this would help a lot!
Is there currently any way to get all one-time charges for a user?

@Naoray Naoray added the major release The issue describes something that will be implemented in a future major release label Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
major release The issue describes something that will be implemented in a future major release
Projects
None yet
Development

No branches or pull requests

4 participants