2017-06-14 3 views
0

私はStripe PHP APIを使用しています。PHP:Stripe API単純なテキスト文字列として有用なエラーを取得していますか?

すべて正常です。しかし、私はStripe APIによって返されるエラーを簡単な読み込み可能なテキスト文字列として取得/捕捉する必要があります。私は必要なもの

これだけです -

Fatal error: Uncaught exception 'Stripe\Error\Card' with message 'Your card's security code is invalid.' in /var/www/... 

Your card's security code is invalid. 

これは私の全体のコードです:

<?php 

error_reporting(-1); 
ini_set('display_errors', 'On'); 

require_once('stripe/init.php'); 


\Stripe\Stripe::setApiKey("sk_test_345345345345345345345"); 

$result = \Stripe\token::create(array(
    "card" => array(
     "name" => 'David', "number" => '4242424242424242', "exp_month" => '12', "exp_year" => '2019', "cvc" => '37674' 
    ) 
)); 

$token = $result['id']; 

echo $token; 

try { 
    $charge = \Stripe\Charge::create(array(
     "amount" => "7000", // amount in cents, again 
     "currency" => "usd", "card" => $token, "description" => "[email protected]" 
    )); 
    echo 'success'; 
} catch (\Stripe\Error\Card $e) { 
    // Card was declined. 
    $e_json = $e->getJsonBody(); 
    $error = $e_json['error']; 

    echo $error; 
} 

?> 

現在、私のコードは次のようなエラーを出力します

ご注意ください帽子これは何もしません:

catch (\Stripe\Error\Card $e) { 
     // Card was declined. 
     $e_json = $e->getJsonBody(); 
     $error = $e_json['error']; 

     echo $error; 
     } 

誰かがこれに助言していただけますか?

ありがとうございます。

EDIT:

私はすべてを試みたのだが、何もまったく機能しないことが非常にイライラさせられます。

キャッチは何もしませんし、私は私のPHPページを実行すると、それは実際にそれはそれでエラーを持っている必要があるときに常に空/ブランクで:

require_once('stripe/init.php'); 


\Stripe\Stripe::setApiKey("sk_test_345345345345345345345"); 

$result = \Stripe\token::create(array(
    "card" => array(
     "name" => 'David', "number" => '4242424242424242', "exp_month" => '12', "exp_year" => '2019', "cvc" => '37674' 
    ) 
)); 

$token = $result['id']; 

echo $token; 

try { 
    $charge = \Stripe\Charge::create(array(
     "amount" => "7000", // amount in cents, again 
     "currency" => "usd", "card" => $token, "description" => "[email protected]" 
    )); 
    echo 'success'; 
} catch (\Stripe\Error\Card $e) { 
echo $e->getMessage(); 
} 

答えて

0

オリジナル:

はこれを試してみてください更新..

$e_json = $e->getJsonBody(); 
$err = $e_json['error']; 
$error = $err['message']; 



ここに私の完全な、働く、コードです。 (私のサイトとDBに関連するマイナスコード)私は取り残さすべてについて

echo '<script type="text/javascript">Stripe.setPublishableKey("' . STRIPE_PUBLIC_KEY . '");</script>'; 
$errors = array(); 
if (isset($_POST['stripeToken'])) { 
    $token = $_POST['stripeToken']; 
    $email = $_POST['stripeEmail']; 
    if (isset($_SESSION['token']) && ($_SESSION['token'] == $token)) { 
     $errors['token'] = 'You have apparently resubmitted the form. Please do not do that.'; 
    } 
    else { 
     $_SESSION['token'] = $token; 
    } 
} 
else { 
    $errors['token'] = 'The order cannot be processed.'; 
} 

if (empty($errors)) { 
try { 
    require_once('lib/Stripe.php'); 
    Stripe::setApiKey(STRIPE_PRIVATE_KEY); 
    $charge = Stripe_Charge::create(array("amount" => $amount,"currency" => "usd","card" => $token,"description" => "charge for $email")); 
    // get objects like $charge->id and add them to your DB. 
} 
catch (Stripe_CardError $e) { // Card was declined. 
    $e_json = $e->getJsonBody(); 
    $err = $e_json['error']; 
    $errors['stripe'] = $err['message']; 
} 
} 

私のファイルの末尾にconfig.inc.phpを、私のサービス/ DBに関連するコード、およびheader('Location: '.$_SERVER['HTTP_REFERER']);のようなものを必要としています。

HTML:

<form action="charge" method="POST"> 
    <script 
    src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
    data-key="pk_live_bRdpwmwordnr8riwowkr4K0pd" 
    data-amount="495" 
    data-label="Credit Card" 
    data-name=" MY COOL SERVICE" 
    data-description="Subscription ($4.95)" 
    data-billing-address="true" 
    data-image="https://s3.amazonaws.com/stripe-uploads/acct_KKcoolThismerchant-icon-780050592-icon128.png"> 
    </script> 
</form> 
+0

への呼び出しの前にtry {を呼び出す必要があります。 –

+0

@DavidHope、私は私の完全なストライプコードマイナス '私のサイトの関連コード'を与える​​私の答えを更新しました。 –

+0

残念ながら私はストライプフォームを使用していません。すべてがサーバー側で行われています。とにかくありがとう。 –

0

\Stripe\Error\Card\Exceptionのサブクラスであるので、あなたは例外メッセージ

catch (\Stripe\Error\Card $e) { 
    echo $e->getMessage(); 
} 

はまたその例外がこの場所で、あなたのコードにかなり早く発生に注意を取得するgetMessage()メソッドを呼び出すことができます:

$result = \Stripe\token::create(array(
    "card" => array(
     "name" => 'David', "number" => '4242424242424242', "exp_month" => '12', "exp_year" => '2019', "cvc" => '37674' 
    ) 
)); 

この例外をキャッチするには、\Stripe\Stripe::setApiKey("sk_test_345345345345345345345");

+0

は何もしません。 –

関連する問題