mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Allow skip ditch items when mismatch
This commit is contained in:
parent
526c25f501
commit
15a6685ff2
2 changed files with 62 additions and 58 deletions
|
@ -104,14 +104,14 @@ class PayUponInvoiceOrderEndpoint {
|
||||||
* @throws RuntimeException When there is a problem with the payment source.
|
* @throws RuntimeException When there is a problem with the payment source.
|
||||||
* @throws PayPalApiException When there is a problem creating the order.
|
* @throws PayPalApiException When there is a problem creating the order.
|
||||||
*/
|
*/
|
||||||
public function create( array $items, PaymentSource $payment_source, WC_Order $wc_order): Order {
|
public function create( array $items, PaymentSource $payment_source, WC_Order $wc_order ): Order {
|
||||||
|
|
||||||
$data = array(
|
$data = array(
|
||||||
'intent' => 'CAPTURE',
|
'intent' => 'CAPTURE',
|
||||||
'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL',
|
'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL',
|
||||||
'purchase_units' => array_map(
|
'purchase_units' => array_map(
|
||||||
static function ( PurchaseUnit $item ): array {
|
static function ( PurchaseUnit $item ): array {
|
||||||
return $item->to_array();
|
return $item->to_array( false );
|
||||||
},
|
},
|
||||||
$items
|
$items
|
||||||
),
|
),
|
||||||
|
@ -120,7 +120,7 @@ class PayUponInvoiceOrderEndpoint {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
$data = $this->ensure_taxes($wc_order, $data);
|
$data = $this->ensure_taxes( $wc_order, $data );
|
||||||
$data = $this->ensure_shipping( $data, $payment_source->to_array() );
|
$data = $this->ensure_shipping( $data, $payment_source->to_array() );
|
||||||
|
|
||||||
$bearer = $this->bearer->bearer();
|
$bearer = $this->bearer->bearer();
|
||||||
|
@ -226,47 +226,49 @@ class PayUponInvoiceOrderEndpoint {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param WC_Order $wc_order
|
* @param WC_Order $wc_order
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @param array $items
|
* @param array $items
|
||||||
* @return array
|
* @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(
|
$items = array_map(
|
||||||
function (WC_Order_Item_Product $item) use ($wc_order): Item {
|
function ( WC_Order_Item_Product $item ) use ( $wc_order ): Item {
|
||||||
$product = $item->get_product();
|
$product = $item->get_product();
|
||||||
$currency = $wc_order->get_currency();
|
$currency = $wc_order->get_currency();
|
||||||
$quantity = $item->get_quantity();
|
$quantity = $item->get_quantity();
|
||||||
$unit_amount = $wc_order->get_item_subtotal($item, false, false);
|
$unit_amount = $wc_order->get_item_subtotal( $item, false, false );
|
||||||
$tax_rates = WC_Tax::get_rates($product->get_tax_class());
|
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
|
||||||
$tax_rate = reset($tax_rates)['rate'] ?? 0;
|
$tax_rate = reset( $tax_rates )['rate'] ?? 0;
|
||||||
$tax = $unit_amount * ($tax_rate / 100);
|
$tax = $unit_amount * ( $tax_rate / 100 );
|
||||||
$tax = new Money($tax, $currency);
|
$tax = new Money( $tax, $currency );
|
||||||
|
|
||||||
return new Item(
|
return new Item(
|
||||||
mb_substr($item->get_name(), 0, 127),
|
mb_substr( $item->get_name(), 0, 127 ),
|
||||||
new Money($wc_order->get_item_subtotal($item, false, false), $currency),
|
new Money( $wc_order->get_item_subtotal( $item, false, false ), $currency ),
|
||||||
$quantity,
|
$quantity,
|
||||||
substr(wp_strip_all_tags($product instanceof WC_Product ? $product->get_description() : ''),
|
substr(
|
||||||
0, 127) ?: '',
|
wp_strip_all_tags( $product instanceof WC_Product ? $product->get_description() : '' ),
|
||||||
|
0,
|
||||||
|
127
|
||||||
|
) ?: '',
|
||||||
$tax,
|
$tax,
|
||||||
$product instanceof WC_Product ? $product->get_sku() : '',
|
$product instanceof WC_Product ? $product->get_sku() : '',
|
||||||
($product instanceof WC_Product && $product->is_virtual()) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
|
( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
|
||||||
$tax_rate
|
$tax_rate
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
$wc_order->get_items(),
|
$wc_order->get_items(),
|
||||||
array_keys($wc_order->get_items())
|
array_keys( $wc_order->get_items() )
|
||||||
);
|
);
|
||||||
|
|
||||||
$fees = array_map(
|
$fees = array_map(
|
||||||
function ( WC_Order_Item_Fee $item ) use ( $wc_order ): Item {
|
function ( WC_Order_Item_Fee $item ) use ( $wc_order ): Item {
|
||||||
$currency = $wc_order->get_currency();
|
$currency = $wc_order->get_currency();
|
||||||
$unit_amount = $item->get_amount();
|
$unit_amount = $item->get_amount();
|
||||||
$total_tax = $item->get_total_tax();
|
$total_tax = $item->get_total_tax();
|
||||||
$tax_rate = ($total_tax / $unit_amount) * 100;
|
$tax_rate = ( $total_tax / $unit_amount ) * 100;
|
||||||
$tax = $unit_amount * ($tax_rate / 100);
|
$tax = $unit_amount * ( $tax_rate / 100 );
|
||||||
$tax = new Money($tax, $currency);
|
$tax = new Money( $tax, $currency );
|
||||||
|
|
||||||
return new Item(
|
return new Item(
|
||||||
$item->get_name(),
|
$item->get_name(),
|
||||||
|
@ -284,63 +286,63 @@ class PayUponInvoiceOrderEndpoint {
|
||||||
|
|
||||||
$items = array_merge( $items, $fees );
|
$items = array_merge( $items, $fees );
|
||||||
|
|
||||||
$items_count = count($data['purchase_units'][0]['items']);
|
$items_count = count( $data['purchase_units'][0]['items'] );
|
||||||
for ($i = 0; $i < $items_count; $i++) {
|
for ( $i = 0; $i < $items_count; $i++ ) {
|
||||||
if (!isset($data['purchase_units'][0]['items'][$i]['tax'])) {
|
if ( ! isset( $data['purchase_units'][0]['items'][ $i ]['tax'] ) ) {
|
||||||
$data['purchase_units'][0]['items'][$i] = $items[$i]->to_array();
|
$data['purchase_units'][0]['items'][ $i ] = $items[ $i ]->to_array();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$shipping = (float)$wc_order->calculate_shipping();
|
$shipping = (float) $wc_order->calculate_shipping();
|
||||||
$total = 0;
|
$total = 0;
|
||||||
$tax_total = 0;
|
$tax_total = 0;
|
||||||
foreach ($items as $item) {
|
foreach ( $items as $item ) {
|
||||||
$unit_amount = (float)$item->unit_amount()->value();
|
$unit_amount = (float) $item->unit_amount()->value();
|
||||||
$tax = (float)$item->tax()->value();
|
$tax = (float) $item->tax()->value();
|
||||||
$qt = $item->quantity();
|
$qt = $item->quantity();
|
||||||
|
|
||||||
$total += (($unit_amount + $tax) * $qt);
|
$total += ( ( $unit_amount + $tax ) * $qt );
|
||||||
$tax_total += $tax * $qt;
|
$tax_total += $tax * $qt;
|
||||||
}
|
}
|
||||||
|
|
||||||
$data['purchase_units'][0]['amount']['value'] = number_format($total + $shipping, 2, '.', '');
|
$data['purchase_units'][0]['amount']['value'] = number_format( $total + $shipping, 2, '.', '' );
|
||||||
$data['purchase_units'][0]['amount']['breakdown']['tax_total']['value'] = number_format($tax_total, 2, '.', '');
|
$data['purchase_units'][0]['amount']['breakdown']['tax_total']['value'] = number_format( $tax_total, 2, '.', '' );
|
||||||
|
|
||||||
$shipping_taxes = (float) $wc_order->get_shipping_tax();
|
$shipping_taxes = (float) $wc_order->get_shipping_tax();
|
||||||
|
|
||||||
$fees_taxes = 0;
|
$fees_taxes = 0;
|
||||||
foreach($wc_order->get_fees() as $fee) {
|
foreach ( $wc_order->get_fees() as $fee ) {
|
||||||
$unit_amount = $fee->get_amount();
|
$unit_amount = $fee->get_amount();
|
||||||
$total_tax = $fee->get_total_tax();
|
$total_tax = $fee->get_total_tax();
|
||||||
$tax_rate = ($total_tax / $unit_amount) * 100;
|
$tax_rate = ( $total_tax / $unit_amount ) * 100;
|
||||||
$tax = $unit_amount * ($tax_rate / 100);
|
$tax = $unit_amount * ( $tax_rate / 100 );
|
||||||
|
|
||||||
$fees_taxes += $tax;
|
$fees_taxes += $tax;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($shipping_taxes > 0 || $fees_taxes > 0) {
|
if ( $shipping_taxes > 0 || $fees_taxes > 0 ) {
|
||||||
$name = $data['purchase_units'][0]['items'][0]['name'];
|
$name = $data['purchase_units'][0]['items'][0]['name'];
|
||||||
$category = $data['purchase_units'][0]['items'][0]['category'];
|
$category = $data['purchase_units'][0]['items'][0]['category'];
|
||||||
$tax_rate = $data['purchase_units'][0]['items'][0]['tax_rate'];
|
$tax_rate = $data['purchase_units'][0]['items'][0]['tax_rate'];
|
||||||
|
|
||||||
unset($data['purchase_units'][0]['items']);
|
unset( $data['purchase_units'][0]['items'] );
|
||||||
$data['purchase_units'][0]['items'][0] = array(
|
$data['purchase_units'][0]['items'][0] = array(
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
'unit_amount' => array(
|
'unit_amount' => array(
|
||||||
'currency_code' => 'EUR',
|
'currency_code' => 'EUR',
|
||||||
'value' => $data['purchase_units'][0]['amount']['breakdown']['item_total']['value'],
|
'value' => $data['purchase_units'][0]['amount']['breakdown']['item_total']['value'],
|
||||||
),
|
),
|
||||||
'category' => $category,
|
'category' => $category,
|
||||||
'quantity' => 1,
|
'quantity' => 1,
|
||||||
'tax' => array(
|
'tax' => array(
|
||||||
'currency_code' => 'EUR',
|
'currency_code' => 'EUR',
|
||||||
'value' => number_format($tax_total + $shipping_taxes, 2, '.', ''),
|
'value' => number_format( $tax_total + $shipping_taxes, 2, '.', '' ),
|
||||||
),
|
),
|
||||||
'tax_rate' => $tax_rate,
|
'tax_rate' => $tax_rate,
|
||||||
);
|
);
|
||||||
|
|
||||||
$data['purchase_units'][0]['amount']['value'] = number_format($total + $shipping + $shipping_taxes, 2, '.', '');
|
$data['purchase_units'][0]['amount']['value'] = number_format( $total + $shipping + $shipping_taxes, 2, '.', '' );
|
||||||
$data['purchase_units'][0]['amount']['breakdown']['tax_total']['value'] = number_format($tax_total + $shipping_taxes, 2, '.', '');
|
$data['purchase_units'][0]['amount']['breakdown']['tax_total']['value'] = number_format( $tax_total + $shipping_taxes, 2, '.', '' );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
|
|
|
@ -268,9 +268,11 @@ class PurchaseUnit {
|
||||||
/**
|
/**
|
||||||
* Returns the object as array.
|
* Returns the object as array.
|
||||||
*
|
*
|
||||||
|
* @param bool $ditch_items_when_mismatch Whether ditch items when mismatch or not.
|
||||||
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function to_array(): array {
|
public function to_array( bool $ditch_items_when_mismatch = true ): array {
|
||||||
$purchase_unit = array(
|
$purchase_unit = array(
|
||||||
'reference_id' => $this->reference_id(),
|
'reference_id' => $this->reference_id(),
|
||||||
'amount' => $this->amount()->to_array(),
|
'amount' => $this->amount()->to_array(),
|
||||||
|
@ -282,7 +284,7 @@ class PurchaseUnit {
|
||||||
$this->items()
|
$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['items'] );
|
||||||
unset( $purchase_unit['amount']['breakdown'] );
|
unset( $purchase_unit['amount']['breakdown'] );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue