Incorrect line item price sent to Afterpay for quantities > 1

Created on 20 October 2023, 8 months ago
Updated 16 March 2024, 3 months ago

Afterpay's documentation indicates that the price of a given Item should be an individual unit price.

https://developers.afterpay.com/docs/api/online-api/data-models/item-object

The createItem() method in RemoteModel is setting a line item total price, which already accounts for quantity.

/**
   * It can only handle order items linked to product variations.
   *
   * @param \Drupal\commerce_order\Entity\OrderItemInterface $item
   *
   * @return \Afterpay\SDK\Model\Item
   */
  public function createItem(OrderItemInterface $item): Item {
    $variation = $item->getPurchasedEntity();
    if (!$variation instanceof ProductVariationInterface) {
      throw new \RuntimeException(t("Can't create Afterpay item from non-product order item."));
    }
    return new Item([
      'name' => $item->label(),
      'sku' => $variation->getSku(),
      'quantity' => intval($item->getQuantity()),
      'pageUrl' => $variation->toUrl()->toString(),
      'price' => $this->createMoney($item->getTotalPrice()),
      // @todo Add event to populate 'imageUrl' and 'categories'.
    ]);
  }
πŸ› Bug report
Status

RTBC

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States cornifex

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @cornifex
  • Merge request !13Use unit price for Afterpay item β†’ (Merged) created by cornifex
  • Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 8
    last update 8 months ago
    Waiting for branch to pass
  • Issue was unassigned.
  • Status changed to Needs review 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States cornifex
  • Status changed to RTBC 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States jds1 Hudson Valley, NY

    RTBC!

  • First commit to issue fork.
  • πŸ‡¬πŸ‡§United Kingdom AndyF

    Thanks @corniflex!

    Rebased against 1.x and updated changelog.

    I don't have access to a site with Afterpay anymore so wasn't able to run the API integration test. (It's not extensive anyway, but much better than nothing.) I've updated the test so that instead of adding one item to the cart, it adds two items which now cost half the price. Otherwise the test remains unchanged, so hopefully it'll work - but as I say, I can't run it without sandbox creds.

    If some kind soul with access to a sandbox wants to run the tests, I'd very much appreciate it.

    There's a functional javascript test that can be run to test the checkout and
    payment capture/void process. The test assumes that Afterpay will accept a total
    of ~45 USD.

    1. Set up an account on the sandbox portal (eg. at
      https://portal.sandbox.afterpay.com/us). You'll need to set the password and
      it should already have a single saved payment method.
    2. Set the following environment variables:
      • COMMERCE_AFTERPAY_TEST_MERCHANT_ID<\code>
      • COMMERCE_AFTERPAY_TEST_SECRET_KEY<\code>
      • COMMERCE_AFTERPAY_TEST_COUNTRY_CODE<\code>
      • COMMERCE_AFTERPAY_TEST_CUSTOMER_EMAIL<\code>
      • COMMERCE_AFTERPAY_TEST_CUSTOMER_PASSWORD<\code>

    https://git.drupalcode.org/project/commerce_afterpay#testing-against-the-sandbox-api

  • Pipeline finished with Skipped
    3 months ago
    #120969
  • πŸ‡¬πŸ‡§United Kingdom AndyF

    Merging because it makes sense per the documentation and is clearly major, but leaving open for feedback on \Drupal\Tests\commerce_afterpay\FunctionalJavascript\ApiIntegrationTest.

Production build 0.69.0 2024