Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/5669 add further payment metadata #7091

Merged
merged 13 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/add-5669-add-further-payment-metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Added more metadata to order
zmaglica marked this conversation as resolved.
Show resolved Hide resolved
44 changes: 35 additions & 9 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1482,16 +1482,42 @@ public function set_payment_method_title_for_order( $order, $payment_method_type
* @return array Array of keyed metadata values.
*/
protected function get_metadata_from_order( $order, $payment_type ) {
switch ( static::class ) {
zmaglica marked this conversation as resolved.
Show resolved Hide resolved
case UPE_Split_Payment_Gateway::class:
$gateway_type = 'split_upe';
break;
case UPE_Payment_Gateway::class:
$gateway_type = 'upe';
break;
default:
$gateway_type = 'classic';
}

switch ( $order->get_created_via() ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this switch statement really needed? Sending checkout and store-api to the server doesn't seem bad by itself. Also, even if something else happens, we'd never see unknown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Fixed here: 8f1b3dd

case 'checkout':
$checkout_type = 'shortcode';
break;
case 'store-api': // For block based orders, the created-via field is set to this value.
$checkout_type = 'block';
break;
default:
$checkout_type = 'unknown';
}

$name = sanitize_text_field( $order->get_billing_first_name() ) . ' ' . sanitize_text_field( $order->get_billing_last_name() );
$email = sanitize_email( $order->get_billing_email() );
$metadata = [
'customer_name' => $name,
'customer_email' => $email,
'site_url' => esc_url( get_site_url() ),
'order_id' => $order->get_id(),
'order_number' => $order->get_order_number(),
'order_key' => $order->get_order_key(),
'payment_type' => $payment_type,
'customer_name' => $name,
'customer_email' => $email,
'site_url' => esc_url( get_site_url() ),
'order_id' => $order->get_id(),
'order_number' => $order->get_order_number(),
'order_key' => $order->get_order_key(),
'payment_type' => $payment_type,
'gateway_type' => $gateway_type,
'checkout_type' => $checkout_type,
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
];

// If the order belongs to a WCPay Subscription, set the payment context to 'wcpay_subscription' (this helps with associating which fees belong to orders).
Expand All @@ -1500,12 +1526,12 @@ protected function get_metadata_from_order( $order, $payment_type ) {

foreach ( $subscriptions as $subscription ) {
if ( WC_Payments_Subscription_Service::is_wcpay_subscription( $subscription ) ) {
$metadata['payment_context'] = 'wcpay_subscription';
$metadata['payment_context'] = 'wcpay_subscription';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice how is_wcpay_subscription is called here, and the whole section is only entered if ! $this->is_subscriptions_plugin_active(). We need a separate block for standard subscriptions, one where 'recurring' === (string) $payment_type && $this->is_subscriptions_plugin_active().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed here: d1996ca

$metadata['subscription_payment'] = count( $subscriptions ) > 1 ? 'renewal' : 'initial';
break;
}
}
}

return apply_filters( 'wcpay_metadata_from_order', $metadata, $order, $payment_type );
}

Expand Down
54 changes: 33 additions & 21 deletions tests/unit/payment-methods/test-class-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,17 @@ public function test_update_payment_intent_adds_customer_save_payment_and_level3
->method( 'set_metadata' )
->with(
[
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
]
);

Expand Down Expand Up @@ -472,13 +476,17 @@ public function test_update_payment_intent_with_selected_upe_payment_method() {
->method( 'set_metadata' )
->with(
[
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
]
);

Expand Down Expand Up @@ -566,13 +574,17 @@ public function test_update_payment_intent_with_payment_country() {
->method( 'set_metadata' )
->with(
[
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
]
);

Expand Down
55 changes: 34 additions & 21 deletions tests/unit/payment-methods/test-class-upe-split-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,17 @@ public function test_update_payment_intent_adds_customer_save_payment_and_level3
->method( 'create_customer_for_user' );

$metadata = [
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show split UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
];

$level3 = [
Expand Down Expand Up @@ -479,13 +483,18 @@ public function test_update_payment_intent_with_selected_upe_payment_method() {
->method( 'create_customer_for_user' );

$metadata = [
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show split UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'client_version' => '6.3.2',
'subscription_payment' => 'no',

];

$level3 = [
Expand Down Expand Up @@ -553,13 +562,17 @@ public function test_update_payment_intent_with_payment_country() {
->method( 'create_customer_for_user' );

$metadata = [
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'customer_name' => 'Jeroen Sormani',
'customer_email' => '[email protected]',
'site_url' => 'http://example.org',
'order_id' => $order_id,
'order_number' => $order_number,
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show split UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
];

$level3 = [
Expand Down
18 changes: 11 additions & 7 deletions tests/unit/test-class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1314,13 +1314,17 @@ public function test_capture_charge_metadata() {
);

$merged_metadata = [
'customer_name' => 'Test',
'customer_email' => $order->get_billing_email(),
'site_url' => esc_url( get_site_url() ),
'order_id' => $order->get_id(),
'order_number' => $order->get_order_number(),
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'customer_name' => 'Test',
'customer_email' => $order->get_billing_email(),
'site_url' => esc_url( get_site_url() ),
'order_id' => $order->get_id(),
'order_number' => $order->get_order_number(),
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic',
'checkout_type' => 'unknown',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
];

$request = $this->mock_wcpay_request( Get_Intention::class, 1, $intent_id );
Expand Down