收取条带化api费用后获取状态

时间:2019-03-19 17:21:15

标签: php api

我正在寻找PHP代码语法来捕获调用成功还是失败以剥离API。我需要捕获对象属性之一来确定这一点,但是我不确定最好的方法是什么。 这是成功ACH api收费的示例响应。我正在寻找调用成功或失败的php代码语法

Stripe\Charge Object
(
[id] => py_1EFlRJBuwnzOEo57Poikh194
[object] => charge
[amount] => 50
[amount_refunded] => 0
[application] => 
[application_fee] => 
[application_fee_amount] => 
[balance_transaction] => txn_1EFlRJBuwnzOEo57dupt8SWp
[captured] => 1
[created] => 1553015365
[currency] => usd
[customer] => cus_Ec6cAdg6N2yYcF
[description] => 
[destination] => 
[dispute] => 
[failure_code] => 
[failure_message] => 
[fraud_details] => Array
    (
    )

[invoice] => 
[livemode] => 
[metadata] => Stripe\StripeObject Object
    (
    )

[on_behalf_of] => 
[order] => 
[outcome] => Stripe\StripeObject Object
    (
        [network_status] => approved_by_network
        [reason] => 
        [risk_level] => not_assessed
        [seller_message] => Payment complete.
        [type] => authorized
    )

[paid] => 
[payment_intent] => 
[receipt_email] => 
[receipt_number] => 
[receipt_url] => https://pay.stripe.com/receipts/acct_1E8sOiBuwnzOEo57/py_1EFlRJBuwnzOEo57Poikh194/rcpt_EjDtGP7iknHI9RCBJ3iUODNC6bVBVyM
[refunded] => 
[refunds] => Stripe\Collection Object
    (
        [object] => list
        [data] => Array
            (
            )

        [has_more] => 
        [total_count] => 0
        [url] => /v1/charges/py_1EFlRJBuwnzOEo57Poikh194/refunds
    )

[review] => 
[shipping] => 
[source] => Stripe\BankAccount Object
    (
        [id] => ba_1E8sTZBuwnzOEo57MKqXEvb7
        [object] => bank_account
        [account_holder_name] => Rassi Stern
        [account_holder_type] => individual
        [bank_name] => STRIPE TEST BANK
        [country] => US
        [currency] => usd
        [customer] => cus_Ec6cAdg6N2yYcF
        [fingerprint] => 9l7up0pswCYSO7eu
        [last4] => 6789
        [metadata] => Stripe\StripeObject Object
            (
            )

        [routing_number] => 110000000
        [status] => verified
    )

[source_transfer] => 
[statement_descriptor] => 
[status] => pending
[transfer_data] => 
[transfer_group] => 
    )

2 个答案:

答案 0 :(得分:2)

您是否正在使用Stripe PHP库?

我实现了这样的东西:

try {
    $charge = \Stripe\Charge::create(array(
        "amount" => $amount, // in pence - min 30p
        "currency" => $currency,
        "description" => $description,
        "statement_descriptor" => $statementDescriptor,
        "source" => $token,
        "metadata" => array (
            'code' => $code
        ),
        "receipt_email" => $email
    ));
    $success = true;
} catch(\Stripe\Error\Card $e) {
    $success = false;
    // Some other stuff to capture the reason for failure and tell the user etc.
}

如果您不使用他们的资料库,则可能值得研究:付款成功或失败的原因多种多样,因此,通过大量的Charge Object寻找答案可能不会100%产生结果你想要的。

答案 1 :(得分:1)

您可以从收费对象中获取“已付费”或“状态”参数的值。

$success = json_encode($charge->paid); // Expected value = true
$success = json_encode($charge->status); // Expected value = succeeded

然后您可以进行检查:

if($success) { // or $success == "succeeded" depending on which array key you go for.
    // Payment succeeded! Do something...
} else {
    print_r(json_encode($charge->failure_message));
    // Do something else...
}

代码也可以在没有json_encode()的情况下工作,我发现使用json_encode可以减少代码的错误。

根据Stripe API文档:https://stripe.com/docs/api/charges/create