Skip to content

Commit

Permalink
Add/5669 add further payment metadata (#7091)
Browse files Browse the repository at this point in the history
Co-authored-by: Radoslav Georgiev <[email protected]>
  • Loading branch information
zmaglica and RadoslavGeorgiev authored Aug 31, 2023
1 parent 6b10aec commit 8ea5c5f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 73 deletions.
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 additional meta data to payment requests
39 changes: 21 additions & 18 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1484,30 +1484,33 @@ 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 ) {
if ( $this instanceof UPE_Split_Payment_Gateway ) {
$gateway_type = 'split_upe';
} elseif ( $this instanceof UPE_Payment_Gateway ) {
$gateway_type = 'upe';
} else {
$gateway_type = 'classic';
}
$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' => $order->get_created_via(),
'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).
if ( 'recurring' === (string) $payment_type && ! $this->is_subscriptions_plugin_active() ) {
$subscriptions = wcs_get_subscriptions_for_order( $order, [ 'order_type' => 'any' ] );

foreach ( $subscriptions as $subscription ) {
if ( WC_Payments_Subscription_Service::is_wcpay_subscription( $subscription ) ) {
$metadata['payment_context'] = 'wcpay_subscription';
break;
}
}
if ( 'recurring' === (string) $payment_type && function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order ) ) {
$metadata['subscription_payment'] = wcs_order_contains_renewal( $order ) ? 'renewal' : 'initial';
$metadata['payment_context'] = $this->is_subscriptions_plugin_active() ? 'regular_subscription' : 'wcpay_subscription';
}

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' => 'upe',
'checkout_type' => '',
'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' => 'upe',
'checkout_type' => '',
'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' => 'upe',
'checkout_type' => '',
'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' => 'split_upe',
'checkout_type' => '',
'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' => 'split_upe',
'checkout_type' => '',
'client_version' => WCPAY_VERSION_NUMBER,
'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' => 'split_upe',
'checkout_type' => '',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
];

$level3 = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,18 @@ function( $metadata ) {
}

public function test_saved_card_zero_dollar_subscription() {
$order = WC_Helper_Order::create_order( self::USER_ID, 0 );
$order = WC_Helper_Order::create_order( self::USER_ID, 0 );
$subscriptions = [ new WC_Subscription() ];
$subscriptions[0]->set_parent( $order );

$this->mock_wcs_order_contains_subscription( true );
$this->mock_wcs_get_subscriptions_for_order( $subscriptions );

$_POST = [
'payment_method' => WC_Payment_Gateway_WCPay::GATEWAY_ID,
self::TOKEN_REQUEST_KEY => $this->token->get_id(),
];

$this->mock_wcs_order_contains_subscription( true );

// The card is already saved and there's no payment needed, so no Setup Intent needs to be created.
$request = $this->mock_wcpay_request( Create_And_Confirm_Setup_Intention::class, 0 );

Expand All @@ -463,9 +466,6 @@ public function test_saved_card_zero_dollar_subscription() {
->expects( $this->never() )
->method( 'add_payment_method_to_user' );

$subscriptions = [ WC_Helper_Order::create_order( self::USER_ID ) ];
$this->mock_wcs_get_subscriptions_for_order( $subscriptions );

$result = $this->mock_wcpay_gateway->process_payment( $order->get_id() );
$result_order = wc_get_order( $order->get_id() );

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' => '',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
];

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

0 comments on commit 8ea5c5f

Please sign in to comment.