Chargify API 优惠券创建
我目前正在开展一个项目并面临任务问题。我正在尝试随机生成一个 6 位优惠券号码并通过 API 将其发布到 Chargify 帐户。如果优惠券创建成功,我希望通过电子邮件将相同的优惠券代码发送给客户。
根据 chargify 文档,这是我应该如何从我的应用程序发送所有详细信息到 chargify:
{"subscription":{
"product_handle":"[@product.handle]",
"customer_attributes":{
"first_name":"Joe",
"last_name":"Blow",
"email":"[email protected]"
},
"credit_card_attributes":{
"full_number":"1",
"expiration_month":"10",
"expiration_year":"2020"
},
"coupon_code":"6 digit random code"
}}
"""
https://[@subdomain].chargify.com/subscriptions.json。
我可以通过这种方法创建 6 位随机数字代码:
rand(999999).to_s.center(6, rand(9).to_s).
但这似乎对我不起作用。任何建议将不胜感激。
谢谢
I am currently working on a project and facing a problem with a task. I am trying to randomly generate a 6 digit coupon number and post it to chargify account via there API. If the coupon creation is successful I want the same coupon code to be send to the customer through Email.
As per chargify documentation this is how I should send all the details to chargify from my application :
{"subscription":{
"product_handle":"[@product.handle]",
"customer_attributes":{
"first_name":"Joe",
"last_name":"Blow",
"email":"[email protected]"
},
"credit_card_attributes":{
"full_number":"1",
"expiration_month":"10",
"expiration_year":"2020"
},
"coupon_code":"6 digit random code"
}}
"""
https://[@subdomain].chargify.com/subscriptions.json.
I am able to create a 6 digit random numerical code by this method :
rand(999999).to_s.center(6, rand(9).to_s).
However this does not seem to be working for me. Any suggestions would be highly appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是我们的技术或开发人员,但我 99% 确定您只能在该 API 调用中指定先前定义的优惠券代码。您必须在 Chargify 管理 Web 界面中定义优惠券代码。在上面的 API 调用中,您可以将优惠券应用于订阅,但假设您已经在管理界面中定义了该优惠券代码。
我们将来会添加该功能,但我没有具体的日期。
对此感到抱歉。
——兰斯·沃利
--- 充电
I'm not part of our tech or dev staff, but I'm 99% sure you can only specify a previously-defined coupon code in that API call. You must define coupon codes in the Chargify admin web interface. In the API call above, you can apply a coupon to the subscription, but the assumption is that you already defined that coupon code in the admin interface.
We will add that capability in the future, but I don't have a specific date for you.
Sorry about that.
--- Lance Walley
--- Chargify
我不确定您想通过调用
center
来做什么。最明智的做法是将优惠券代码清零。这样就可以了:这将生成诸如“664001”和“061532”之类的代码。
请注意,您需要
rand(1000000)
而不是rand(999999)
。这是因为 rand 为您提供 0 到比参数小 1 之间的随机整数。rand(999999)
只会给你最大 999998 的随机数。上面的代码违反了 DRY(不要重复自己):“06”和“1000000”都依赖于优惠券代码的长度。解决方法如下:
虽然更长,但如果优惠券代码长度发生变化,现在只需更改一件事。用命名常量替换“幻数”也有助于代码传达其意图。
I'm not sure what you're trying to do with your call to
center
. The most sensible thing to do would be to zero-fill the coupon code. This would do it:This will generate codes such as "664001" and "061532".
Note that you want
rand(1000000)
rather thanrand(999999)
. That's because rand gives you random integers between 0 and one less than the argument.rand(999999)
will only give you random numbers up to 999998.There's a violation of DRY (Don't Repeat Yourself) in the above code: Both the "06" and the "1000000" depend upon the length of the coupon code. Here's a fix for that:
Although longer, there's now only one thing to change if the coupon code length should change. The replacement of "magic numbers" with a named constant also helps the code to communicate its intent.