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

feat(connector): [BANKOFAMERICA] Implement Apple Pay #3061

Merged
merged 6 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
82 changes: 79 additions & 3 deletions crates/router/src/connector/bankofamerica/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use serde::{Deserialize, Serialize};

use crate::{
connector::utils::{
self, AddressDetailsData, CardData, CardIssuer, PaymentsAuthorizeRequestData,
PaymentsSyncRequestData, RouterData,
self, AddressDetailsData, ApplePayDecrypt, CardData, CardIssuer,
PaymentsAuthorizeRequestData, PaymentsSyncRequestData, RouterData,
},
consts,
core::errors,
Expand All @@ -16,6 +16,7 @@ use crate::{
api::{self, enums as api_enums},
storage::enums,
transformers::ForeignFrom,
ApplePayPredecryptData,
},
};

Expand Down Expand Up @@ -110,11 +111,18 @@ pub struct GooglePayPaymentInformation {
fluid_data: FluidData,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ApplePayPaymentInformation {
tokenized_card: TokenizedCard,
}

#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum PaymentInformation {
Cards(CardPaymentInformation),
GooglePay(GooglePayPaymentInformation),
ApplePay(ApplePayPaymentInformation),
}

#[derive(Debug, Serialize)]
Expand All @@ -128,6 +136,16 @@ pub struct Card {
card_type: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TokenizedCard {
number: Secret<String>,
expiration_month: Secret<String>,
expiration_year: Secret<String>,
cryptogram: Secret<String>,
transaction_type: TransactionType,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FluidData {
Expand Down Expand Up @@ -215,6 +233,12 @@ impl From<PaymentSolution> for String {
}
}

#[derive(Debug, Serialize)]
pub enum TransactionType {
#[serde(rename = "1")]
ApplePay,
}

impl
From<(
&BankOfAmericaRouterData<&types::PaymentsAuthorizeRouterData>,
Expand Down Expand Up @@ -320,6 +344,48 @@ impl
}
}

impl
TryFrom<(
&BankOfAmericaRouterData<&types::PaymentsAuthorizeRouterData>,
Box<ApplePayPredecryptData>,
)> for BankOfAmericaPaymentsRequest
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(
(item, apple_pay_data): (
&BankOfAmericaRouterData<&types::PaymentsAuthorizeRouterData>,
Box<ApplePayPredecryptData>,
),
) -> Result<Self, Self::Error> {
let email = item.router_data.request.get_email()?;
let bill_to = build_bill_to(item.router_data.get_billing()?, email)?;
let order_information = OrderInformationWithBill::from((item, bill_to));
let processing_information =
ProcessingInformation::from((item, Some(PaymentSolution::ApplePay)));
let client_reference_information = ClientReferenceInformation::from(item);

let expiration_month = apple_pay_data.get_applepay_decrypted_expiration_month()?;
deepanshu-iiitu marked this conversation as resolved.
Show resolved Hide resolved
let expiration_year = apple_pay_data.get_applepay_decrypted_expiration_year_4_digit()?;

let payment_information = PaymentInformation::ApplePay(ApplePayPaymentInformation {
tokenized_card: TokenizedCard {
number: apple_pay_data.application_primary_account_number,
cryptogram: apple_pay_data.payment_data.online_payment_cryptogram,
transaction_type: TransactionType::ApplePay,
expiration_year,
expiration_month,
},
});

Ok(Self {
processing_information,
payment_information,
order_information,
client_reference_information,
})
}
}

impl
TryFrom<(
&BankOfAmericaRouterData<&types::PaymentsAuthorizeRouterData>,
Expand Down Expand Up @@ -368,6 +434,17 @@ impl TryFrom<&BankOfAmericaRouterData<&types::PaymentsAuthorizeRouterData>>
match item.router_data.request.payment_method_data.clone() {
payments::PaymentMethodData::Card(ccard) => Self::try_from((item, ccard)),
payments::PaymentMethodData::Wallet(wallet_data) => match wallet_data {
payments::WalletData::ApplePay(_) => {
let payment_method_token = item.router_data.get_payment_method_token()?;
match payment_method_token {
types::PaymentMethodToken::ApplePayDecrypt(decrypt_data) => {
Self::try_from((item, decrypt_data))
}
types::PaymentMethodToken::Token(_) => {
Err(errors::ConnectorError::InvalidWalletToken)?
}
}
}
payments::WalletData::GooglePay(google_pay_data) => {
Self::try_from((item, google_pay_data))
}
Expand All @@ -378,7 +455,6 @@ impl TryFrom<&BankOfAmericaRouterData<&types::PaymentsAuthorizeRouterData>>
| payments::WalletData::KakaoPayRedirect(_)
| payments::WalletData::GoPayRedirect(_)
| payments::WalletData::GcashRedirect(_)
| payments::WalletData::ApplePay(_)
| payments::WalletData::ApplePayRedirect(_)
| payments::WalletData::ApplePayThirdPartySdk(_)
| payments::WalletData::DanaRedirect {}
Expand Down
29 changes: 28 additions & 1 deletion crates/router/src/connector/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
pii::PeekInterface,
types::{
self, api, storage::payment_attempt::PaymentAttemptExt, transformers::ForeignTryFrom,
PaymentsCancelData, ResponseId,
ApplePayPredecryptData, PaymentsCancelData, ResponseId,
},
utils::{OptionExt, ValueExt},
};
Expand Down Expand Up @@ -853,6 +853,33 @@ impl ApplePay for payments::ApplePayWalletData {
}
}

pub trait ApplePayDecrypt {
fn get_applepay_decrypted_expiration_month(&self) -> Result<Secret<String>, Error>;
fn get_applepay_decrypted_expiration_year_4_digit(&self) -> Result<Secret<String>, Error>;
}

impl ApplePayDecrypt for Box<ApplePayPredecryptData> {
fn get_applepay_decrypted_expiration_year_4_digit(&self) -> Result<Secret<String>, Error> {
deepanshu-iiitu marked this conversation as resolved.
Show resolved Hide resolved
Ok(Secret::new(format!(
"20{}",
self.application_expiration_date
.peek()
.get(0..2)
.ok_or(errors::ConnectorError::RequestEncodingFailed)?
)))
}

fn get_applepay_decrypted_expiration_month(&self) -> Result<Secret<String>, Error> {
deepanshu-iiitu marked this conversation as resolved.
Show resolved Hide resolved
Ok(Secret::new(
self.application_expiration_date
.peek()
.get(2..4)
.ok_or(errors::ConnectorError::RequestEncodingFailed)?
.to_owned(),
))
}
}

pub trait CryptoData {
fn get_pay_currency(&self) -> Result<String, Error>;
}
Expand Down
Loading