Manage baskets (v2)
Submit basket information within your payment.
A basket is a collection of product information for each transaction. It is not a mandatory resource, but required for risk analysis for some payment methods, such as Unzer Direct Debit Secured and Unzer Invoice.
How it works
- In your eCommerce site, the customer selects the items they want to purchase. You send a POST request to create the
v2/basketresource. A unique basket ID is created.
basketIdcan now be used when the payment is initiated.
Once a basket has been created, it cannot be replaced or deleted. If you haven’t assigned a basket to a transaction yet, you can update it. This means you cannot change specific attributes, but you can provide new basket details. For example, you can add more items, remove items, but cannot update the basket ID.
To learn more about the baskets, see Basket section.
Creating a basket
To create a new basket resource, send a POST request with your desired contents in the JSON format.
The SDK provides the
Unzer::createBasket() method to do so.
v2/baskets endpoint will automatically be used when the basket object has the
totalValueGross property set.
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx'); $basketItem = (new UnzerSDK\Resources\EmbeddedResources\BasketItem()) ->setBasketItemReferenceId('Artikelnummer4711') ->setQuantity(5) ->setTotalValueGross(100.1) ->setVat(19.0) ->setTitle('Item Title 1') ->setAmountDiscountPerUnitGross(0.1) ->setSubTitle('Red case') ->setImageUrl('https://url.to.the.basket.item.image') ->setType(UnzerSDK\Constants\BasketItemTypes::GOODS); $basket = (new UnzerSDK\Resources\Basket()) ->setTotalValueGross(500) ->setCurrencyCode('EUR') ->setOrderId('uniqueOrderId_1') ->addBasketItem($basketItem); $unzer->createBasket($basket);
- Although it’s possible to use any character combination for the
orderId, we recommend using the same
orderIdas for payments (created by
types/charge, and so on).
- The order ID must be unique.
The response contains a basket ID that is saved in your basket object.
Fetching a basket
To fetch a specific
basket resource, use your private key and send a GET request with the basket ID.
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx'); $basket = $unzer->fetchBasket('s-bsk-1');
fetchBasket method returns a basket instance containing the basket information.
Go to the API reference for more information.
Adding a basket to your transaction
Here you can see how to add a basket to a transaction, in our example a
charge request. To add a basket to a transaction, add the basket ID to your
resources parameter list as shown in the following example:
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx'); $charge = new Charge(500.00, 'EUR', $returnUrl); $unzer->performCharge($charge, 's-crd-9wmri5mdlqps', null, null, 's-bsk-1');
Updating a basket
In some cases, it might be necessary to change an existing basket, for example if the customer added a voucher or the shipping fees changed.
In this case, you must update the whole basket. You can’t change specific parts of a basket. Either copy the contents of the old one, alter them and submit it using the
Unzer::updateBasket method, or create a new basket.
$unzer = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx'); $basket= $unzer->fetchBasket('s-bsk-1'); $basket->setTotalValueGross(400); $basket->getBasketItemByIndex(0)->setQuantity(4); $unzer->updateBasket($basket);