How to use laravel cashier charge with `withMetaData` and `create` - laravel

I am new to laravel cashier, and I am stuck here.
For my games, users can subscribe. I have 3 subscription methods.
Monthly
Yearly
One-time.
For monthly and Yearly subscriptions, I have used the below code and it works perfectly.
$subscription = $user->newSubscription($subscriptionName, $plan->stripe_id)->withMetaData(['game' => $game->name])->create(null, [
'email' => $user->email,
'name' => $user->name,
['metadata' => ['game' => $game->name]],
], $subscriptionOptions);
But for the one-time subscription, I have used the below code
$subscription = $user->charge(2500, 'pm_1IpuT.....')->withMetaData(['game' => $game->name])->create(null, [
'email' => $user->email,
'name' => $user->name,
['metadata' => ['game' => $game->name]],
], $subscriptionOptions);
for this, I am and getting the below error
Error
Call to undefined method Laravel\Cashier\Payment::withMetaData()
Can anyone help to solve this?

As they mention here you pass the metadata as a third argument in the charge method
$user->charge(100, $paymentMethod, [
'custom_option' => $value,
]);
so in your example, you need to edit it like this
$subscription = $user->charge(2500, 'pm_1IpuT.....', [
'metadata' => [
'game' => $game->name,
'email' => $user->email,
'name' => $user->name,
]
]);
for more additional data to pass check stripe docs

Related

Laravel one validation function for Store and Update

I am trying to have one validation function for both store and update. So I don't repeat code. It works well. The problem is testing 'unique'. I worked around with this idea. But feels long-winded. Is there a better way to do it?
I want to check for unique at the store.
At update, unique check ignores own id.
I don't want different validations like I did as the user will be
first notified of the unique error, he will fix it. then something
else might be wrong and he has to fix again.
.
public function validateRequest($request){
if($request->method() == "PUT")
{
$val = $this->validate($request, [
'name' => 'unique:customers,id',
'phone' => 'unique:customers,id',
]);
}
if($request->method() == "POST"){
$val = $this->validate($request, [
'name' => 'unique:customers',
'phone' => 'unique:customers'
]);
}
$validation = $this->validate($request, [
'name' => 'required',
'phone' => 'required|integer|gt:0',
'phone2' => 'nullable|integer|gt:0|',
'email' => 'email|nullable',
'note' => 'nullable',
],
[
'phone.integer' => "Invalid phone format. Use international format. Eg: 971550000000",
'phone2.integer' => "Invalid phone format. Use international format. Eg: 971550000000",
'required' => "Required Field",
]);
return $validation;
}

Laravel sending attachment from two different inputs

I'm trying to send an email through Laravel from a form that has two different file inputs with different names, cv and cover_letter.
The build function works just fine but it only attaches one file when it sends the email, I'd like it to send both.
I've tried this solution here on SO but no luck sending both attachments either.
public function build(Request $request)
{
$today = Carbon::now()->format('Y-m-d');
return $this->from([
'email' => $request->email,
'name' => $request->name
])
->to( 'jobs#domain.com' )
->subject( New job application '.$request->name.' for the position of '.$request->role.'.')
->view('emails.jobsform')
->with([
'name' => $request->name,
'tel' => $request->tel,
'email' => $request->email,
'role' => $request->role,
'location' => $request->location,
'call_code' => $request->call_code,
])
->attach(
$request->cv, [
'as' => $today.".".$request->name.".".$request->role.'.pdf',
'mime' => 'application/pdf'],
$request->cover_letter, [
'as' => $today.".".$request->name.".".$request->role.'-Cover Letter.pdf',
'mime' => 'application/pdf']
);
}
Hope someone can help, thanks.
In example, he use attach with foreach(), it means that for new file he has new attach, and u want to attach all files in one attach(). Try this
->attach(
$request->cv, [
'as' => $today.".".$request->name.".".$request->role.'.pdf',
'mime' => 'application/pdf'])
->attach(
$request->cover_letter, [
'as' => $today.".".$request->name.".".$request->role.'-Cover Letter.pdf',
'mime' => 'application/pdf']
);

Laravel mail blade passing a variable

I need to pass some variables to my sending email.
Here is my code:
$data = [
'first' => $request->first,
'last' => $request->last,
'business_org' => $request->business_org,
'instagram' => $request->instagram,
'email' => $request->email,
'phone' => $request->phone,
'unique' => $request->unique,
'purchased' => $request->products_purchased,
'city' => $request->city,
'state' => $request->state,
'filename' => $fileName
];
// send email with details
Mail::send('emails.justshoot', $data, function($message) {
$message->from('us#something.com', 'Just Shoot Upload');
$message->to('myemail#gmail.com')->cc('myemail#gmail.com');
});
I then attempt to access the variable so I can display it in my email.
emails.justshoot.blade.php
{{$data}} gives an error. What am I doing wrong?
You are totally fine with what you are doing, but the second param is the data to pass to the view, and as with the with([]) method to call the view, the array passed will generate an object for each entry, and so with what you are doing you are generating $first, $last, $business_org and the $data is just the name of the array, so it isn't been passed as element to the view: if you want this, you should pass [$data] to the mail send :
$data = [
'first' => $request->first,
'last' => $request->last,
'business_org' => $request->business_org,
'instagram' => $request->instagram,
'email' => $request->email,
'phone' => $request->phone,
'unique' => $request->unique,
'purchased' => $request->products_purchased,
'city' => $request->city,
'state' => $request->state,
'filename' => $fileName
];
// send email with details
Mail::send('emails.justshoot', [$data], function($message) {
$message->from('us#something.com', 'Just Shoot Upload');
$message->to('myemail#gmail.com')->cc('myemail#gmail.com');
});
and then in the view you can do {{$data}}
TIPS: You should create a mail with php artisan and then you are able to do whatever you want, in a more elegant way

Laravel - Validate value against string

I am running some basic validation inside a Laravel 5.5 controller like this...
$this->validate($request, [
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'required',
]);
Is there a way to check if 'mykey' matches a php string I have saved? I know I can do an if statement and compare them but wondered if there was a way I could do this inside the validation itself?
You can use in rule, This works for n number of values
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => [
Rule::in([env('MY_KEY'),config('app.another_key')]),
]
]);
Laravel provides a regex option for validation. Depending on the complexity of the string comparison it may be useful:
https://laravel.com/docs/5.5/validation#rule-regex
You can this rule:
$key = "my_saved_key"
$request->validate([
'name' => 'required|max:30',
'email' => 'required|unique:users|email',
'password' => 'required|max:20',
'mykey' => 'in:' . $key,
]
]);

laravel wont set header during gopay payment integration

I have a problem with payment integration to my laravel project. It is a GOPAY REST API.
It should by default set request headers with Accept, Content-type and Authorization where the token is stored. Problem is that it doesnt set my request headers. I used the same thing in a normal script which included the SDK and it worked correctly. However in my laravel project it just doesnt work. The SDK uses Curl to set headers and i think there is somewhere the problem.
I also didnt find any similar problem, and i definitely didnt google anyone who integrated GoPay into Laravel.
Pay method in my controller
//minimal configuration
$gopay = GoPay\payments([
'goid' => '8583340073',
'clientId' => '1162442589',
'clientSecret' => 'eDxNQ3ru',
'isProductionMode' => false,
'scope' => GoPay\Definition\TokenScope::ALL,
'language' => GoPay\Definition\Language::CZECH],
['logger' => new GoPay\Http\Log\PrintHttpRequest()]);
$response = $gopay->createPayment([
'payer' => [
'default_payment_instrument' => PaymentInstrument::BANK_ACCOUNT,
'allowed_payment_instruments' => [PaymentInstrument::BANK_ACCOUNT],
'default_swift' => BankSwiftCode::FIO_BANKA,
'allowed_swifts' => [BankSwiftCode::FIO_BANKA, BankSwiftCode::MBANK],
'contact' => [
'first_name' => 'Zbynek',
'last_name' => 'Zak',
'email' => 'test#test.cz',
'phone_number' => '+420777456123',
'city' => 'C.Budejovice',
'street' => 'Plana 67',
'postal_code' => '373 01',
'country_code' => 'CZE',
],
],
'target' => ['type' => 'ACCOUNT', 'goid' => '8583340073'],
'currency' => Currency::CZECH_CROWNS,
'order_number' => '001',
'order_description' => 'pojisteni01',
'items' => [
['name' => 'item01', 'amount' => 50],
['name' => 'item02', 'amount' => 100],
],
'recurrence' => [
'recurrence_cycle' => Recurrence::DAILY,
'recurrence_period' => "7",
'recurrence_date_to' => '2016-12-31'
],
'additional_params' => [
array('name' => 'invoicenumber', 'value' => '2015001003')
],
'callback' => [
'return_url' => 'http://www.hume.cz/public',
'notification_url' => 'http://www.hume.cz/public'
]
]);
I think that somehow laravel changes the headers and doesnt allow the SDK to do it. If you know anything please help me. If you need any extra information, please just ask.
Thank you very much!!
There is a package for handling GoPay payments with Laravel. Install, update config with your credentials and start using GoPay facade to createPayment or another function from official SDK.
I have eshop in production with this my own package and everything works fine.
https://github.com/hazestudio/laravel-gopay-sdk

Resources