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

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.

Related

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);

Laravel - Multiple Arrays for Multiple Recipients (Mailable/Notifications)

I'm looking for advice for a project and an interesting predicament I've put myself in.
I have multiple fields in my shipment model, they are:
shipto_id
shipfrom_id
billto_id
They all link (through different relationships) to my customer model:
public function billtoAccount()
{
return $this->belongsTo('App\Customer', 'bill_to');
}
public function shiptoAccount()
{
return $this->belongsTo('App\Customer', 'ship_to');
}
public function shipfromAccount()
{
return $this->belongsTo('App\Customer', 'ship_from');
}
and these customers (in reality would likely be better described as customer accounts (these are NOT USER ACCOUNTS, they're more just like a profile for each company that business is done with)) can have a multitude of users associated with them.
public function users()
{
return $this->belongsToMany(User::class);
}
Now, while I know how to send off mailables and notifications, I was curious to know how I would go about sending off those to multiple user's emails. So let me describe the following: Something is created and the following customers (and in turn, their users) are referenced.
(billto_id) - Customer Account 1 - User 1 (email1#example.com)
(shipto_id) - Customer Account 2 - User 2 (email2#example.com) & User 3 (email3#example.com)
(shipfrom_id) - Customer Account 37 - User 6 (email4#example.com)
Now, how would I go about moving the emails of the users over to an array of emails to have a notification or mailable sent to them?
So it should pop out: email1#example.com, email2#example.com, email3#example.com, email4#examples.com
Elaborating on #Devon 's comment:
This is business logic. You could have a method on your Shipment model that returns the customer instances to be notified as an array, e.g. getNotifiables() : array
Then in your Customer model you may use the Notifiable trait
use Illuminate\Notifications\Notifiable;
And looping over your Notifiables, i.e. Customers
$notification = new ShipmentWasCreated()
foreach ($shipment->getNotifiables() as $notifiable) {
$notifiable->notify($notification);
}
Better alternative:
Notification::send($shipment->getNotifiables(), new ShipmentWasCreated());

Laravel Cashier list all user invoices

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 :)

Laravel get related data from 3 tables

I have three tables: users, emails,attachments. User table is connected with emails by user_id. Emails table is connected with attachments by email_id.
My question is: How should I make it look eloquent in laravel to get all users their emails and their attachments? (I know how get all user and they emails but I don't know how to add attachments.)
Depending on your database relationship,you may declare a relationship method in your Email model, for example:
// One to One (If Email has only one attachment)
public function attachment()
{
return $this->hasOne(Attachment::class);
}
Otherwise:
// One to Many (If Email contains more than one attachment)
public function attachments()
{
return $this->hasMany(Attachment::class);
}
To retrieve the related attachment(s) from Email model when reading a user using id, you may try something like this:
$user = User::with('email.attachment')->find(1); // For One-to-One
$user = User::with('email.attachments')->find(1); // For One-to-Many
Hope you've already declared the relationship method in the User model for Email model using the method name email.
Note: make sure, you have used right namespace for models. It may work if you've done everything right and followed the Laravel convention, otherwise do some research. Also check the documentation.

How to get the payment transaction of a subscription just created through Recurly's Ruby API?

If a subscription is created with
recurly_subscription = Recurly::Subscription.create!( ... )
the corresponding invoice can be retrieved with
recurly_subscription.invoice
But how to retrieve the corresponding payment transaction?
try this:
s = Recurly::Subscription.first
s.invoice.transactions

Resources