/    Sign up×
Bounties /Pin to ProfileBookmark

add a payment method with credit card in laravel

i just want to know how can i connect with the payment revolut api and where should i stock the response in the db

to post a answer
LaravelPHP

2 Replies

Davinci•003 auto-replyAdding a payment method with a credit card in Laravel can be efficiently done using Laravel Cashier, which provides an expressive, fluent interface to Stripe's and Paddle's subscription billing services. For this example, we'll focus on integrating Stripe, as it's one of the most popular payment processing services.

### Step 1: Install Laravel Cashier

First, you need to install Laravel Cashier. Run this command in your terminal:

``bash
composer require laravel/cashier
`

### Step 2: Configuration

After installing Cashier, publish the Cashier migration:

`bash
php artisan vendor:publish --tag="cashier-migrations"
`

Then, run your database migrations:

`bash
php artisan migrate
`

Next, configure your Stripe keys in your
.env file. You can find these keys in your Stripe dashboard.

`plaintext
STRIPE_KEY=your_stripe_key
STRIPE_SECRET=your_stripe_secret
`

### Step 3: Set Up Your Model

Add the
Billable trait to your model, typically the User model.

`php
use LaravelCashierBillable;

class User extends Authenticatable
{
use Billable;
}
`

### Step 4: Create a Payment Form

You'll need a form to collect credit card details. Stripe provides a secure way to handle this with Stripe Elements. Include the Stripe.js script and set up a form in your view:

`html




<!-- A Stripe Element will be inserted here. -->


<!-- Used to display form errors. -->


Submit Payment

`

### Step 5: Set Up Stripe Elements in Your JavaScript

You'll need to write some JavaScript to create an instance of
Elements and mount it to your form. This is a basic setup:

`javascript
var stripe = Stripe('your_stripe_public_key');
var elements = stripe.elements();
var style = {
base: {
// Add your base input styles here. For example:
fontSize: '16px',
color: '#32325d',
},
};

var card = elements.create('card', {style: style});
card.mount('#card-element');

card.addEventListener('change', ({error}) =&gt; {
const displayError = document.getElementById('card-errors');
if (error) {
displayError.textContent = error.message;
} else {
displayError.textContent = '';
}
});
`

### Step 6: Process the Payment

To process the payment, you'll need to handle the form submission and use Cashier's methods to create a charge. This involves setting up a route and controller method to handle the form submission.

In your controller, you can use the
createOrGetStripeCustomer and charge methods to charge the card:

`php
use IlluminateHttpRequest;

public function charge(Request $request)
{
$user = $request-&gt;user();
$paymentMethod = $request-&gt;input('payment_method');

$user-&gt;createOrGetStripeCustomer();
$user-&gt;addPaymentMethod($paymentMethod);

$charge = $user-&gt;charge(
1000, // Amount in cents
$paymentMethod
);

return response()-&gt;json($charge);
}
``

### Step 7: Secure Your Payment Flow

Remember to use CSRF protection on your payment form and validate all inputs. Also, ensure your site is served over HTTPS to protect sensitive information.

This is a basic overview of adding a credit card payment method in Laravel using Cashier and Stripe. Depending on your specific requirements, you might need to adjust this setup, especially when dealing with subscriptions or more complex billing scenarios. Always refer to the official Laravel Cashier and Stripe documentation for the most current information and best practices.

Was this helpful?

Copy linkTweet thisAlerts:
@Yussuf4331Apr 08.2024 — Can you please guide me on the steps
@undefinedauthorIn the client side should pay an amount he chooses so after payment i should generate an invoice for it, thats why i want to know how the revolut api response is so i can make sure that the payment is proceed before the invoice is generatedApr 09.2024
×

Success!

Help @undefined spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...