mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Add test cases for PurchaseUnitSanitizer
This commit is contained in:
parent
837bdb0392
commit
a43b93ffff
4 changed files with 276 additions and 23 deletions
|
@ -112,7 +112,7 @@ class PayUponInvoiceOrderEndpoint {
|
|||
'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL',
|
||||
'purchase_units' => array_map(
|
||||
static function ( PurchaseUnit $item ): array {
|
||||
return $item->to_array( false );
|
||||
return $item->to_array( true, false );
|
||||
},
|
||||
$items
|
||||
),
|
||||
|
|
|
@ -297,10 +297,11 @@ class PurchaseUnit {
|
|||
* Returns the object as array.
|
||||
*
|
||||
* @param bool $sanitize_output Whether output should be sanitized for PayPal consumption.
|
||||
* @param bool $allow_ditch_items Whether to allow items to be ditched.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array( bool $sanitize_output = true ): array {
|
||||
public function to_array( bool $sanitize_output = true, bool $allow_ditch_items = true ): array {
|
||||
$purchase_unit = array(
|
||||
'reference_id' => $this->reference_id(),
|
||||
'amount' => $this->amount()->to_array(),
|
||||
|
@ -335,7 +336,7 @@ class PurchaseUnit {
|
|||
}
|
||||
|
||||
if ( $sanitize_output && isset( $this->sanitizer ) ) {
|
||||
$purchase_unit = ( $this->sanitizer->sanitize( $purchase_unit, $this->items() ) );
|
||||
$purchase_unit = ( $this->sanitizer->sanitize( $purchase_unit, $this->items(), $allow_ditch_items ) );
|
||||
}
|
||||
|
||||
return $purchase_unit;
|
||||
|
|
|
@ -47,6 +47,13 @@ class PurchaseUnitSanitizer {
|
|||
*/
|
||||
private $item_objects = array();
|
||||
|
||||
/**
|
||||
* Whether to allow items to be ditched.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $allow_ditch_items = true;
|
||||
|
||||
/**
|
||||
* The working mode
|
||||
*
|
||||
|
@ -154,11 +161,13 @@ class PurchaseUnitSanitizer {
|
|||
*
|
||||
* @param array $purchase_unit The purchase_unit array that should be sanitized.
|
||||
* @param array|Item[] $item_objects The purchase unit Item objects used for recalculations.
|
||||
* @param bool $allow_ditch_items Whether to allow items to be ditched.
|
||||
* @return array
|
||||
*/
|
||||
public function sanitize( array $purchase_unit, array $item_objects ): array {
|
||||
$this->purchase_unit = $purchase_unit;
|
||||
$this->item_objects = $item_objects;
|
||||
public function sanitize( array $purchase_unit, array $item_objects, bool $allow_ditch_items = true ): array {
|
||||
$this->purchase_unit = $purchase_unit;
|
||||
$this->item_objects = $item_objects;
|
||||
$this->allow_ditch_items = $allow_ditch_items;
|
||||
|
||||
$this->sanitize_item_amount_mismatch();
|
||||
$this->sanitize_item_tax_mismatch();
|
||||
|
@ -176,6 +185,7 @@ class PurchaseUnitSanitizer {
|
|||
|
||||
if ( $this->is_mode_extra_line() ) {
|
||||
if ( $item_mismatch < 0 ) {
|
||||
|
||||
// Do floors on item amounts so item_mismatch is a positive value.
|
||||
foreach ( $this->item_objects as $index => $item ) {
|
||||
$this->purchase_unit['items'][ $index ] = $item->to_array(
|
||||
|
@ -198,7 +208,7 @@ class PurchaseUnitSanitizer {
|
|||
|
||||
if ( $item_mismatch !== 0.0 ) {
|
||||
// Ditch items.
|
||||
if ( isset( $this->purchase_unit['items'] ) ) {
|
||||
if ( $this->allow_ditch_items && isset( $this->purchase_unit['items'] ) ) {
|
||||
unset( $this->purchase_unit['items'] );
|
||||
}
|
||||
}
|
||||
|
@ -212,7 +222,7 @@ class PurchaseUnitSanitizer {
|
|||
private function sanitize_item_tax_mismatch(): void {
|
||||
$tax_mismatch = $this->calculate_tax_mismatch();
|
||||
|
||||
if ( $tax_mismatch !== 0.0 ) {
|
||||
if ( $this->allow_ditch_items && $tax_mismatch !== 0.0 ) {
|
||||
// Unset tax in items.
|
||||
foreach ( $this->purchase_unit['items'] as $index => $item ) {
|
||||
if ( isset( $this->purchase_unit['items'][ $index ]['tax'] ) ) {
|
||||
|
@ -233,7 +243,7 @@ class PurchaseUnitSanitizer {
|
|||
private function sanitize_breakdown_mismatch(): void {
|
||||
$breakdown_mismatch = $this->calculate_breakdown_mismatch();
|
||||
|
||||
if ( $breakdown_mismatch !== 0.0 ) {
|
||||
if ( $this->allow_ditch_items && $breakdown_mismatch !== 0.0 ) {
|
||||
// Ditch breakdowns and items.
|
||||
if ( isset( $this->purchase_unit['items'] ) ) {
|
||||
unset( $this->purchase_unit['items'] );
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue