I am trying to charge a one time fee + first month subscription fee in stripe but I can't figure out how to do it since they change their interface.
After the initial fee + first month subscription fee, it should roll on a monthly basis.
Ideally I am looking to do this with Laravel Cashier.
Any ideas and examples will be welcome.
Here is how to charge a subscription or a one time pay in stripe:
public function index()
{
if (!request()->wantsJson()) {
abort(404);
}
$plan = request()->get('plan');
$stripeToken = request()->get('stripeToken');
$user = User::findOrFail(request()->get('userId'));
if (is_null($user) || is_null($plan) || is_null($stripeToken)) {
return response()->json(403);
}
if ($plan === self::PREMIUM) {
$user->newSubscription('main', self::PREMIUM_ID)->create($stripeToken);
}
if ($plan === self::EXTENDED) {
$transaction = $user->charge(999, ['currency' => 'usd', 'source' => $stripeToken]);
$payment = new Payment([
'payment_id' => $transaction->id,
'payment_status' => $transaction->paid,
'amount' => $transaction->amount,
]);
$user->payments()->save($payment);
}
return response()->json(200);
}
$user->newSubscription('main', 'plan_xxxxxxxxxxxxxx')->create($request->stripeToken);
$customer = Customer::retrieve($parent_account->stripe_id);
$charge = Charge::create(array(
'customer' => $customer->id,
'amount' => $amount,
'currency' => 'gbp',
'description' => 'Joining Fee',
));
Related
I have integrated stripe payment in flutter app but After payment got failed even then order placed in Laravel database so please check, what I have done wrong.
Please check at save method, May be I am wrong and can't validate purchase response.
payment controller
public function makePayment(Request $request)
{
try{
$data = $request->input('cartItems');
$cartItems = json_decode($data, true);
$orderData = $request->input('order');
$selectPaymentOption = json_decode($orderData, true);
$totalAmount = 0.0;
foreach ($cartItems as $cartItem){
$order = new Order();
$order->order_date = Carbon::now()->toDateString();
$order->product_id = $cartItem['productId'];
$order->payment_type = $selectPaymentOption['paymentType'];
$order->user_id = $request->input('userId');
$order->quantity = $cartItem['productQuantity'];
$order->amount = ($cartItem['productPrice'] - $cartItem['productDiscount']);
$totalAmount+= $order->amount * $order->quantity;
$order->save();
}
if($selectPaymentOption['paymentType'] == 'Card'){
\Stripe\Stripe::setApiKey('sk_test_hJUgYYzeXtitxxxx71lK8nE00MELJJS8c');
$token = \Stripe\Token::create([
'card' => [
'number' => $request->input('cardNumber'),
'exp_month' => $request->input('expiryMonth'),
'exp_year' => $request->input('expiryYear'),
'cvc' => $request->input('cvcNumber')
]
]);
$charge = \Stripe\Charge::create([
'amount' => $totalAmount * 100,
'currency' => 'inr',
'source' => $token,
'receipt_email' => $request->input('email'),
]);
}
return response(['result' => true]);
} catch (\Exception $exception){
return response(['result' => $exception]);
}
}
and my Flutter's Post request is here.
I want to POST _makePayment method after complete payment successful.
void _makePayment(BuildContext context, Payment payment) async {
PaymentService _paymentService = PaymentService();
var paymentData = await _paymentService.makePayment(payment);
var result = json.decode(paymentData.body);
print(paymentData);
CartService _cartService = CartService();
this.widget.cartItems!.forEach((cartItem) {
_cartService.makeTheCartEmpty();
});
if (result['result'] == true) {
_showPaymentSuccessMessage(context);
Timer(Duration(seconds: 4), () {
Navigator.pop(context);
Navigator.push(
context, MaterialPageRoute(builder: (context) => HomeScreen()));
});
}
}
Referring to my comment above, this is the rough solution I suggested in your controller you have to switch the logic
public function makePayment(Request $request)
{
try{
$data = $request->input('cartItems');
$cartItems = json_decode($data, true);
$orderData = $request->input('order');
$selectPaymentOption = json_decode($orderData, true);
##Change your frontend logic to pass total amount as variable
$totalAmount = $request->totalAmount;
if($selectPaymentOption['paymentType'] == 'Card'){
##Never have any sk or pk in your controller, switch this to config('common.sk_test')
\Stripe\Stripe::setApiKey(config('common.sk_test'));
$token = \Stripe\Token::create([
'card' => [
'number' => $request->input('cardNumber'),
'exp_month' => $request->input('expiryMonth'),
'exp_year' => $request->input('expiryYear'),
'cvc' => $request->input('cvcNumber')
]
]);
$charge = \Stripe\Charge::create([
'amount' => $totalAmount * 100,
'currency' => 'inr',
'source' => $token,
'receipt_email' => $request->input('email'),
]);
}
##After the stripe transaction is finished you can foreach your cart and do what you need to your database
foreach ($cartItems as $cartItem){
$order = new Order();
$order->order_date = Carbon::now()->toDateString();
$order->product_id = $cartItem['productId'];
$order->payment_type = $selectPaymentOption['paymentType'];
$order->user_id = $request->input('userId');
$order->quantity = $cartItem['productQuantity'];
$order->amount = ($cartItem['productPrice'] - $cartItem['productDiscount']);
$order->save();
}
return response(['result' => true]);
} catch (\Exception $exception){
return response(['result' => $exception]);
}
}
For the config('common.sk_test') part of my answer, in you config folder you can create a new file where you have you custom app variables, so create a file for instance common.php and 'sk_test' that takes its value from you .env file
I created customer and product in stripe.
I create paymentIntent, invoiceItems and invoice for this customer. How i can connect invoice and payment?
My controller:
//check user in stripe
$stripeCustomer = $this->stripe->customers->search([
'query' => "email:'$user->email'",
]);
if (isset($stripeCustomer['data']) && !count($stripeCustomer['data']) ) {
//create new user
$stripeCustomer = $this->stripe->customers->create([
'email' => $user->email,
'name' => $user->name
]);
$stripeCustomerId = $stripeCustomer->id ?: 0;
} else {
$stripeCustomerId = $stripeCustomer['data'][0]->id;
}
$invoiceItems = $this->stripe->invoiceItems->create([
'customer' => $stripeCustomerId,
'price' => $product ? $product->stripe_price_id : null,
]);
//create draft invoice
$invoice = $this->stripe->invoices->create([
'customer' => $stripeCustomerId,
]);
//create payment
$paymentIntent = $this->stripe->paymentIntents->create([
'customer' => $stripeCustomerId,
'amount' => $invoiceItems->amount,
'currency' => Payment::CURRENCY_EUR,
'payment_method_types' => ['card']
]);
$clientSecret = $paymentIntent->client_secret;
After submitting form (number card, etc...) I am confirmPayment in view:
const { error } = await stripe.confirmPayment({
elements,
confirmParams: {
// Make sure to change this to your payment completion page
return_url: "{{route('payment-success'), [ 'token' => $token ])}}",
},
});
My paymentSuccess method:
public function paymentSuccess($token)
{
$data = json_decode(base64_decode($token));
//$data->paymentId
// maybe here i must pay invoice for my paymentId???
}
Invoices can be paid in two ways
Stripe automatically creates and then attempts to collect payment on
invoices for customers on subscriptions according to your
subscriptions settings. However, if you’d like to attempt payment on
an invoice out of the normal collection schedule or for some other
reason, you can do so as follows.
$this->stripe->invoices->finalizeInvoice(
$invoice->id,
[]
);
$this->stripe->invoices->pay(
$invoice->id,
[]
);
OR
You can manually send an invoice through email to your customer to
pay. You can do so as follow
$this->stripe->invoices->finalizeInvoice(
$invoice->id,
[]
);
$this->stripe->invoices->sendInvoice(
$invoice->id,
[]
);
I've been trying to get the saved customer id for stripe from database but with no luck.
It works everywhere else, I could get it and save it again if I wanted, but whenever I try to use it in payment intent to automatically renew a subscription, it gives me this error: Trying to get property 'stripecustomerid' of non-object.
this is the bit of the stripe code for recurring charge where the error happens:
public function renew($subscription)
{
\Stripe\Stripe::setApiKey('sk_test_XXXXXXXX');
header('Content-Type: application/json');
try {
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$user = \Auth::user();
$payment_methods = \Stripe\PaymentMethod::all([
'customer' => $user->stripecustomerid,
'type' => 'card'
]);
$payment_intent = \Stripe\PaymentIntent::create([
'amount' => $subscription->plan->stripePrice(),
'currency' => 'usd',
'customer' => $user->stripecustomerid,
'payment_method' => $payment_methods->data[0]->id,
'off_session' => true,
'confirm' => true,
]);
echo json_encode([
'paymentIntent' => $payment_intent,
]);
}
catch (\Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
}
and stripecustomerid is the name of the column where I saved the customer id.
I can print it in another function, and it works when I use GET, but it just doesn't work when the subscription tries to renew.
I have some issues using the Laravel Cashier for creating subscriptions.
First, from my backend I am creating a Package, which calls the following two Strip functions:
public function createStripeProduct(array $data)
{
$product = $this->stripe->products->create([
'name' => $data['title']." ".appName(),
]);
return $product->id;
}
public function createStripePrice(array $data)
{
$price = $this->stripe->prices->create([
'unit_amount' => $data['price'] * $this->multiple,
'currency' => $this->currency,
'recurring' => ['interval' => 'month'],
'product' => $data['stripe_prod_id'],
]);
return $price->id;
}
Then in my Controller, I am creating the session:
public function create(Request $request)
{
$key = config('services.stripe.secret');
$stripe = new Stripe\StripeClient($key);
$stripeCustomer = $user->createOrGetStripeCustomer();
$checkout_session = $stripe->checkout->sessions->create([
'customer' => $stripeCustomer['id'],
'success_url' => route('frontend.user.account'),
'cancel_url' => route('frontend.user.account'),
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => $request->stripe_price_id,
'quantity' => 1,
],
],
'mode' => 'subscription',
'allow_promotion_codes' => true,
]);
return $checkout_session['id'];
}
Everything is working so far, but with the implementation, I can subscribe one use multiple times to the same or to a different Package.
How can I prevent this from happening and also how to implement a future upgrade/downgrade of the Package?
To answer your two questions:
1) I can subscribe one use multiple times to the same or to a different Package. How can I prevent this from happening
Your code is fetching a Stripe Customer object in createOrGetStripeCustomer(). You can list all Subscriptions on the Customer with https://stripe.com/docs/api/subscriptions/list#list_subscriptions-customer and then check if you want to create an additional CheckoutSession Subscription on that Customer.
2) how to implement a future upgrade/downgrade of the Package?
You would use the code snippets here: https://stripe.com/docs/billing/subscriptions/upgrade-downgrade#changing where you update the Subscription's SubscriptionItem with a new Price ID.
$sub = \Stripe\Subscription::update('sub_123', [
'cancel_at_period_end' => false,
'proration_behavior' => 'create_prorations',
'items' => [
[
'id' => $subscription->items->data[0]->id,
'price' => 'price_456', // the new Price to update to
],
],
]);
I have to update the expiry date by counting from the month. Please guide me. While saving data expiry date is working fine. But I don't understand how to update it.
My Controller Code For Save Data
public function pay_success(Request $request){
$input = $request->all();
date_default_timezone_set('asia/calcutta');
$input['months'] = $request->months;
$expiry_date = Carbon::now()->addMonths($input['months']);
$input['expiry_date'] = $expiry_date;
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
//Send Email
$email = $input['email'];
$messageData = ['email' =>$input['email'],'name' =>$input['name'],'package' =>$input['package'],'months' =>$input['months'],'amount' =>$input['amount'],'expiry_date' =>$input['expiry_date']];
Mail::send('emails.mail',$messageData,function($message) use($email){
$message->to($email)->subject('Registration with AddSpy');
});
$arr = array('msg' => 'Payment successful.', 'status' => true);
return Response()->json($arr);
}
My Update Code is
public function update(Request $request) {
date_default_timezone_set('asia/calcutta');
$months = $request->months;
$expiry_date = Carbon::now()->addMonths($months);
$request['expiry_date'] = $expiry_date;
$data = ['id'=>$request->id, 'name'=>$request->name, 'phone'=>$request->phone, 'country'=>$request->country, 'state'=>$request->state,
'purpose'=>$request->purpose, 'package'=>$request->package, 'months'=>'$months', 'quantity'=>$request->quantity, 'amount'=>$request->amount, 'expiry_date'=>'$expiry_date'];
DB::table('users')->where('id',$request->id)->update($data);
return response()->json($data);
}
Anyone please suggest me a answer. I do changes in my code but It gives this message "message": "SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '$expiry_date' for column addspy.users.expiry_date at row 1 (SQL: update users set id = 47, name = Ayush, phone = 6393611129, country = India, state = UP, purpose = parent, package = basic, months = $months, quantity = 1, amount = 4000, expiry_date = $expiry_date where id = 47)",
"exception": "Illuminate\Database\QueryException",
Thanks in advance
You're sending strings to the database ('$months' & '$expiry_date'). Simply removing the quotes should fix your problem.
i.e.
$data = [
'id' => $request->id,
'name' => $request->name,
'phone' => $request->phone,
'country' => $request->country,
'state' => $request->state,
'purpose' => $request->purpose,
'package' => $request->package,
'months' => $months,
'quantity' => $request->quantity,
'amount' => $request->amount,
'expiry_date' => $expiry_date,
];