Skip to content

Commit 29dd366

Browse files
committed
Replace flat Plan product parameters with child object
1 parent 10f5821 commit 29dd366

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

285-
implicit val planInputDecoder: Decoder[PlanInput] = Decoder.forProduct10(
255+
implicit val planInputDecoder: Decoder[PlanInput] = Decoder.forProduct8(
286256
"id",
287257
"amount",
288258
"currency",
289259
"interval",
290-
"name",
260+
"product",
291261
"interval_count",
292262
"metadata",
293-
"nickname",
294-
"statement_descriptor",
295-
"trial_period_days" // TODO: does not belong here
263+
"nickname"
296264
)(PlanInput.apply)
297265

298-
implicit val planInputEncoder: Encoder[PlanInput] = Encoder.forProduct10(
266+
implicit val planInputEncoder: Encoder[PlanInput] = Encoder.forProduct8(
299267
"id",
300268
"amount",
301269
"currency",
302270
"interval",
303-
"name", // TODO: belongs on child
271+
"product",
304272
"interval_count",
305273
"metadata",
306-
"nickname",
307-
"statement_descriptor", // TODO: belongs on child
308-
"trial_period_days" // TODO: does not belong here
274+
"nickname"
309275
)(x => PlanInput.unapply(x).get)
310276

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

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

0 commit comments

Comments
 (0)