如何在laravel中集成API条带?

时间:2015-10-06 06:53:07

标签: php laravel-5 stripe-payments

如何在laravel中集成API条带?我想在laravel中为我的项目做api集成条带。请任何人说一些好主意。

1 个答案:

答案 0 :(得分:0)

这是简单的api,您可以查看它以快速了解Laravel上的Stripe:

public function postStripeTest(Request $request){
        $rules = [
            'token' => 'required',
            'amount' => 'required',
        ];

        $validator = Validator::make($request->all(), $rules);

        if ($validator->passes()) {
            try {
                $charge = Stripe::charges()->create([
                    // 'customer'       => 'cus_4EBumIjyaKooft',
                    'currency'      => 'USD',
                    'amount'        => $request['amount'],
                    "source"        => $request['token'],
                    "description" => "Example charge"
                ]);

                return response()->json([
                            'status' => true,
                            'data'   => $charge['id']
                        ]);
            } catch (Exception $e) {
                return response()->json([
                        'status' => false,
                        'data'   => "charges stripe fail!"
                    ]);
            }

        }
        // get all error messages
        foreach ($validator->messages()->toArray() as $key => $msg) {
            $messages[$key] = reset($msg);
        }

        return response()->json([
            'status' => false,
            'data'   => $messages
        ], 412);

    }