2017-11-04 5 views
3

스트라이프를 사용하여 카드의 데이터를 보내면 다음 메시지가 표시됩니다.데이터를받지 않을 때 치명적인 오류를 해결하는 방법 및 페이지를 다시로드 할 때 데이터를 전송하는 줄무늬?

$ 50를 성공적으로 청구했습니다!

tok_1BKYfLDi3J7CIS2eVqZVeZqT

그러나 지불 데이터를 보낼 때 데이터를 수신하지 않을 때 표시됩니다 이러한 오류

Notice: Undefined index: token in C:\xampp\htdocs\DesignFormStripe\charge.php on line 9 Fatal error: Uncaught Stripe\Error\Card: Cannot charge a customer that has no active card in C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php:128 from API request 'req_W6Qbd7iMVHDsGS' Stack trace: #0 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php(102): Stripe\ApiRequestor::_specificAPIError('{\n "error": {\n...', 402, Array, Array, Array) #1 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php(309): Stripe\ApiRequestor->handleErrorResponse('{\n "error": {\n...', 402, Array, Array) #2 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php(65): Stripe\ApiRequestor->_interpretResponse('{\n "error": {\n...', 402, Array) #3 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiResource.php(119): Stripe\ApiRequestor->request('post', '/v1/charges', Array, Array) #4 C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiResource.php(158): Stripe\ApiResource::_staticRequest('post', '/v1/charges', Array, NULL) #5 C:\xampp\htdocs\DesignFormStripe\php\lib\str in C:\xampp\htdocs\DesignFormStripe\php\lib\stripe\lib\ApiRequestor.php on line 128

어떻게 그 실행 오류를 해결할 수 있습니까?


업데이트 내 질문에

handling errors에, 스트라이프의 설명서와 함께 이전 오류를 수정합니다.

코드는 거의 다음과 같이 해결된다 : 이제

Status is:402 Type is:card_error Code is:missing Param is:card Message is:Cannot charge a customer that has no active card

: 수신 데이터 않고 페이지를 열면

<?php 
    require_once('./config.php'); 

    $token = $_POST['token']; // retrieve token POST parameter to charge the card (stripeToken) 


    $customer = \Stripe\Customer::create(array(
     'email' => '[email protected]', 
     //'email' => $_POST['stripeEmail'], 
     'card' => $token 
)); 

try { 
    // Use Stripe's library to make requests... 

    $charge = \Stripe\Charge::create(array(
     'customer' => $customer->id, 
     'amount' => 500, 
     'description' => 'Event charge', 
     'currency' => 'usd' 
)); 

    echo '<h1>Successfully charged $50!</h1>'; 
    echo "$token"; 

} catch(\Stripe\Error\Card $e) { 
    // Since it's a decline, \Stripe\Error\Card will be caught 
    $body = $e->getJsonBody(); 
    $err = $body['error']; 

    print('Status is:' . $e->getHttpStatus() . "\n"); 
    print('Type is:' . $err['type'] . "\n"); 
    print('Code is:' . $err['code'] . "\n"); 
    // param is '' in this case 
    print('Param is:' . $err['param'] . "\n"); 
    print('Message is:' . $err['message'] . "\n"); 
} catch (\Stripe\Error\RateLimit $e) { 
    // Too many requests made to the API too quickly 
} catch (\Stripe\Error\InvalidRequest $e) { 
    // Invalid parameters were supplied to Stripe's API 
} catch (\Stripe\Error\Authentication $e) { 
    // Authentication with Stripe's API failed 
    // (maybe you changed API keys recently) 
} catch (\Stripe\Error\ApiConnection $e) { 
    // Network communication with Stripe failed 
} catch (\Stripe\Error\Base $e) { 
    // Display a very generic error to the user, and maybe send 
    // yourself an email 
} catch (Exception $e) { 
    // Something else happened, completely unrelated to Stripe 
} 
?> 

이제 다음 메시지를 도시 새로운 문제 :(

그러나 페이지를 다시로드하고 양식 재 제출을 확인하면 이전 오류와 동일한 메시지가 표시됩니다.

나는 스트라이프 만 토큰은 한 번 충전의 요청에 따라 생성 허용하기 때문에 오류가 표시되어 있는지 상상한다.

양식 재 제출을 확인할 때 이러한 오류를 표시하는 대신 사용자 정의 오류 메시지를 표시하십시오 (예 : ). 지불 요청이 이미 처리 중입니다.

답변

0

여기서 문제는 카드 토큰 tok_XXXX$_POST['token']에 존재할 것으로 기대한다는 것입니다. 양식이 제출되었지만이를 포착하지 않을 때 매개 변수가 양식에서 전송되지 않는 경우가있는 것 같습니다.

이 때문에 $token은 비어 있으며 단순히 스트라이프로 전송되지 않고 고객이 카드없이 생성됩니다. 다음 단계는 고객에게 비용을 청구하는 것이지만 카드가 없기 때문에 API는 오류를 반환합니다.

이 문제를 방지하려면 코드에서 두 가지를 변경해야합니다. 먼저, here과 같이 Stripe의 API에서 반환 할 수있는 모든 오류를 포착해야합니다. 아이디어는 예외를 잡아 내고 마지막에 자세한 오류를 기록하는 동안 사용자 친화적 인 오류 메시지를 표시하여 해결합니다.

그런 다음 코드 시작 부분에 $_POST['token']이 예상대로 설정되도록 일부 논리를 추가해야합니다. 그렇지 않은 경우 일찍 퇴장하여 고객에게 오류를 반환하십시오.

마지막으로 문제는 일반적으로 토큰이 성공적으로 만들어지지 않고 양식을 제출할 수있는 버그 클라이언트 쪽에서 발생합니다. 일반적인 문제는 토큰을 만들거나 일부 브라우저에서 JS 코드를 제대로 초기화하지 않을 때까지 양식 제출을 사용 중지하지 않는 것입니다.

+0

친구 나는 당신이 말한 것과는 다른 방식으로 해결했지만 해결했지만 불구하고 페이지가 다시로드되고 양식을 다시 제출할 때 오류가 표시됩니다. – OneL

+0

그건 완전히 다른 문제입니다. 페이지를 다시로드하면 동일한 토큰을 다시 제출하고 토큰은 한 번만 사용할 수 있습니다. 이것은 여전히 ​​오류를 적절하게 포착하지 못한다는 것을 의미합니다. – koopajah

+0

오류가 올바르게 잡히고 해결되었지만 문제는 이미 주석 처리 한 것입니다. 예를 들어 사례를 수행하고 있지만 클라이언트가 요청을 보내면 어떻게됩니까? – OneL