Using Payment-Element in Stripe and Laravel - laravel

We have everything working in Laravel using Stripes card-element. We want to start using the new payment-element. We are currently using the setup intent.
Following the guide: https://stripe.com/docs/payments/payment-element/migration
Stripe says to add payment methods like so:
$intent = \Stripe\SetupIntent::create([
'customer' => $customer->id,
'payment_method_types' => ['card', 'bancontact', 'ideal'],
]);
In Laravel we currently have it setup like so:
return view('payment.payment')->with([
'services' => $services,
'intent' => $user->createSetupIntent()
]);
How do we pass payment_method_types to the Laravel cashier's createSetupIntent() method?

Related

How to change programmatically Laravel Omnipay object?

Currently, I am handling stripe payments with Omnipay's Stripe library. Similar with the example below:
$gateway = Omnipay::create('Stripe');
$gateway->setApiKey("sk_test_xHGgkZtGOlTwxi4d8GYOHifZ");
$formData = array('number' => '4242424242424242', 'expiryMonth' => '6', 'expiryYear' => '2030', 'cvv' => '123');
$response = $gateway->purchase(array('amount' => '10.00', 'currency' => 'USD', 'card' => $formData))->send();
However, there are too much different payment gateways. Let's assume, I've both Paypal and Stripe packages of omnipay. How can I handle the omnipay package that selected as a default on db. I don't know what to do. Ofcourse, You could say simply use if and else. But, it's not a good in practice. What sticks my mind and bother me is implementing this idea with a generic way with the S.O.L.I.D. principles in Laravel.

Relations in resources?

So I'm planning to start using resources for my "API" (vue endpoint). So I started to search for some tutorials about the subject, and found a youtuber that describes the process. And I started making my own API resource. The youtuber shows briefly how to use the relations, but the thing is that I receive Property [description] does not exist on this collection instance. when trying to use the relation in the resource.
The current setup is:
$stack = Stack::select(['id', 'name', 'subject_id', 'description', 'image'])->where('id', '=', $requestId)->first();
$questions = $stack->load('question.choiceInRandomOrder');
return $questions;
And with resource it would be something like (notice choiceInRandomOrde, I would need that relation also):
return [
'subject' => $this->subject->name,
'name' => $this->name,
'slug' => $this->slug,
'description' => $this->description,
'image' => $this->image,
'questions' => [
'description' => $this->question->description,
'is_info' => $this->question->is_info,
'source' => $this->question->source,
'image' => $this->question->image,
]
];
}
And for testing, I have setup the following in my routes web.php
use App\Stack;
use App\Http\Resources\StackResource;
Route::get('/json', function(){
$stack = Stack::find(2);
return new StackResource($stack);
});
You try to access the name of subject in 'subject' => $this->subject->name, but you do not load the relation.
i don't know if i'm right but i thnk it has to be with the fact that ure not doing an Eloquent call but a Query Builder call (when doing $stack = Stack::select ... ). Why select just some fields in the call if you can choose the parameters to show directly in the model class? (see this).
Try to doing an Eloquent call instead (something like Stack::find(1)) and test it. It should work.

Laravel - Payone

Is there an easy wrapper for the payone(https://www.payone.de/) API for Laravel? I only found one company who sells a package but nothing that is open source. I would appreciate any help.
You should consider Omnipay: http://omnipay.thephpleague.com/
Because:
It is gateway independent.
It is framework independent. It works well with Laravel but also Symfony, Yii, etc.
There is an Omnipay plugin for PayOne: https://github.com/academe/OmniPay-Payone
The code to make a purchase via Omnipay is pretty much the same regardless of the gateway. Here is some sample code that should work, although you should check the details of the Payone classes for other information that you need to send. The Payone gateway can work in multiple different ways depending on how your account is set up.
$gateway = Omnipay::create('Payone_ShopServer');
$card = new CreditCard(array(
'firstName' => 'Example',
'lastName' => 'User',
'number' => '4111111111111111',
// ... etc
));
$transaction = $gateway->purchase(array(
'amount' => '10.00',
'currency' => 'USD',
'description' => 'This is a test purchase transaction.',
'card' => $card,
));
$response = $transaction->send();
if ($response->isSuccessful()) {
echo "Purchase transaction was successful!\n";
}
// At this point you should get $response->getTransactionReference()
// and store that or something similar.

Metered billing with Laravel Cashier

I'm using Laravel 5's Cashier, and would like to add additional fees to a user's monthly bill.
Stripe has this functionality available through their API: https://stripe.com/docs/subscriptions#metered-billing-and-one-time-charges
However, it does not look like Cashier is setup to handle this. Any ideas on how this could be added with Cashier?
I ended up creating a custom class to add an invoice item.
class AddStripeInvoiceItem
{
public static function execute($user, $amount)
{
Stripe::setApiKey(xxxxxx);
$invoiceItem = ['customer' => $user->stripe_id,
'currency' => 'USD',
'amount' => $amount,
'description' => '+ '.$amount
];
InvoiceItem::create($invoiceItem);
}
}

Using Laravel Facades outside Laravel

I have a Laravel application which I use as an API to a much larger application built in Joomla. I really love using Laravel and decided to use Eloquent within the Joomla application. I got this working by importing the bootstrap\autoload.php file in the Laravel application and creating a Capsule
require JPATH_ROOT.'/../laravel_app/bootstrap/autoload.php';
$capsule = new Capsule();
$config = new JConfig();
$capsule->addConnection([
'driver' => 'mysql',
'host' => $config->host,
'database' => $config->db,
'username' => $config->user,
'password' => $config->password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => $config->dbprefix,
'strict' => false
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
This works great and I can use Eloquent. It loads the Eloquent models from the app directly.
What I want to know is how I get the rest of the Laravel app working inside my Joomla app, including using Facades.
For example I have the use of Config.get('key') within one of the Eloquent models, works fine when called in Laravel, but throws and error when called in Joomla.
Fatal error: Class 'Config' not found in laravel_app/app/Model.php on line 192
I have looked at laravel_site/public/index.php to see how it initiates the application and so far so good this seems to be a working solution:
require JPATH_ROOT.'/../laravel_site/bootstrap/autoload.php';
$app = require_once JPATH_ROOT.'/../laravel_site/bootstrap/app.php';
$kernel = $app->make('Illuminate\Contracts\Http\Kernel');
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
//$response->send();
//$kernel->terminate($request, $response);
Facades now appear to be working fine. I've purposely left out $response->send(); and $kernel->terminate($request, $response); so that the routing does not take place and override Joomla's own routing.
I also no longer need to instantiate the Capsule as Laravel is doing that for me now.
I haven't tested this fully so I do not know how robust it is or what features will work but all is good so far.

Resources