Allow skip ditch items when mismatch

This commit is contained in:
dinamiko 2022-08-30 16:40:04 +02:00
parent 526c25f501
commit 15a6685ff2
2 changed files with 62 additions and 58 deletions

View file

@ -111,7 +111,7 @@ class PayUponInvoiceOrderEndpoint {
'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL',
'purchase_units' => array_map(
static function ( PurchaseUnit $item ): array {
return $item->to_array();
return $item->to_array( false );
},
$items
),
@ -230,8 +230,7 @@ class PayUponInvoiceOrderEndpoint {
* @param array $items
* @return array
*/
private function ensure_taxes(WC_Order $wc_order, array $data): array
{
private function ensure_taxes( WC_Order $wc_order, array $data ): array {
$items = array_map(
function ( WC_Order_Item_Product $item ) use ( $wc_order ): Item {
$product = $item->get_product();
@ -247,8 +246,11 @@ class PayUponInvoiceOrderEndpoint {
mb_substr( $item->get_name(), 0, 127 ),
new Money( $wc_order->get_item_subtotal( $item, false, false ), $currency ),
$quantity,
substr(wp_strip_all_tags($product instanceof WC_Product ? $product->get_description() : ''),
0, 127) ?: '',
substr(
wp_strip_all_tags( $product instanceof WC_Product ? $product->get_description() : '' ),
0,
127
) ?: '',
$tax,
$product instanceof WC_Product ? $product->get_sku() : '',
( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,

View file

@ -268,9 +268,11 @@ class PurchaseUnit {
/**
* Returns the object as array.
*
* @param bool $ditch_items_when_mismatch Whether ditch items when mismatch or not.
*
* @return array
*/
public function to_array(): array {
public function to_array( bool $ditch_items_when_mismatch = true ): array {
$purchase_unit = array(
'reference_id' => $this->reference_id(),
'amount' => $this->amount()->to_array(),
@ -282,7 +284,7 @@ class PurchaseUnit {
$this->items()
),
);
if ( $this->ditch_items_when_mismatch( $this->amount(), ...$this->items() ) ) {
if ( $ditch_items_when_mismatch && $this->ditch_items_when_mismatch( $this->amount(), ...$this->items() ) ) {
unset( $purchase_unit['items'] );
unset( $purchase_unit['amount']['breakdown'] );
}