Reputation: 41
I'm new to Stripe and payment integration in general.
I would like to implement a payment system in my application. I have already set the credit card input, which is also verified by creating a token of this type:
{
"card": {
"address_city": null,
"address_country": null,
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "US",
"cvc_check": "unchecked",
"dynamic_last4": null,
"exp_month": 12,
"exp_year": 2025,
"funding": "credit",
"id": "card_1HZFtCHAdtJCId9lZP626zJI",
"last4": "4242",
"name": null,
"object": "card",
"tokenization_method": null
},
"client_ip": "5.171.212.113",
"created": 1601989654,
"id": "tok_1HZFtCHAdtJCId9lxdU1jFVa",
"livemode": false,
"object": "token",
"type": "card",
"used": false
}
so far so good, what I should do is send this token to my php server (an external hosting without any particular library installed). Can anyone explain to me how to process the payment from back-end? I checked the documentation but none of them explain how to do it using a normal php hosting. I thank everyone in advance for taking the time!
Upvotes: 0
Views: 903
Reputation: 7419
While @Juan has answered above using the Charges API, for an integration which supports Strong Customer Authentication I would recommend using the Payment Intents API.
You can read through the end-to-end guide for creating a payment which includes both client and server code snippets in a variety of languages (including PHP). This is the recommend pattern.
Since you already have a card_123
if you want to attempt payment without SCA support, you can actually go right to creating the payment:
\Stripe\PaymentIntent::create([
'amount' => 1234,
'currency' => 'usd',
// 'customer' => 'cus_567', // only if you've attached the card
'payment_method' => 'card_123',
'error_on_requires_action' => true,
'confirm' => true,
]);
Upvotes: 0
Reputation: 176
Considering you have already installed stripe you would follow these steps. If not you should use composer to install it.
composer require stripe/stripe-php
The Stripe API uses API keys to authenticate requests so you must auth first using your API keys to use anything. https://dashboard.stripe.com/login?redirect=/account/apikeys
$stripe = new \Stripe\StripeClient(INSERT_API_KEY_HERE);
You already got your card so you can charge it.
Edit: You can get all the data you need by making a request using the api. By the time you create a user in your app, you should also create a Customer using stripe and save their stripe-id in your database so you can access to their data.
$customer = $stripe->customers->create(['description' => 'My First Test Customer',]);
// Save your customer ID using $customer->id
$stripe->charges->create([
'amount' => 2000,
'currency' => 'usd',
'source' => 'INSERT_CARD_ID',
'description' => 'My First Test Charge',
]);
Source:
A payment source to be charged. This can be the ID of a card (i.e., credit or debit card), a bank account, a source, a token, or a connected account. For certain sources—namely, cards, bank accounts, and attached sources—you must also pass the ID of the associated customer.
Upvotes: 1