Pago API tema matriz multidimensional en php

Kris:

Mi proveedor de pago emitió el siguiente código de ejemplo para su API:

$order = $mollie->orders->create([
       "amount" => [
          "value" => "1027.99",
          "currency" => "EUR"
        ],
        "billingAddress" => [
          "streetAndNumber" => "Keizersgracht 313",
          "postalCode" => "1016 EE",
          "city" => "Amsterdam",
          "country" => "nl",
          "givenName" => "Luke",
          "familyName" => "Skywalker",
          "email" => "[email protected]",
        ],
        "shippingAddress" => [
          "streetAndNumber" => "Keizersgracht 313",
          "postalCode" => "1016 EE",
          "city" => "Amsterdam",
          "country" => "nl",
          "givenName" => "Luke",
          "familyName" => "Skywalker",
          "email" => "[email protected]",
        ],
        "metadata" => [
          "order_id" => $orderId
        ],
        "consumerDateOfBirth" => "1958-01-31",
        "locale" => "en_US",
        "orderNumber" => strval($orderId),
        "redirectUrl" => "{$protocol}://{$hostname}{$path}/orders/return.php?order_id={$orderId}",
        "webhookUrl" => "{$protocol}://{$hostname}{$path}/orders/webhook.php",
        "method" => "ideal",
        "lines" => [
            [
                "sku" => "5702016116977",
                "name" => "LEGO 42083 Bugatti Chiron",
                "productUrl" => "https://shop.lego.com/nl-NL/Bugatti-Chiron-42083",
                "imageUrl" => 'https://sh-s7-live-s.legocdn.com/is/image//LEGO/42083_alt1?$main$',
                "quantity" => 2,
                "vatRate" => "21.00",
                "unitPrice" => [
                    "currency" => "EUR",
                    "value" => "399.00"
                ],
                "totalAmount" => [
                    "currency" => "EUR",
                    "value" => "698.00"
                ],
                "discountAmount" => [
                    "currency" => "EUR",
                    "value" => "100.00"
                ],
                "vatAmount" => [
                    "currency" => "EUR",
                    "value" => "121.14"
                ]
            ],
            [
                "type" => "digital",
                "sku" => "5702015594028",
                "name" => "LEGO 42056 Porsche 911 GT3 RS",
                "productUrl" => "https://shop.lego.com/nl-NL/Porsche-911-GT3-RS-42056",
                "imageUrl" => 'https://sh-s7-live-s.legocdn.com/is/image/LEGO/42056?$PDPDefault$',
                "quantity" => 1,
                "vatRate" => "21.00",
                "unitPrice" => [
                    "currency" => "EUR",
                    "value" => "329.99"
                ],
                "totalAmount" => [
                    "currency" => "EUR",
                    "value" => "329.99"
                ],
                "vatAmount" => [
                    "currency" => "EUR",
                    "value" => "57.27"
                ]
            ]
        ]
    ]);

Lo que me gustaría hacer es sustituir el segundo "líneas" duros codificados dimensión con datos de estas sesiones vars.

foreach ($_SESSION['cart_products'] as $cart_itm) {
    $name = $cart_itm['cat_item_titel_' . $lang . ''];
    $unitPrice = $cart_itm['cat_item_prijs'];
    $sku = $cart_itm['cat_item_code'];
}

Y esta es la solución que se me ocurrió, pero esto simplemente no funciona. La razón, supongo, es que los datos de la matriz $ órdenes se repite como una cadena y no código funcional como debería ser. Pero no tengo ni idea de cómo aproach este problema.

$i = 0;
        $lngth = count($_SESSION['cart_products']);

        foreach ($_SESSION['cart_products'] as $cart_itm) {

            $orders[] = '[';
            $orders[] .= '"sku" => ' . $cart_itm['cat_item_code'] . ',';
            $orders[] .= '"name" => ' . $cart_itm['cat_item_titel_en'] . ',';
            $orders[] .= '"quantity" => 1,';
            $orders[] .= '"vatRate" => "0.00",';
            $orders[] .= '"unitPrice" => [
                            "currency" => "EUR",
                            "value" => "15.50"],';
            $orders[] .= '"totalAmount" => [
                            "currency" => "EUR",
                            "value" => "15.50"],';
            $orders[] .= '"vatAmount" => [
                    "currency" => "EUR",
                    "value" => "0.00"]';

            if ($i == $lngth - 1) {
                $orders[] .= ']';
            }

            else {
                $orders[] .= '],';
            }

            $i++;

        }

        $echoOrders = implode($orders);


        $order = $mollie->orders->create([
            "amount" => [
                "value" => "15.50",
                "currency" => "EUR"
            ],
            "billingAddress" => [
                "streetAndNumber" => $address,
                "postalCode" => $postalcode,
                "city" => $city,
                "country" => $country,
                "givenName" => $firstname,
                "familyName" => $name,
                "email" => $email,
            ],
            "shippingAddress" => [
                "streetAndNumber" => $addressa,
                "postalCode" => $postalcodea,
                "city" => $citya,
                "country" => $countrya,
                "givenName" => $firstnamea,
                "familyName" => $namea,
                "email" => $email,
            ],
            "metadata" => [
                "order_id" => $orderId
            ],
            "locale" => "nl_BE",
            "orderNumber" => strval($orderId),
            "redirectUrl" => "{$protocol}://{$hostname}{$path}/thankyou.php?order_id={$orderId}",
            "webhookUrl" => "https://www.cluster-park.com/dev/includes/webhook.php",
            "lines" => [ $echoOrders ]
        ]);

Todas las sugerencias son más que bienvenidos. ¡Gracias!

Nigel Ren:

Es necesario construir una matriz asociativa, en el momento en que se está construyendo una cadena. Esto debe darle un punto de partida ...

    $lines = [];
    foreach ($_SESSION['cart_products'] as $cart_itm) {

        $lines[] = [ "sku" => $cart_itm['cat_item_code'],
                   "name" => $cart_itm['cat_item_titel_en'],
                   "quantity" => 1,
                   "vatRate" => "0.00",
                   "unitPrice" => [
                        "currency" => "EUR",
                        "value" => "15.50"]
                   ],
             // Add all of the other data you have
        ];

    }

entonces adelante en el código, se agregan en el uso de ...

"lines" => $lines

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=281209&siteId=1
Recomendado
Clasificación