Laravel Cashier list all user invoices - laravel

How to display all user invoices for referencing in the admin section of the application.
I can get a user invoices by
$userinvoices = $user->invoices();
Or I can get all invoice by stripe API:
$invoices = \Stripe\Invoice::all(array("limit" => 30));
in the second case, I can't get the details of the user the invoice belongs to.
Or is there any way to save invoice data to database on every creation of the invoice in stripe.

Your first option is the better way to go since you have all the information on the invoice and also the user info on the object.
If you want, you can go to your stripe dashboard -> Your account -> account settings -> webhooks -> add endpoint -> select events and select the invoiceitem.created event. setup your endpoint in the application and do whatever you need with it.
Example:
public function invoiceCreated(Request $request){
$payload = $request->all();
if($payload['type'] == 'invoiceitem.created'){
// do whatever you want with the $payload["data"]...
}
}
Good Luck :)

Related

MorphTo with conditions

I am working with field MorphTo and I try to make conditions for the resources.
For example I have 4 resources:
Accounts
PaymentMethodCreditCard
PaymentMethodBankAccount
Transactions
Every Account can add as many Payment Methods as he wants.
And in the transaction I work with MorphTo to select the Payment Method that the account selected.
My problem starts when I try to create a transaction from Nova and get a list of all the Payment Methods in the db without any relation to the account.
My ideal idea was like that:
MorphTo::make('Transactions')
->withoutTrashed()
->dependsOn('Account', function (MorphTo $field, NovaRequest $request, FormData $formData) {
if(isset($formData->Account)){
$field->types([
PaymentMethodCreditCard::class => fn($q)=>$q->where('account_id', $formData->Account),
PaymentMethodBankAccount::class => fn($q)=>$q->where('account_id', $formData->Account),
]);
}
}),
But of course it will not work, Someone has any idea how I can add conditions to the resource?
I'll give you two answers. Since the question does not exactly clarify whether the Account value is changable during the edit process or it's predefined.
Account value does not change.
If the account value does not change after the resource is loaded, then the solution you are looking for is Relatable Filtering
public static function relatablePaymentMethods(NovaRequest $request, $query)
{
$resource = $request->findResourceOrFail();
return $query->where('account_id', $resource->Account);
}
Account value can change
If the account value can change, then you'll need to create your own "Morph To" behavior using Select and Hidden fields.
With Select field, you can utilize the dependsOn and options functions, to change the query results when the Account changes.

Using a single payment method ID between multiple customers in Stripe - Laravel Cashier

I have an application with two subscriber levels (company and reader). Person A has already subscribed as a company. He wants to Add person B as a reader subscriber.
Is there a way I can use the same payment method id Person A used in Stripe for the company to subscribe the Person B as a reader? When I try this, I get an error.
The payment method you provided has already been attached to a
customer.
Sample code
$user = User::where('id', 26)->first();
$user->subscription('company')->incrementQuantity(1);
$paymethod = $user->defaultPaymentMethod();
$id = 28;
$user2 = User::where('id', $id)->first();
$user2->newSubscription('reader', 'price_xxx')->create($paymethod->id);
A payment method can only be attached to a single customer. Either the subscription need to belong to the same customer also (and can then use the same payment method), or you'll need to collect the payment method again for a separate customer.

Laravel Nova field BelongsToMany drilling down the data

I am developing a Web Application using Laravel. I am using Laravel Nova for building the admin panel. But I am having an issue with BelongsToMany field.
I have the database schema as follow
Area
Station - has area_id because an area has many stations
manager - area_id because each user belongs to an area
Area_station - station_id and manager_id (many to many)
So in the Station nova resource, I added a field like this.
BelongsToMany::make('Managers', 'managers', Manager::class),
Therefore, when I go to the Station details page from the Nova admin panel and select "Attach manager", in the dropdown of the next page (page to attach a manager to the department), I can see all the available managers in the database.
But instead of displaying all the available managers in the drop down, I like to filter the managers/users which belongs to the same area as the selected Station. I mean when I attached a manager to the Station, I have to select a station. Is it possible to filter or to achieve what I want in Nova?
Override relatableQuery function which will determines which instances of the model may be attached to other resources, under Manager nova resource.
Approach 1
public static function relatableQuery(NovaRequest $request, $query)
{
// In case manager is get attached to another resource except Station.
if ($request->resource() == 'App\Nova\Station') {
$station = $request->findResourceOrFail();
return $query->where('area_id', $station->area_id);
}
return parent::relatableQuery($request, $query);
}
Update
Approach 2 - Learned something new just now.
You can add a relatable query for the relationship under Station Nova resource. In this case no need to check for the resource.
public static function relatableManagers(NovaRequest $request, $query)
{
$station = $request->findResourceOrFail();
return $query->where('area_id', $station->area_id);
}
Approach 2 is better in my opinion.

laravel cashier with stripe no monthly plan error

I am having trouble using laravel cashier with stripe new product and plan integration. So i created a product and within that product i created a plan. but when i submit the request to i get an error checked my database the user table contains the card information the subscription table is empty. How do integrate laravel cashier with stripe new system product & plan?
error:
No such plan: Monthly
here my code
public function store(Request $request)
{
$token = $request->stripeToken;
auth()->user()->newSubscription('main', 'Monthly')->create($token);
return 'Done';
}
You need to create a product on stripe(when creating it you can set the billing cycle). Once you have created the product on stripe it will then have a product ID. Instead of using the nickname you have assigned it on stripe you need to use the ID like so
$nickname = 'Monthly';
$plan_id = 'plan_xxxxxxxxxxx';
auth()->user()->newSubscription($nickname, $plan_id)->create($request->stripeToken);

Magento Payment Info Block

I have created new payment method (gateway). In this gateway I sending information to bank for credit payment and I use some additional payment options like Name/Person Age/Person Profit/Credit Term/...
By this fields I calculate Credit Term and send all of this data to bank.
I would like to show this information in Payment Method info block (right sidebar in default theme), but I would not like to save this fields to database (so in admin area later I will have information like it was standart Check/Money Order payment and just payment method name would be another)
I can't show this fields in Payment Method info block, because it shows only fields stored in database and only way that I found - store this data in core/session and then in info block retrieve this data back
I doing something like this in Payment Model:
class OS_LacPayCS_Model_Payment extends Mage_Payment_Model_Method_Abstract
{
...
public function assignData($data)
{
parent::assignData($data);
$session = Mage::getSingleton('core/session');
$session->setData('payment_additional', $data);
return $this;
}
...
}
and then getting it
class OS_LacPayCS_Block_Payment_Info extends Mage_Payment_Block_Info
{
...
public function getPaymentInfo()
{
$session = Mage::getSingleton('core/session');
return $session->getData('payment_additional');
}
...
}
Is there another way to get this data?
And also I wish to add some additional rows in Order Review Tab on checkout, how can I add them w/o rewriting review template and block?
Thanx
Magento payment api defines additional_information field that is saved as serialized array to db and that you can use for storing the data you might need to display. You can set data to there by assignData($data) method

Resources