Skip to content

Commit

Permalink
Merge branch 'develop' into rpp/7411-duplicate-prevention
Browse files Browse the repository at this point in the history
  • Loading branch information
htdat authored Oct 26, 2023
2 parents c7a932d + ea53700 commit f273a60
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fix Afterpay checkout error when shipping information is missing
32 changes: 2 additions & 30 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -2903,34 +2903,6 @@ public function cancel_authorization( $order ) {
];
}

/**
* Create the shipping data array to send to Stripe when making a purchase.
*
* @param WC_Order $order The order that is being paid for.
* @return array The shipping data to send to Stripe.
*/
public function get_shipping_data_from_order( WC_Order $order ): array {
return [
'name' => implode(
' ',
array_filter(
[
$order->get_shipping_first_name(),
$order->get_shipping_last_name(),
]
)
),
'address' => [
'line1' => $order->get_shipping_address_1(),
'line2' => $order->get_shipping_address_2(),
'postal_code' => $order->get_shipping_postcode(),
'city' => $order->get_shipping_city(),
'state' => $order->get_shipping_state(),
'country' => $order->get_shipping_country(),
],
];
}

/**
* Create the level 3 data array to send to Stripe when making a purchase.
*
Expand Down Expand Up @@ -3749,11 +3721,11 @@ private function upe_needs_redirection( $payment_methods ) {
*
* @param Create_And_Confirm_Intention $request The request object for creating and confirming intention.
* @param Payment_Information $payment_information The payment information object.
* @param mixed $order The order object or data.
* @param WC_Order $order The order object.
*
* @return void
*/
protected function modify_create_intent_parameters_when_processing_payment( Create_And_Confirm_Intention $request, Payment_Information $payment_information, $order ) {
protected function modify_create_intent_parameters_when_processing_payment( Create_And_Confirm_Intention $request, Payment_Information $payment_information, WC_Order $order ) {
// Do nothing.
}
}
56 changes: 56 additions & 0 deletions includes/class-wc-payments-order-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,62 @@ public function attach_intent_info_to_order( $order, $intent_id, $intent_status,
$order->save();
}

/**
* Create the shipping data array to send to Stripe when making a purchase.
*
* @param WC_Order $order The order that is being paid for.
* @return array The shipping data to send to Stripe.
*/
public function get_shipping_data_from_order( WC_Order $order ): array {
return [
'name' => implode(
' ',
array_filter(
[
$order->get_shipping_first_name(),
$order->get_shipping_last_name(),
]
)
),
'address' => [
'line1' => $order->get_shipping_address_1(),
'line2' => $order->get_shipping_address_2(),
'postal_code' => $order->get_shipping_postcode(),
'city' => $order->get_shipping_city(),
'state' => $order->get_shipping_state(),
'country' => $order->get_shipping_country(),
],
];
}

/**
* Create the billing data array to send to Stripe when making a purchase, based on order's billing data.
*
* @param WC_Order $order The order that is being paid for.
* @return array The shipping data to send to Stripe.
*/
public function get_billing_data_from_order( WC_Order $order ): array {
return [
'name' => implode(
' ',
array_filter(
[
$order->get_billing_first_name(),
$order->get_billing_last_name(),
]
)
),
'address' => [
'line1' => $order->get_billing_address_1(),
'line2' => $order->get_billing_address_2(),
'postal_code' => $order->get_billing_postcode(),
'city' => $order->get_billing_city(),
'state' => $order->get_billing_state(),
'country' => $order->get_billing_country(),
],
];
}

/**
* Updates an order to cancelled status, while adding a note with a link to the transaction.
*
Expand Down
17 changes: 17 additions & 0 deletions includes/exceptions/class-invalid-address-exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Class Invalid_Address_Exception
*
* @package WooCommerce\Payments
*/

namespace WCPay\Exceptions;

use Exception;

defined( 'ABSPATH' ) || exit;

/**
* Exception for throwing errors when address is invalid.
*/
class Invalid_Address_Exception extends Exception {}
41 changes: 38 additions & 3 deletions includes/payment-methods/class-upe-split-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
namespace WCPay\Payment_Methods;

use Exception;
use WC_Order;
use WC_Payments_API_Setup_Intention;
use WC_Payments_Features;
use WCPay\Core\Server\Request\Get_Setup_Intention;
use WCPay\Exceptions\Add_Payment_Method_Exception;
use WCPay\Exceptions\Invalid_Address_Exception;
use WCPay\Exceptions\Process_Payment_Exception;
use WCPay\Logger;
use WCPay\Session_Rate_Limiter;
Expand Down Expand Up @@ -548,6 +550,39 @@ public function get_stripe_id() {
return $this->stripe_id;
}

/**
* Handles the shipping requirement for Afterpay payments.
*
* This method extracts the shipping and billing data from the order and sets the appropriate
* shipping data for the Afterpay payment request. If neither shipping nor billing data is valid
* for shipping, an exception is thrown.
*
* @param WC_Order $order The order object containing shipping and billing information.
* @param Create_And_Confirm_Intention $request The Afterpay payment request object to set shipping data on.
*
* @throws Invalid_Address_Exception If neither shipping nor billing address is valid for Afterpay payments.
* @return void
*/
private function handle_afterpay_shipping_requirement( WC_Order $order, Create_And_Confirm_Intention $request ): void {
$check_if_usable = function( array $address ): bool {
return $address['country'] && $address['state'] && $address['city'] && $address['postal_code'] && $address['line1'];
};

$shipping_data = $this->order_service->get_shipping_data_from_order( $order );
if ( $check_if_usable( $shipping_data['address'] ) ) {
$request->set_shipping( $shipping_data );
return;
}

$billing_data = $this->order_service->get_billing_data_from_order( $order );
if ( $check_if_usable( $billing_data['address'] ) ) {
$request->set_shipping( $billing_data );
return;
}

throw new Invalid_Address_Exception( __( 'A valid shipping address is required for Afterpay payments.', 'woocommerce-payments' ) );
}


/**
* Modifies the create intent parameters when processing a payment.
Expand All @@ -556,13 +591,13 @@ public function get_stripe_id() {
*
* @param Create_And_Confirm_Intention $request The request object for creating and confirming intention.
* @param Payment_Information $payment_information The payment information object.
* @param mixed $order The order object or data.
* @param WC_Order $order The order object.
*
* @return void
*/
protected function modify_create_intent_parameters_when_processing_payment( Create_And_Confirm_Intention $request, Payment_Information $payment_information, $order ) {
protected function modify_create_intent_parameters_when_processing_payment( Create_And_Confirm_Intention $request, Payment_Information $payment_information, WC_Order $order ): void {
if ( Payment_Method::AFTERPAY === $this->get_selected_stripe_payment_type_id() ) {
$request->set_shipping( $this->get_shipping_data_from_order( $order ) );
$this->handle_afterpay_shipping_requirement( $order, $request );
}
}
}

0 comments on commit f273a60

Please sign in to comment.