Skip to content

Commit fea9039

Browse files
committed
Replace flat Plan product parameters with child object
1 parent a7775e7 commit fea9039

File tree

1 file changed

+32
-55
lines changed

1 file changed

+32
-55
lines changed

src/main/scala/org/mdedetrich/stripe/v1/Plans.scala

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -223,88 +223,54 @@ object Plans extends LazyLogging {
223223
* @param currency 3-letter ISO code for currency.
224224
* @param interval Specifies billing frequency. Either [[Interval.Day]],
225225
* [[Interval.Week]], [[Interval.Month]] or [[Interval.Year]].
226-
*
227226
* @param intervalCount The number of intervals between each subscription
228227
* billing. For example, [[interval]]=[[Interval.Month]]
229228
* and [[intervalCount]]=3 bills every 3 months. Maximum of
230229
* one year interval allowed (1 year, 12 months, or 52 weeks).
230+
* @param product The product whose pricing the created plan will represent.
231+
* This can either be the ID of an existing product, or a dictionary
232+
* containing fields used to create a service product.
231233
* @param metadata A set of key/value pairs that you can attach to a plan object.
232234
* It can be useful for storing additional information about
233235
* the plan in a structured format. This will be unset if you
234236
* POST an empty value.
235237
* @param nickname A brief description of the plan, hidden from customers.
236-
*
237-
* The following parameters belong on the [[product]] child object:
238-
*
239-
* @param name Name of the plan, to be displayed on invoices and in
240-
* the web interface.
241-
* @param statementDescriptor An arbitrary string to be displayed on your
242-
* customer’s credit card statement. This may be up to
243-
* 22 characters. As an example, if your website is
244-
* RunClub and the item you’re charging for is your
245-
* Silver Plan, you may want to specify a [[statementDescriptor]]
246-
* of RunClub Silver Plan. The statement description may not include `<>"'`
247-
* characters, and will appear on your customer’s statement in
248-
* capital letters. Non-ASCII characters are automatically stripped.
249-
* While most banks display this information consistently,
250-
* some may display it incorrectly or not at all.
238+
*
251239
* @param trialPeriodDays Specifies a trial period in (an integer number of)
252240
* days. If you include a trial period, the customer
253241
* won’t be billed for the first time until the trial period ends.
254242
* If the customer cancels before the trial period is over,
255243
* she’ll never be billed at all.
256-
* @throws StatementDescriptorTooLong - If [[statementDescriptor]] is longer than 22 characters
257-
* @throws StatementDescriptorInvalidCharacter - If [[statementDescriptor]] has an invalid character
258244
*/
259245
case class PlanInput(id: String,
260246
amount: BigDecimal,
261247
currency: Currency,
262248
interval: Interval,
263-
name: String,
249+
product: Product,
264250
intervalCount: Option[Long] = None,
265251
metadata: Option[Map[String, String]] = None,
266-
nickname: Option[String] = None,
267-
statementDescriptor: Option[String] = None,
268-
trialPeriodDays: Option[Long] = None) {
269-
statementDescriptor match {
270-
case Some(sD) if sD.length > 22 =>
271-
throw StatementDescriptorTooLong(sD.length)
272-
case Some(sD) if sD.contains("<") =>
273-
throw StatementDescriptorInvalidCharacter("<")
274-
case Some(sD) if sD.contains(">") =>
275-
throw StatementDescriptorInvalidCharacter(">")
276-
case Some(sD) if sD.contains("\"") =>
277-
throw StatementDescriptorInvalidCharacter("\"")
278-
case Some(sD) if sD.contains("\'") =>
279-
throw StatementDescriptorInvalidCharacter("\'")
280-
case _ =>
281-
}
282-
}
252+
nickname: Option[String] = None)
283253

284-
implicit val planInputDecoder: Decoder[PlanInput] = Decoder.forProduct10(
254+
implicit val planInputDecoder: Decoder[PlanInput] = Decoder.forProduct8(
285255
"id",
286256
"amount",
287257
"currency",
288258
"interval",
289-
"name",
259+
"product",
290260
"interval_count",
291261
"metadata",
292-
"nickname",
293-
"statement_descriptor",
294-
"trial_period_days" // TODO: does not belong here
262+
"nickname"
295263
)(PlanInput.apply)
296264

297-
implicit val planInputEncoder: Encoder[PlanInput] = Encoder.forProduct10(
265+
implicit val planInputEncoder: Encoder[PlanInput] = Encoder.forProduct8(
298266
"id",
299267
"amount",
300268
"currency",
301269
"interval",
302-
"name", // TODO: belongs on child
270+
"product",
303271
"interval_count",
304272
"metadata",
305-
"nickname",
306-
"statement_descriptor", // TODO: belongs on child
307-
"trial_period_days" // TODO: does not belong here
273+
"nickname"
308274
)(x => PlanInput.unapply(x).get)
309275

310276
def create(planInput: PlanInput)(idempotencyKey: Option[IdempotencyKey] = None)(
@@ -315,15 +281,26 @@ object Plans extends LazyLogging {
315281
executionContext: ExecutionContext): Future[Try[Plan]] = {
316282
val postFormParameters = PostParams.flatten(
317283
Map(
318-
"id" -> Option(planInput.id.toString),
319-
"amount" -> Option(planInput.amount.toString()),
320-
"currency" -> Option(planInput.currency.iso.toLowerCase),
321-
"interval" -> Option(planInput.interval.id.toString),
322-
"name" -> Option(planInput.name),
323-
"interval_count" -> planInput.intervalCount.map(_.toString),
324-
"statement_descriptor" -> planInput.statementDescriptor,
325-
"trial_period_days" -> planInput.trialPeriodDays.map(_.toString)
326-
)) ++ mapToPostParams(planInput.metadata, "metadata")
284+
"id" -> Option(planInput.id.toString),
285+
"amount" -> Option(planInput.amount.toString()),
286+
"currency" -> Option(planInput.currency.iso.toLowerCase),
287+
"interval" -> Option(planInput.interval.id.toString),
288+
"interval_count" -> planInput.intervalCount.map(_.toString),
289+
"nickname" -> planInput.nickname
290+
)) ++ mapToPostParams(planInput.metadata, "metadata") ++ {
291+
planInput.product match {
292+
case service: Product.ServiceProduct =>
293+
val params = PostParams.flatten(
294+
Map(
295+
"id" -> service.id,
296+
"name" -> Option(service.name),
297+
"statement_descriptor" -> service.statementDescriptor
298+
)
299+
)
300+
mapToPostParams(Option(params ++ mapToPostParams(service.metadata, "metadata")), "product")
301+
case Product.ProductId(id) => Map("product" -> id)
302+
}
303+
}
327304

328305
logger.debug(s"Generated POST form parameters is $postFormParameters")
329306

0 commit comments

Comments
 (0)