Bug: [PLUGIN] - WooCommerce - Refactor Code for Clarity and Readability



The [WooCommerce-Payments-Plugin](https://docs.hyperswitch.io/features/e-commerce-platform-plugins/woocommerce-plugin) connect with multiple payment processors with a single API to improve payment conversions, reduce costs and ops. It supports multiple currencies, offers fraud protection, and provides an easy-to-use dashboard for managing transactions and tracking sales.

### Getting started

1. Get familiar with [Php](https://www.php.net/manual/en/index.php) and [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript).
2. Check the [README.md](https://github.com/juspay/hyperswitch-woocommerce-plugin/blob/main/README.md) for project structure and setup instructions.
3. To setup locally, follow the steps provided [here](https://github.com/juspay/hyperswitch-woocommerce-plugin/blob/main/README.md)

### **Description:**

You are provided with a code snippet that performs a specific function. Your task is to refactor the code to improve its readability, performance, and maintainability. Consider applying best practices such as input validation, descriptive variable naming, reducing complexity, and handling edge cases. The goal is to make the code more efficient, cleaner, and easier to understand while maintaining its core functionality.

Code Snippet - 

This provided code snippet is a part of [WooCommerce-Plugin](https://github.com/juspay/hyperswitch-woocommerce-plugin/blob/main/js/hyperswitch-hyperservice.js).

```javascript
async function hyperswitchPaymentHandleSubmit(isOneClickPaymentMethod, result) {
  if (result || isOneClickPaymentMethod) {
    let err;
    if (!isOneClickPaymentMethod && result) {
      const { error } = await hyper.confirmPayment({
        confirmParams: {
          return_url: hyperswitchReturnUrl,
        },
        redirect: "if_required",
      });
      err = error;
    } else {
      const { error } = await hyper.confirmOneClickPayment(
        {
          confirmParams: {
            return_url: hyperswitchReturnUrl,
          },
          redirect: "if_required",
        },
        result
      );
      err = error;
    }
    if (err) {
      if (err.type) {
        if (err.type == "validation_error") {
          jQuery([document.documentElement, document.body]).animate(
            {
              scrollTop: jQuery(
                ".payment_box.payment_method_hyperswitch_checkout"
              ).offset().top,
            },
            500
          );
        } else {
          location.href = hyperswitchReturnUrl;
        }
      } else {
        location.href = hyperswitchReturnUrl;
      }
      jQuery(".payment_method_hyperswitch_checkout").unblock();
    } else {
      location.href = hyperswitchReturnUrl;
    }
  }
}
````
---

## Contribution Guidelines:

- Fork the repository and create a new branch for your work.
- Ensure the WebSDK follows best practices for API integration and field rendering.
- Write clean, well-documented code with clear commit messages.
- Add unit tests to ensure the dynamic field rendering works as expected.
- Make sure to follow our coding standards and contribution guidelines.

---

## Helpful Resources:

- Link to WooCommerce-Plugin documentation: [WooCommerce](https://docs.hyperswitch.io/features/e-commerce-platform-plugins/woocommerce-plugin)

### Submission Process:

- Ask the maintainers for assignment of the issue, you can request for assignment by commenting on the issue itself.
- Once assigned, submit a pull request (PR).
- Maintainers will review and provide feedback, if any.
- Maintainers can unassign issues due to inactivity, [read more here](https://github.com/juspay/hyperswitch/wiki/Hacktoberfest-Contribution-Rules).
- For this issue, please submit a PR on [@juspay/hyperswitch-woocommerce-plugin](https://github.com/juspay/hyperswitch-woocommerce-plugin) repo, and link it to the issue.

Refer [here](https://github.com/juspay/hyperswitch/blob/main/docs/TERMS_OF_CONTEST.md) for Terms and conditions for the contest.

If you have any questions or need help getting started, feel free to ask in the comments!

diff --git a/crates/router/src/connector/aci/transformers.rs b/crates/router/src/connector/aci/transformers.rs
index c7dfe239526..4eecc3c1eeb 100644
--- a/crates/router/src/connector/aci/transformers.rs
+++ b/crates/router/src/connector/aci/transformers.rs
@@ -56,6 +56,7 @@ pub enum PaymentDetails {
     BankAccount(BankDetails),
     Wallet,
     Klarna,
+    Paypal,
 }
 
 #[derive(Clone, Eq, PartialEq, Serialize)]
@@ -112,6 +113,7 @@ impl TryFrom<&types::PaymentsRouterData> for AciPaymentsRequest {
             }),
             api::PaymentMethod::PayLater(_) => PaymentDetails::Klarna,
             api::PaymentMethod::Wallet => PaymentDetails::Wallet,
+            api::PaymentMethod::Paypal => PaymentDetails::Paypal,
         };
 
         let auth = AciAuthType::try_from(&item.connector_auth_type)?;
diff --git a/crates/router/src/connector/adyen/transformers.rs b/crates/router/src/connector/adyen/transformers.rs
index 381ef3412aa..81bfb2807ff 100644
--- a/crates/router/src/connector/adyen/transformers.rs
+++ b/crates/router/src/connector/adyen/transformers.rs
@@ -209,6 +209,7 @@ impl TryFrom<&types::PaymentsRouterData> for AdyenPaymentRequest {
             api::PaymentMethod::BankTransfer => None,
             api::PaymentMethod::Wallet => None,
             api::PaymentMethod::PayLater(_) => None,
+            api::PaymentMethod::Paypal => None,
         };
 
         let shopper_interaction = match item.request.off_session {
diff --git a/crates/router/src/connector/authorizedotnet/transformers.rs b/crates/router/src/connector/authorizedotnet/transformers.rs
index bda7718fdb5..c6eed4a5d88 100644
--- a/crates/router/src/connector/authorizedotnet/transformers.rs
+++ b/crates/router/src/connector/authorizedotnet/transformers.rs
@@ -59,6 +59,7 @@ enum PaymentDetails {
     BankAccount(BankAccountDetails),
     Wallet,
     Klarna,
+    Paypal,
 }
 
 #[derive(Serialize, PartialEq)]
@@ -146,6 +147,7 @@ impl TryFrom<&types::PaymentsRouterData> for CreateTransactionRequest {
             }),
             api::PaymentMethod::PayLater(_) => PaymentDetails::Klarna,
             api::PaymentMethod::Wallet => PaymentDetails::Wallet,
+            api::PaymentMethod::Paypal => PaymentDetails::Paypal,
         };
         let authorization_indicator_type =
             item.request.capture_method.map(|c| AuthorizationIndicator {
@@ -350,6 +352,7 @@ impl<F> TryFrom<&types::RefundsRouterData<F>> for CreateRefundRequest {
             }),
             api::PaymentMethod::PayLater(_) => PaymentDetails::Klarna,
             api::PaymentMethod::Wallet => PaymentDetails::Wallet,
+            api::PaymentMethod::Paypal => PaymentDetails::Paypal,
         };
 
         merchant_authentication = MerchantAuthentication::try_from(&item.connector_auth_type)?;
diff --git a/crates/router/src/connector/checkout/transformers.rs b/crates/router/src/connector/checkout/transformers.rs
index 7359599822d..a1deedf9ac2 100644
--- a/crates/router/src/connector/checkout/transformers.rs
+++ b/crates/router/src/connector/checkout/transformers.rs
@@ -75,6 +75,7 @@ impl TryFrom<&types::PaymentsRouterData> for PaymentsRequest {
             api::PaymentMethod::BankTransfer => None,
             api::PaymentMethod::Wallet => None,
             api::PaymentMethod::PayLater(_) => None,
+            api::PaymentMethod::Paypal => None,
         };
 
         let three_ds = match item.auth_type {
diff --git a/crates/router/src/connector/stripe/transformers.rs b/crates/router/src/connector/stripe/transformers.rs
index 897953d226c..b376adef59b 100644
--- a/crates/router/src/connector/stripe/transformers.rs
+++ b/crates/router/src/connector/stripe/transformers.rs
@@ -123,6 +123,7 @@ pub enum StripePaymentMethodData {
     Klarna(StripeKlarnaData),
     Bank,
     Wallet,
+    Paypal,
 }
 
 impl TryFrom<&types::PaymentsRouterData> for PaymentIntentRequest {
@@ -160,6 +161,7 @@ impl TryFrom<&types::PaymentsRouterData> for PaymentIntentRequest {
                 })
             }
             api::PaymentMethod::Wallet => StripePaymentMethodData::Wallet,
+            api::PaymentMethod::Paypal => StripePaymentMethodData::Paypal,
         };
         let shipping_address = match item.address.shipping.clone() {
             Some(mut shipping) => Address {
diff --git a/crates/router/src/types/api/payments.rs b/crates/router/src/types/api/payments.rs
index d69db679042..bdd79cd6410 100644
--- a/crates/router/src/types/api/payments.rs
+++ b/crates/router/src/types/api/payments.rs
@@ -153,6 +153,8 @@ pub enum PaymentMethod {
     Wallet,
     #[serde(rename(deserialize = "pay_later"))]
     PayLater(PayLaterData),
+    #[serde(rename(deserialize = "paypal"))]
+    Paypal,
 }
 
 #[derive(Eq, PartialEq, Clone, Debug, serde::Serialize)]
@@ -170,6 +172,7 @@ pub enum PaymentMethodDataResponse {
     BankTransfer,
     Wallet,
     PayLater(PayLaterData),
+    Paypal,
 }
 
 impl Default for PaymentMethod {
@@ -541,6 +544,7 @@ impl From<PaymentMethod> for PaymentMethodDataResponse {
                 PaymentMethodDataResponse::PayLater(pay_later_data)
             }
             PaymentMethod::Wallet => PaymentMethodDataResponse::Wallet,
+            PaymentMethod::Paypal => PaymentMethodDataResponse::Paypal,
         }
     }
 }
diff --git a/crates/router/src/types/storage/enums.rs b/crates/router/src/types/storage/enums.rs
index d76267ec2d9..72fae43632a 100644
--- a/crates/router/src/types/storage/enums.rs
+++ b/crates/router/src/types/storage/enums.rs
@@ -470,6 +470,7 @@ pub enum PaymentMethodType {
     ConsumerFinance,
     Wallet,
     Klarna,
+    Paypal,
 }
 
 #[derive(
diff --git a/migrations/2022-11-25-121143_add_paypal_pmt/down.sql b/migrations/2022-11-25-121143_add_paypal_pmt/down.sql
new file mode 100644
index 00000000000..4c5dda8ed30
--- /dev/null
+++ b/migrations/2022-11-25-121143_add_paypal_pmt/down.sql
@@ -0,0 +1,6 @@
+-- This file should undo anything in `up.sql`
+DELETE FROM pg_enum
+WHERE enumlabel = 'paypal'
+AND enumtypid = (
+  SELECT oid FROM pg_type WHERE typname = 'PaymentMethodType'
+)
diff --git a/migrations/2022-11-25-121143_add_paypal_pmt/up.sql b/migrations/2022-11-25-121143_add_paypal_pmt/up.sql
new file mode 100644
index 00000000000..732ed26504b
--- /dev/null
+++ b/migrations/2022-11-25-121143_add_paypal_pmt/up.sql
@@ -0,0 +1,2 @@
+-- Your SQL goes here
+ALTER TYPE "PaymentMethodType" ADD VALUE 'paypal' after 'pay_later';

## Type of Change
<!-- Put an `x` in the boxes that apply -->

- [ ] Bugfix
- [x] New feature
- [ ] Enhancement
- [ ] Refactoring
- [ ] Dependency updates

## Description
Add paypal as payment method type


### Additional Changes

- [x] This PR modifies the database schema
- [ ] This PR modifies application configuration/environment variables

<!-- 
Provide links to the files with corresponding changes.

Following are the paths where you can find config files:
1. `config`
2. `crates/router/src/configs`
3. `loadtest/config`
-->


## Motivation and Context
<!--
Why is this change required? What problem does it solve?
If it fixes an open issue, please link to the issue here.

If you don't have an issue, we'd recommend starting with one first so the PR
can focus on the implementation (unless its an obvious bug or documentation fix
that will have little conversation).
-->
Earlier, when PayPal was merged, we were using wallet as the payment method; however, because PayPal is the payment method when we are integrating PayPal through Adyen, the payment method should be changed to PayPal. Thus, this is resolved.



## How did you test it?
<!--
Did you write an integration/unit/API test to verify the code changes?
Or did you test this change manually (provide relevant screenshots)?
-->
Manually tested (No DB error when payment method is paypal)


## Checklist
<!-- Put an `x` in the boxes that apply -->

- [x] I formatted the code `cargo +nightly fmt`
- [x] I addressed lints thrown by `cargo clippy`
- [x] I reviewed submitted code
- [ ] I added unit tests for my changes where possible
- [ ] I added a [CHANGELOG](/CHANGELOG.md) entry if applicable
