Stripe / PHP:处理客户对象成功但收费失败(4000000000000341)

时间:2017-07-31 13:39:58

标签: php stripe-payments

在Stripe中,测试编号4000 0000 0000 0341按照Stripe Testing page模拟"将此卡附加到Customer对象的情况成功,但尝试向客户收费失败。&# 34;在我的情况下,我想将此情况视为错误并将其发送到错误处理程序,而不是向客户报告收费成功。

我在PHP中这样做,所以我没有从API获取JSON对象,而是从客户那里获得PHP对象。我是Stripe API的新手,所以我不知道我应该在这里做些什么。我试图搜索有关这种情况的信息,但无法找到任何有用的信息。并且看起来这个案例并不是由现有的SO问题处理的。

代码的摘录如下。如果客户没有收费,我需要将$charge_failed设置为true

\Stripe\Stripe::setApiKey(STRIPE_SECRET_KEY);
// Create a customer.
try {
    $customer = \Stripe\Customer::create(array( "source" => $my_token, 'email' => $customers_email, "description" => $my_description ));

    $charge_failed = false;  //TODO Set this boolean according to whether the charge passed or failed.

    if ($charge_failed) {
      //TODO Report to the user that the charge failed and they need to resubmit.
      die();
    }
}
catch(\Stripe\Error\Card $e) {
    // Card was declined. Report the card declined to the user.
    die();
}
catch (\Stripe\Error\RateLimit $e) {
    // Report the error appropriately.
    die();
} catch (\Stripe\Error\InvalidRequest $e) {
    // Report the error appropriately.
    die();
} catch (\Stripe\Error\Authentication $e) {
    // Report the error appropriately.
    die();
} catch (\Stripe\Error\ApiConnection $e) {
    // Report the error appropriately.
    die();
} catch (\Stripe\Error\Base $e) {
    // Report the error appropriately.
    die();
} catch (Exception $e) {
    // Report the error appropriately.
    die();
}

2 个答案:

答案 0 :(得分:0)

在某些日子之前我成功实施了条带。检查下面希望这会有所帮助。

if ($params['testmode'] == "on") {
    \Stripe\Stripe::setApiKey($params['private_test_key']);
    $pubkey = $params['public_test_key'];
} else {
    \Stripe\Stripe::setApiKey($params['private_live_key']);
    $pubkey = $params['public_live_key'];
}

include_once 'Stripe/init.php';
include_once 'Stripe/lib/Stripe.php';

//Set Stripe keys
$params = array(
    "testmode" => "off",
    "private_live_key" => "sk_live_",
    "public_live_key" => "pk_live_",
    "private_test_key" => "sk_test_",
    "public_test_key" => "pk_test_"
);

if ($params['testmode'] == "on") {
    \Stripe\Stripe::setApiKey($params['private_test_key']);
    $pubkey = $params['public_test_key'];
} else {
    \Stripe\Stripe::setApiKey($params['private_live_key']);
    $pubkey = $params['public_live_key'];
}

if (isset($_POST['stripeToken'])) {
    try {

        $customer = \Stripe\Customer::create(array(
                    'email' => $varUserEmail,
                    'source' => $_POST['stripeToken']
                ));

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

        $result = "success";
        //Save information in the database and send an email with status
          //Ends here
    } catch (\Stripe\Error\Card $e) {
        $error = $e->getMessage();
        $result = "declined";
         //Save information in the database and send an email with status
}

答案 1 :(得分:0)

我可能错了,但我相信只有当您已经拥有现有客户对象并且您正在尝试更新其付款方式时才会出现这种情况。在尝试创建客户并且客户创建失败之前,您用于创建客户的令牌将无效。如果这是初始费用,则javascript方法将返回失败。