@@ -2,7 +2,28 @@ module StripeMock
2
2
module RequestHandlers
3
3
module ParamValidators
4
4
5
- def validate_create_plan_params ( params )
5
+ def already_exists_message ( obj_class )
6
+ "#{ obj_class . to_s . split ( "::" ) . last } already exists."
7
+ end
8
+
9
+ def not_found_message ( obj_class , obj_id )
10
+ "No such #{ obj_class . to_s . split ( "::" ) . last . downcase } : #{ obj_id } "
11
+ end
12
+
13
+ def missing_param_message ( attr_name )
14
+ "Missing required param: #{ attr_name } ."
15
+ end
16
+
17
+ def invalid_integer_message ( my_val )
18
+ "Invalid integer: #{ my_val } "
19
+ end
20
+
21
+ #
22
+ # ProductValidator
23
+ #
24
+
25
+
26
+ def validate_create_product_params ( params )
6
27
params [ :id ] = params [ :id ] . to_s
7
28
required_product_fields = @base_strategy . create_plan_params [ :product ] . keys
8
29
@@ -14,25 +35,91 @@ def validate_create_plan_params(params)
14
35
raise Stripe ::InvalidRequestError . new ( message , name ) if params [ :product ] [ name ] . nil?
15
36
end
16
37
17
- @base_strategy . create_plan_params . keys . each do |name |
38
+ @base_strategy . create_product_params . keys . reject { |k , _ | k == :id } . each do |k |
39
+ raise Stripe ::InvalidRequestError . new ( missing_param_message ( k ) , k ) if params [ k ] . nil?
40
+ end
41
+
42
+ if !%w[ good service ] . include? ( params [ :type ] )
43
+ raise Stripe ::InvalidRequestError . new ( "Invalid type: must be one of good or service" , :type )
44
+ end
45
+
46
+ if products [ params [ :id ] ]
47
+ raise Stripe ::InvalidRequestError . new ( already_exists_message ( Stripe ::Product ) , :id )
48
+ end
49
+ end
50
+
51
+ #
52
+ # PlanValidator
53
+ #
54
+
55
+ def missing_plan_amount_message
56
+ "Plans require an `amount` parameter to be set."
57
+ end
58
+
59
+ SUPPORTED_PLAN_INTERVALS = [ "month" , "year" , "week" , "day" ]
60
+
61
+ def invalid_plan_interval_message
62
+ "Invalid interval: must be one of month, year, week, or day"
63
+ end
64
+
65
+ SUPPORTED_CURRENCIES = [
66
+ "usd" , "aed" , "afn" , "all" , "amd" , "ang" , "aoa" , "ars" , "aud" , "awg" , "azn" , "bam" , "bbd" , "bdt" , "bgn" ,
67
+ "bif" , "bmd" , "bnd" , "bob" , "brl" , "bsd" , "bwp" , "bzd" , "cad" , "cdf" , "chf" , "clp" , "cny" , "cop" , "crc" ,
68
+ "cve" , "czk" , "djf" , "dkk" , "dop" , "dzd" , "egp" , "etb" , "eur" , "fjd" , "fkp" , "gbp" , "gel" , "gip" , "gmd" ,
69
+ "gnf" , "gtq" , "gyd" , "hkd" , "hnl" , "hrk" , "htg" , "huf" , "idr" , "ils" , "inr" , "isk" , "jmd" , "jpy" , "kes" ,
70
+ "kgs" , "khr" , "kmf" , "krw" , "kyd" , "kzt" , "lak" , "lbp" , "lkr" , "lrd" , "lsl" , "mad" , "mdl" , "mga" , "mkd" ,
71
+ "mmk" , "mnt" , "mop" , "mro" , "mur" , "mvr" , "mwk" , "mxn" , "myr" , "mzn" , "nad" , "ngn" , "nio" , "nok" , "npr" ,
72
+ "nzd" , "pab" , "pen" , "pgk" , "php" , "pkr" , "pln" , "pyg" , "qar" , "ron" , "rsd" , "rub" , "rwf" , "sar" , "sbd" ,
73
+ "scr" , "sek" , "sgd" , "shp" , "sll" , "sos" , "srd" , "std" , "szl" , "thb" , "tjs" , "top" , "try" , "ttd" , "twd" ,
74
+ "tzs" , "uah" , "ugx" , "uyu" , "uzs" , "vnd" , "vuv" , "wst" , "xaf" , "xcd" , "xof" , "xpf" , "yer" , "zar" , "zmw" ,
75
+ "eek" , "lvl" , "svc" , "vef"
76
+ ]
77
+
78
+ def invalid_currency_message ( my_val )
79
+ "Invalid currency: #{ my_val . downcase } . Stripe currently supports these currencies: #{ SUPPORTED_CURRENCIES . join ( ", " ) } "
80
+ end
81
+
82
+ def validate_create_plan_params ( params )
83
+ plan_id = params [ :id ] . to_s
84
+ product_id = params [ :product ]
85
+
86
+ @base_strategy . create_plan_params . keys . each do |attr_name |
18
87
message =
19
- if name == :amount
20
- "Plans require an `#{ name } ` parameter to be set."
21
- elsif name == :product
88
+ if attr_name == :amount
89
+ "Plans require an `#{ attr_name } ` parameter to be set."
90
+ elsif attr_name == :product
22
91
"Missing required param: name."
23
92
else
24
- "Missing required param: #{ name } ."
93
+ "Missing required param: #{ attr_name } ."
25
94
end
26
- raise Stripe ::InvalidRequestError . new ( message , name ) if params [ name ] . nil?
95
+ raise Stripe ::InvalidRequestError . new ( message , attr_name ) if params [ attr_name ] . nil?
27
96
end
28
97
29
- if plans [ params [ :id ] ]
30
- raise Stripe ::InvalidRequestError . new ( "Plan already exists." , :id )
98
+ if plans [ plan_id ]
99
+ message = already_exists_message ( Stripe ::Plan )
100
+ raise Stripe ::InvalidRequestError . new ( message , :id )
101
+ end
102
+
103
+ unless products [ product_id ]
104
+ message = not_found_message ( Stripe ::Product , product_id )
105
+ raise Stripe ::InvalidRequestError . new ( message , :product )
106
+ end
107
+
108
+ unless SUPPORTED_PLAN_INTERVALS . include? ( params [ :interval ] )
109
+ message = invalid_plan_interval_message
110
+ raise Stripe ::InvalidRequestError . new ( message , :interval )
111
+ end
112
+
113
+ unless SUPPORTED_CURRENCIES . include? ( params [ :currency ] )
114
+ message = invalid_currency_message ( params [ :currency ] )
115
+ raise Stripe ::InvalidRequestError . new ( message , :currency )
31
116
end
32
117
33
118
unless params [ :amount ] . integer?
34
- raise Stripe ::InvalidRequestError . new ( "Invalid integer: #{ params [ :amount ] } " , :amount )
119
+ message = invalid_integer_message ( params [ :amount ] )
120
+ raise Stripe ::InvalidRequestError . new ( message , :amount )
35
121
end
122
+
36
123
end
37
124
38
125
def require_param ( param_name )
0 commit comments