Laravel Cashier - Create New Subscription With Existing Customer Object - php

I'm using Laravel Cashier along with Stripe to manage subscriptions. The user will supply their credit card information when signing up, but they won't be subscribed in a specific plan at this point. So I can successfully use Stripe Checkout to create a Stripe customer object and save the Stripe customer ID in my database. But when it comes time for the user to enroll in a plan, I can't see a way to use the Stripe customer ID to enroll them in the plan they want.
Of course, I could ask for their credit card information again and get a Stripe token to use with Laravel Cashier, but I'd like to avoid this since the app already created a Stripe customer object when they signed up and I'd like to simply use the existing customer object to charge their credit card rather than asking for their card number again.
To try illustrate what I'm trying to do, here is some sample code from the Laravel docs:
$user->newSubscription('main', 'monthly')->create($creditCardToken);
But what I'd like to be able to do is something like this (note the change to the create method:
$user->newSubscription('main', 'monthly')->create($user->stripe_id);
Any advice?

If there is a stripe ID for the user, you don't have to supply the token
$user->newSubscription('main', 'monthly')->create();
Have a look at the SubscriptionBuilder class.

You can try this.
$user->setStripeKey(env("STRIPE_KEY"));
# card details
$card = [
'card_number' => 'xxxxxxxx',
'card_cvc' => 'xxx',
'exp_month' => 'xx',
'exp_year' => 'xxxx',
'name' => 'xxx',
];
# generate token
$token = $this->generateAccessToken($card);
private function generateAccessToken($card)
{
$client = new \GuzzleHttp\Client();
$url = 'https://api.stripe.com/v1/tokens';
$pubKey = env("STRIPE_SECRET");
$postBody = [
'key' => $pubKey,
'payment_user_agent' => 'stripe.js/Fbebcbe6',
'card' => [
'number' => $card['card_number'],
'cvc' => $card['card_cvc'],
'exp_month' => $card['exp_month'],
'exp_year' => $card['exp_year'],
'name' => $card['name']
]
];
$response = $client->post($url, [
'form_params' => $postBody
]);
$response_obj = json_decode($response->getbody()->getContents());
return $response_obj->id;
}
# main or primary
$subscription_obj = $user->newSubscription('subscription_name', 'stripe_plan_id')->create($token);

Related

Laravel Stripe MultiPlan Subscription

I want to add Add On to my subscription. Thus i follow this MultiPlan in Laravel doc. For every new subscription, new stripe product are create (means every plan have different product. including the addon). User need to subscribe to any subscription before subscript to the addon. There are no error, but the Subscription DB from stripe for the current subscription will return null quantity and null stripe_plan. Then create error from that database as i cant call the current subscription. Why does the stripe does that? or am I surpose to create new plans under the same product id in Stripe?
My code to create stripe product and plan
$stripe = new StripeClient(env('STRIPE_SECRET'));
$product_stripe = $stripe->products->create([
'name' => $request->service_name,
]);
$plan_stripe = $stripe->plans->create([
// 'id' => $stripe_plan,
'nickname' => $request->service_name,
'product' => $product_stripe['id'],
'amount' => $request->price*100,
'currency' => 'usd',
'interval' => $request->period,
'interval_count' => $request->period_num,
]);
This is my code to subscribe to addon
$user = auth()->user();
$user->subscription('default')->addPlanAndInvoice('plan_stripe_id', 'quantity');
Note that default is the user current subscription.

Stripe - Charge user and send invoice via e-mail

I have a mobile app (Flutter) which uses Lumen as backend. For payments I use Stripe SDK for PHP.
I need to send an invoice to user after successful charge. I understood it works this way:
Create customer in Stripe
Charge customer using customer_id
Create invoice item using customer_id
Stripe will automatically send an invoice via email using customer's email
I have two functions:
public function addCard(Request $request) {
$user = User::find($user->id);
if (is_null($user->stripe_id) || empty($user->stripe_id)) {
$customer = \Stripe\Customer::create([
"name" => $user->name,
"email" => $user->email,
"description" => "Test",
]);
$user->stripe_id = $customer->id;
$user->save();
}
}
public function charge(Request $request) {
$charge = \Stripe\Charge::create([
'amount' => 2000,
'currency' => 'eur',
'customer' => $user->stripe_id,
'description' => 'Some description',
]);
\Stripe\InvoiceItem::create([
'customer' => $user->stripe_id,
'amount' => 2500,
'currency' => 'usd',
'description' => 'One-time setup fee',
'auto_advance' => true,
]);
}
It adds user but without credit card details. Which arguments should I pass so that Stripe can create card token for specified user? Since it's a mobile app I cannot use Stripe.js for this purpose.
At the moment when the charge function is called I get the error message:
Cannot charge a customer that has no active card
This is my first project with payment gateway... So any help is really appreciated.
Just to clarify, an Invoice is something you would send to your customer and ask your customer to pay for the invoice 0 .
If you've already charged your customer, you will NOT send invoice to your customer because this will end up double charging your customer. Instead, you should send a receipt 1 to your customer on the charge you've created.
Saying all that,
To accept a payment from a customer, through Stripe, you could do the following
1. Create one-time charge
Step 1: create a token representing the credit card information. This normally involves your customer goes to your app/website and see a credit card form
You could use Stripe Element which is a preBuild secure UI showing a credit card form
...
...
var token = stripe.createToken(cardElement);
...
Step 2: After you get the token, pass it your PHP server side and call Stripe Charge API
// see https://stripe.com/docs/api/charges/create?lang=php#create_charge-source
$charge = \Stripe\Charge::create([
'amount' => 2000,
'currency' => 'eur',
'source' => $token->id,
'description' => 'Some description',
]);
Follow step by step guide to try the above
2. Save a credit card to a customer and charge the customer later
This is very similar to case 1, except now you are saving a card to the customer and charge the customer. token by default is single use only, if you want to reuse the card e.g. you are charging the customer later for some subscriptions, you will need to save the card to the customer aka Attach Card to customer before you can re-use the token/card.
Step 1: create a token same as above
...
var token = stripe.createToken(cardElement);
...
Step 2: Pass the token your PHP server side and save the card to the customer
// Create a new customer with the token
// https://stripe.com/docs/api/customers/create#create_customer-source
$customer = \Stripe\Customer::create([
"name" => $user->name,
"email" => $user->email,
"description" => "Test",
"source" => $token->id,
]);
// Or you could attach it to an existing customer
\Stripe\Customer::createSource(
'cus_xxxx',
[
'source' => $token->id,
]
);
Step 3: Charge the customer with the saved card.
// When charging this customer, it will use the default card saved at step 2
$charge = \Stripe\Charge::create([
'amount' => 2000,
'currency' => 'eur',
'customer' => $customer->id,
'description' => 'Some description',
]);
Follow step by step guide to try the above
The above shows the simplest way to charge to a customer credit card using Stripe API + Stripe Element
This is just the starting point. After you are familiar with the above concept, you could explore more on
1. PaymentMethod and PaymentIntent
These are the new APIs Stripe recommend but built on top of charges API
https://stripe.com/docs/payments/accept-a-payment
https://stripe.com/docs/payments/save-and-reuse
Invoicing
https://stripe.com/docs/billing/invoices/workflow
https://stripe.com/docs/billing/invoices/create-invoice
This is powerful billing tool which allows you to bill your customer either on their saved card or sending an invoice to your customer to pay.
Check this article (link). You can send the receipt of the transaction (not the invoice)
In your case I think you need pass the email from the server with the following
stripe.paymentIntents.create(
{
amount: 1000,
currency: USD,
payment_method: clonedPaymentMethod.id,
confirmation_method: 'automatic',
confirm: true,
receipt_email: 'abc#cde.com',
}, {
stripeAccount: stripeVendorAccount
},
function(err, paymentIntent) {
// asynchronously called
const paymentIntentReference = paymentIntent;

How to authorize with Stripe 3d Secure while Subscription type is Trailing

I want the 3d-secure modal authorization check with subscription type = trialing.
I am following this link to setup stripe subscription. When I create subscription without 'trial_period_days' the 3d-secure authorization modal pops-up as subscription status becomes 'incomplete'.
But when i pass >trial_period_days and 'payment_behavior' => 'allow_incomplete', modal don't work as subscription status becomes 'active'.
How can i show authorization modal when subscription is trialing?
I have seen this link https://stripe.com/docs/payments/3d-secure#manual-three-ds too, but no progress.
Suggest me a way to implement this.
Here is my code:
public function createCustomer($token) {
\Stripe\Stripe::setApiKey(secretKey);
$customer = \Stripe\Customer::create([
'email' => 'any_email#domain.com',
'source' => $token,
]);
return $this->createSubscription($customer, $token);
}
public function createSubscription($customer, $token) {
$plan_id = $this->getPlanId();
$payment_intent = $this->createSetupIntent($customer->id, $token);
$subscription = \Stripe\Subscription::create([
'customer' => $customer->id,
'items' => [
[
'plan' => $plan->id,
],
],
'trial_period_days' => 14,
'expand' => ['latest_invoice.payment_intent'],
'payment_behavior' => 'allow_incomplete',
]);
return [
'subscription' => $subscription,
'payment_intent' => $payment_intent
];
}
public function createSetupIntent($customer_id, $token) {
$client = new Client();
$url = "https://api.stripe.com/v1/setup_intents";
$response = $client->request('POST', $url, [
'auth' => ['sk_test_key', ''],
'form_params' => [
'customer' => $customer_id,
'payment_method_types' => ["card"],
'payment_method_options' => [
"card" => [
"request_three_d_secure" => "any"
]
]
],
'timeout' => 10.0
]);
$setup_intent = $response->getBody()->getContents();
return json_decode($setup_intent, true);
}
I expect 3d-secure authorization check modal too when i set subscription to trialing.
What you are describing is a scenario in Stripe doc
Basically when you create a subscription with trial period, since there is no immediate payment occurs, there will be no 3DS authentication needed.
The authentication is delayed until the trial period end.
To ask the user for authentication so that when the trial end there will be no need for 3DS authentication, when a subscription is created with trial period, The subscription will have a pending_setup_intent attribute
You could use that pending_setup_intent to ask the user to complete the authentication. You don't have to create a setup Intent explicitly. What you can do is to check the status within the subscription.
If subscription is in trialing, check if there is a pending_setup_intent, if so, pass to the pending_setup_intent.client_secret to the frontend where your customer is subscribing to your product, and call Stripe.js handleCardSetup
stripe.handleCardSetup(psi.client_secret)
.then(siResult => {
log({siResult});
}).catch(err => {
log({siErr: err});
});
When the card is setup and the trial ends, there will be less likely that the charge will need to go through 3DS authentication again.
You could use Stripe Test Card, 4000002500003155 is good for this testing.
You could simulate the trial end by updating the subscription with
trial_end: "now"
off_session: true // This is needed because by default, subscription update is considered on_session
Hope the above helps

How to populate `identifier` and `providers` in Firebase custom authentication?

I'm authenticating my users on my web service and then creating Firebase custom token via php-jwt:
// Requires: composer require firebase/php-jwt
use Firebase\JWT\JWT;
// Get your service account's email address and private key from the JSON key file
$service_account_email = ...;
$private_key = ...;
function create_custom_token($uid, $is_premium_account) {
global $service_account_email, $private_key;
$now_seconds = time();
$payload = array(
"iss" => $service_account_email,
"sub" => $service_account_email,
"aud" => "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
"iat" => $now_seconds,
"exp" => $now_seconds+(60*60), // Maximum expiration time is one hour
"uid" => $uid,
"claims" => array(
"premium_account" => $is_premium_account
)
);
return JWT::encode($payload, $private_key, "RS256");
}
But the users that I authenticate this way, don't show the administrator-friendly "Identifier" and "Providers" fields in the "Authentication" panel in the Firebase Console:
The first two are users that I authenticated via this custom authentication process, and the last one is a user that I authenticated directly via Google.
How can I populate the "Identifier" and the "Providers" fields for users created via custom authentication?
The "Providers" column will only display an icon if the information attached to a user matches one or more of the the given providers in the "Sign-In Methods" section (https://console.firebase.google.com/project/_/authentication/providers).
Custom providers don't have a distinct icon, and Firebase wouldn't know what to display in the "Identifier" column (the UID is already in its own column at the end).
However, you do have some control for the display of the columns by creating them in advance (meaning: before signing them in for the first time), or by updating the user information after the user entry has been created.
I prepared an example showing which combination of fields leads to which display:
Please note:
The display name has no effect: if it is the only data provided, the user is considered anonymous.
Email + Password match the "Email/Password" Provider
Phone Numbers will alway match the "Phone" provider
The icons for a matched provider will be displayed in the column, even if a provider has been disabled.
Emails and Phone numbers have to be unique. If your application allows multiple users with the same email address/phone number, you will get into trouble, if you just want to see more information about the users of your Firebase project.
You can create and update users via the Firebase Auth REST API, but I would suggest to use one of the official Firebase Admin SDKs SDK to do it - in case you want to stick to PHP, I happen to know an unofficial one: kreait/firebase-php (Documentation) (Disclaimer: I'm the maintainer of the PHP SDK :) ).
On a non-technical note: I wouldn't bother too much with the user list in the Firebase Web Console: use the Firebase CLI tool or one of the official (or unofficial ;) ) Admin SDKs to create an overview that meets your needs.
You mentioned in the Bounty Annotation that you asked this in the Firebase Slack Community without an answer - you can find me and other PHP developers in the #php channel. I enabled notifications for the channel, so please feel free to join if you have further questions.
FYI, this is the code I wrote with the PHP SDK to create the data for the screenshot above:
<?php
declare(strict_types=1);
use Kreait\Firebase;
use Kreait\Firebase\Util\JSON;
require_once __DIR__.'/vendor/autoload.php';
$serviceAccount = Firebase\ServiceAccount::fromJsonFile(__DIR__.'/service_account.json');
$firebase = (new Firebase\Factory())
->withServiceAccount($serviceAccount)
->create();
$auth = $firebase->getAuth();
// Remove all users
foreach ($auth->listUsers() as $user) {
$auth->deleteUser($user->uid);
}
// Simulate custom auth
$ct = $auth->createCustomToken('a-custom-auth');
$r = $auth->getApiClient()->exchangeCustomTokenForIdAndRefreshToken($ct);
echo JSON::prettyPrint($auth->getUser('a-custom-auth'));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'displayname-only',
'displayName' => 'Jérôme Gamez',
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email-only',
'email' => 'jerome#example.org',
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email-and-password',
'email' => 'jerome#example.com',
'password' => 'password'
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'phone-only',
'phoneNumber' => '+49-123-1234567',
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email+name+phone',
'email' => 'jerome#example.net',
'displayName' => 'Jérôme Gamez',
'phoneNumber' => '+49-123-7654321',
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email+name+password+phone',
'email' => 'jerome#example.de',
'displayName' => 'Jérôme Gamez',
'password' => 'example123',
'phoneNumber' => '+49-321-7654321',
]));

WePay API response error "payment method does not exist or does not belong to app"

i was having a problem with Wepay API. My codes are correct but it keeps on returning an error saying "payment method does not exist or does not belong to app". I already configured the permission to allow tokenized credit cards. But still. Any feedback is greatly appreciated. Thanks!
Here is my code
require_once('public/payment/wepay/wepay.php');
$user = API::get_client(['fldClientEmail' => $email])->first();
// change to useProduction for live environments
\Wepay::useStaging(WEPAY_CLIENT_ID, WEPAY_CLIENT_SECRET);
$wepay = new \WePay($user->fldClientWepayTokenAccess);
// $wepay = new \WePay(WEPAY_ACCESS_TOKEN);
// dd($email);die;
// dd($user->fldClientWepayAccountID);die;
// charge the credit card
$response = $wepay->request('checkout/create', [
'account_id' => $user->fldClientWepayAccountID,
'amount' => number_format(Input::get('amount_tipped'),2),
'currency' => 'USD',
'short_description' => 'A short description',
'type' => 'goods',
'payment_method' => array(
'type' => 'credit_card',
'credit_card' => array(
'id' => Input::get('cc_id')
)
)
]);
// display the response
return $response;
Make sure that when you follow the tutorial from their docs, you replace all of the credentials from the examples. I was using their Javascript library for the tokenization of the credit card with the client_id they provided.
response = WePay.credit_card.create({
"client_id": YOUR.CLIENT.ID.HERE,
"user_name": valueById('name'),
"email": valueById('email'),
"cc_number": valueById('cc-number'),
"cvv": valueById('cc-cvv'),
"expiration_month": valueById('cc-month'),
"expiration_year": valueById('cc-year'),
"address": {
"postal_code": valueById('postal_code')
}
If you don't provide your own, is like you were creating those credit cards for another application that's not yours.
If this didn't do the trick, check this article, hopefully it does:
https://support.wepay.com/hc/en-us/articles/203609273-WePay-API-Error-Codes

Categories