Skip to content

Commit c39beb2

Browse files
sahkalkashif-mKashif
authored
feat(router): Custom payment link config for payment create (#2741)
Co-authored-by: Kashif <[email protected]> Co-authored-by: Kashif <[email protected]> Co-authored-by: Sahkal Poddar <[email protected]>
1 parent aea390a commit c39beb2

File tree

9 files changed

+51
-14
lines changed

9 files changed

+51
-14
lines changed

crates/api_models/src/admin.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,11 @@ pub struct PrimaryBusinessDetails {
455455
#[derive(Clone, Debug, Deserialize, ToSchema, Serialize, PartialEq)]
456456
#[serde(deny_unknown_fields)]
457457
pub struct PaymentLinkConfig {
458+
#[schema(
459+
max_length = 255,
460+
max_length = 255,
461+
example = "https://i.imgur.com/RfxPFQo.png"
462+
)]
458463
pub merchant_logo: Option<String>,
459464
pub color_scheme: Option<PaymentLinkColorSchema>,
460465
}

crates/api_models/src/payments.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3150,6 +3150,8 @@ pub struct PaymentLinkObject {
31503150
#[serde(default, with = "common_utils::custom_serde::iso8601::option")]
31513151
pub link_expiry: Option<PrimitiveDateTime>,
31523152
pub merchant_custom_domain_name: Option<String>,
3153+
#[schema(value_type = PaymentLinkConfig)]
3154+
pub payment_link_config: Option<admin::PaymentLinkConfig>,
31533155
/// Custom merchant name for payment link
31543156
pub custom_merchant_name: Option<String>,
31553157
}

crates/diesel_models/src/payment_link.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use time::PrimitiveDateTime;
44

55
use crate::{enums as storage_enums, schema::payment_link};
66

7-
#[derive(Clone, Debug, Eq, PartialEq, Identifiable, Queryable, Serialize, Deserialize)]
7+
#[derive(Clone, Debug, Identifiable, Queryable, Serialize, Deserialize)]
88
#[diesel(table_name = payment_link)]
99
#[diesel(primary_key(payment_link_id))]
1010
pub struct PaymentLink {
@@ -21,7 +21,9 @@ pub struct PaymentLink {
2121
#[serde(default, with = "common_utils::custom_serde::iso8601::option")]
2222
pub fulfilment_time: Option<PrimitiveDateTime>,
2323
pub custom_merchant_name: Option<String>,
24+
pub payment_link_config: Option<serde_json::Value>,
2425
}
26+
2527
#[derive(
2628
Clone,
2729
Debug,
@@ -48,4 +50,5 @@ pub struct PaymentLinkNew {
4850
#[serde(default, with = "common_utils::custom_serde::iso8601::option")]
4951
pub fulfilment_time: Option<PrimitiveDateTime>,
5052
pub custom_merchant_name: Option<String>,
53+
pub payment_link_config: Option<serde_json::Value>,
5154
}

crates/diesel_models/src/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ diesel::table! {
668668
fulfilment_time -> Nullable<Timestamp>,
669669
#[max_length = 64]
670670
custom_merchant_name -> Nullable<Varchar>,
671+
payment_link_config -> Nullable<Jsonb>,
671672
}
672673
}
673674

crates/router/src/core/payment_link.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use common_utils::{
33
consts::{
44
DEFAULT_BACKGROUND_COLOR, DEFAULT_MERCHANT_LOGO, DEFAULT_PRODUCT_IMG, DEFAULT_SDK_THEME,
55
},
6-
ext_traits::ValueExt,
6+
ext_traits::{OptionExt, ValueExt},
77
};
88
use error_stack::{IntoReport, ResultExt};
99
use masking::{PeekInterface, Secret};
@@ -15,7 +15,6 @@ use crate::{
1515
routes::AppState,
1616
services,
1717
types::{domain, storage::enums as storage_enums, transformers::ForeignFrom},
18-
utils::OptionExt,
1918
};
2019

2120
pub async fn retrieve_payment_link(
@@ -71,16 +70,11 @@ pub async fn intiate_payment_link_flow(
7170
.await
7271
.to_not_found_response(errors::ApiErrorResponse::PaymentLinkNotFound)?;
7372

74-
let payment_link_config = merchant_account
75-
.payment_link_config
76-
.map(|pl_config| {
77-
serde_json::from_value::<admin_types::PaymentLinkConfig>(pl_config)
78-
.into_report()
79-
.change_context(errors::ApiErrorResponse::InvalidDataValue {
80-
field_name: "payment_link_config",
81-
})
82-
})
83-
.transpose()?;
73+
let payment_link_config = if let Some(pl_config) = payment_link.payment_link_config.clone() {
74+
extract_payment_link_config(Some(pl_config))?
75+
} else {
76+
extract_payment_link_config(merchant_account.payment_link_config.clone())?
77+
};
8478

8579
let order_details = validate_order_details(payment_intent.order_details)?;
8680

@@ -235,3 +229,17 @@ fn validate_order_details(
235229
});
236230
Ok(updated_order_details)
237231
}
232+
233+
fn extract_payment_link_config(
234+
pl_config: Option<serde_json::Value>,
235+
) -> Result<Option<admin_types::PaymentLinkConfig>, error_stack::Report<errors::ApiErrorResponse>> {
236+
pl_config
237+
.map(|config| {
238+
serde_json::from_value::<admin_types::PaymentLinkConfig>(config)
239+
.into_report()
240+
.change_context(errors::ApiErrorResponse::InvalidDataValue {
241+
field_name: "payment_link_config",
242+
})
243+
})
244+
.transpose()
245+
}

crates/router/src/core/payments/operations/payment_create.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,11 @@ async fn create_payment_link(
800800
merchant_id.clone(),
801801
payment_id.clone()
802802
);
803+
804+
let payment_link_config = payment_link_object.payment_link_config.map(|pl_config|{
805+
common_utils::ext_traits::Encode::<api_models::admin::PaymentLinkConfig>::encode_to_value(&pl_config)
806+
}).transpose().change_context(errors::ApiErrorResponse::InvalidDataValue { field_name: "payment_link_config" })?;
807+
803808
let payment_link_req = storage::PaymentLinkNew {
804809
payment_link_id: payment_link_id.clone(),
805810
payment_id: payment_id.clone(),
@@ -810,6 +815,7 @@ async fn create_payment_link(
810815
created_at,
811816
last_modified_at,
812817
fulfilment_time: payment_link_object.link_expiry,
818+
payment_link_config,
813819
custom_merchant_name: payment_link_object.custom_merchant_name,
814820
};
815821
let payment_link_db = db
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- This file should undo anything in `up.sql`
2+
ALTER TABLE payment_link DROP COLUMN IF EXISTS payment_link_config;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Your SQL goes here
2+
ALTER TABLE payment_link ADD COLUMN IF NOT EXISTS payment_link_config JSONB NULL;

openapi/openapi_spec.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8221,7 +8221,9 @@
82218221
"properties": {
82228222
"merchant_logo": {
82238223
"type": "string",
8224-
"nullable": true
8224+
"example": "https://i.imgur.com/RfxPFQo.png",
8225+
"nullable": true,
8226+
"maxLength": 255
82258227
},
82268228
"color_scheme": {
82278229
"allOf": [
@@ -8250,6 +8252,9 @@
82508252
},
82518253
"PaymentLinkObject": {
82528254
"type": "object",
8255+
"required": [
8256+
"payment_link_config"
8257+
],
82538258
"properties": {
82548259
"link_expiry": {
82558260
"type": "string",
@@ -8260,6 +8265,9 @@
82608265
"type": "string",
82618266
"nullable": true
82628267
},
8268+
"payment_link_config": {
8269+
"$ref": "#/components/schemas/PaymentLinkConfig"
8270+
},
82638271
"custom_merchant_name": {
82648272
"type": "string",
82658273
"description": "Custom merchant name for payment link",

0 commit comments

Comments
 (0)