【问题标题】:How to get associative array from Stripe objects PHP如何从 Stripe 对象 PHP 中获取关联数组
【发布时间】:2020-07-29 09:10:32
【问题描述】:

我正在尝试从使用 Stripe 的 PHP 库从端点检索的对象中检索元数据和付款金额。

我正在使用他们的示例代码的略微修改版本:

$payload = @file_get_contents('php://input');
$event = null;

try {
    $event = \Stripe\Event::constructFrom(
        json_decode($payload, true)
    );
} catch(\UnexpectedValueException $e) {
    // Invalid payload
    http_response_code(400);
    exit();
}

// Handle the event
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
        // Read the result.
        $paymentIntentValues = $paymentIntent->values(); // Returns standard array??
        break;
    case 'payment_method.attached':
        $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
        // Read the result.
        $paymentMethodValues = $paymentMethod->values(); // Returns standard array??
        break;
    // ... handle other event types
    default:
        // Unexpected event type
        http_response_code(400);
        exit();
}

http_response_code(200);

问题是当我转储任何对象时,例如$paymentIntent,转储只是 Charge 对象,当我尝试使用 values(); 获取值时,它给了我一个模棱两可的标准数组。

例如

array (45) [
    0 => string (12) "pi_secretkey"
    1 => string (12) "paymentIntent"
    2 => integer 247
    3 => integer 0
    4 => null
    5 => null
    6 => null
    7 => string (13) "anothersecret"
    8 => Stripe\StripeObject (7) (
        protected '_lastResponse' -> null
        protected '_opts' -> Stripe\Util\RequestOptions (3) (
            public 'apiBase' -> null
            public 'apiKey' -> null
            public 'headers' -> array (0) []
        )

我希望能够读取该 $paymentIntent 对象的值,并通过关联数组或对象一致地使用这些值。

【问题讨论】:

    标签: php stripe-payments


    【解决方案1】:

    为了检索列出 Stripe 对象值的关联数组,例如PaymentIntent 对象,您必须使用 toArray() 函数

    例如

    // Handle the event
    switch ($event->type) {
        case 'payment_intent.succeeded':
            $paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
            // Read the result.
            $paymentIntentValues = $paymentIntent->toArray(); // Returns associative array of values.
            break;
        case 'payment_method.attached':
            $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
            // Read the result.
            $paymentMethodValues = $paymentMethod->toArray(); // Returns associative array of values.
            break;
        // ... handle other event types
        default:
            // Unexpected event type
            http_response_code(400);
            exit();
    }
    

    结果:

    array (45) [
        'id' => string (12) "pi_secretkey"
        'object' => string (12) "paymentIntent"
        'amount' => integer 247
        'amount_refunded' => integer 0
        'application' => null
        'application_fee' => null
        'application_fee_amount' => null
        'balance_transaction' => string (13) "anothersecret"
        etc... etc...
    

    【讨论】:

      猜你喜欢
      • 2012-10-19
      • 2013-11-18
      • 1970-01-01
      • 2012-08-30
      • 2020-10-03
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多