0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

オンライン決済Stripeで「有効」の商品のみを取得する方法

Posted at

この記事の目的

Next.jsとLaravelで商品管理システムを作っていて、特定の商品のみを取得する方法について記事を作りたくなったので執筆します。
オンライン決済Stripeで「有効」(赤枠)の商品のみを取得する方法について解説します。

これは、Laravel側でデータを後述の手順で取得することができます。

image.png

Laravel(サーバ側)での処理・実装

public function listProducts()関数が今回の記事でやりたい部分です。
まず、Stripeに保存されている商品を全件数取得します。

sample.php
$products = Product::all();

つぎに、全件数をループしながら有効(active)になっている商品を選別・抽出していきます。

sample.php
            $activeProducts = [];//Add
            foreach($products->data as &$product){
                if($product->active){//Add
                    //dd($product->active);
                    if(isset($product->default_price)){
                        $price = Price::retrieve($product->default_price);
                        $product->price = $price->unit_amount / 100;
                        $product->currency = $price->currency;
                    }
                    $activeProducts[] = $product;
                }
                /*
                if(isset($product->default_price)){
                    $price = Price::retrieve($product->default_price);
                    $product->price = $price->unit_amount / 100;
                    $product->currency = $price->currency;
                }
                */
                
            }
            return response()->json($activeProducts);

全文を記載したコードはこちら↓

backend/app/Http/Controllers/StripController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Stripe\Stripe;//Add
use Stripe\Product;//Add
use Stripe\Price;//Add

class StripeController extends Controller
{
    //Method to create product
    public function createProduct(Request $request){
        Stripe::setApiKey(env('STRIPE_SECRET_KEY'));

        //Create Product
        $product = Product::create([
            'name'=>$request->input('productName'),
            'description'=>$request->input('productDescription'),
        ]);

        //Set price
        $price = Price::create([
            'unit_amount'=>$request->input('productPrice')*100,
            'currency'=>'USD',
            'product'=>$product->id,
        ]);

        return response()->json(['product'=>$product,'price'=>$price]);
    }

    //Methos to list all products
    public function listProducts()
    {
        Stripe::setApiKey(env('STRIPE_SECRET_KEY'));
        try{
            //Get list of all products
            $products = Product::all();
            //dd($products->data);
            $activeProducts = [];//Add
            foreach($products->data as &$product){
                if($product->active){//Add
                    //dd($product->active);
                    if(isset($product->default_price)){
                        $price = Price::retrieve($product->default_price);
                        $product->price = $price->unit_amount / 100;
                        $product->currency = $price->currency;
                    }
                    $activeProducts[] = $product;
                }
                /*
                if(isset($product->default_price)){
                    $price = Price::retrieve($product->default_price);
                    $product->price = $price->unit_amount / 100;
                    $product->currency = $price->currency;
                }
                */
                
            }
            return response()->json($activeProducts);
        }catch(\Exception $e){
            return response()->json(['error' => $e->getMessage()], 400);
        }
    }
    //Method to delete product
    public function deleteProduct($productId)
    {
        Stripe::setApiKey(env('STRIPE_SECRET_KEY'));
        //Delete product
        $product = Product::retrieve($productId);
        $product->delete();

        return response()->json(['status'=>'success']);
    }

    //Method to update product
    public function updateProduct($productId,Request $request)
    {
        Stripe::setApiKey(env('STRIPE_SECRET_KEY'));
        //Update product
        $product = Product::retrieve($productId);
        $product->name = $request->name;
        $product->description = $request->description;
        $product->save();
        return response()->json($product);
    }
}

以上です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?