2015-10-06 60 views

回答

0

這是簡單的API,你可以用它檢查,以便快速瞭解有關條紋上Laravel:

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); 

    }