使用 Grails Stripe 插件

Using the Grails Stripe plugin

我在我的应用程序中使用 Stripe grails plugin,但出现以下错误:

Class:groovy.lang.MissingPropertyExceptionMessage:No such property: Stripe for class: com.myApp.app.SubscriptionRequestController

这是我的操作:

def charge(String stripeToken, Double amount) {
    Stripe.apiKey = grailsApplication.config.grails.plugins.stripe.secretKey
    def amountInCents = (amount * 100) as Integer
    def chargeParams = [
        'amount': amountInCents,
        'currency': 'usd',
        'card': stripeToken,
        'description': 'customer@sample.org'
    ]

    def status
    try {
        Charge.create(chargeParams)
        status = 'Your purchase was successful.'
    } catch(CardException) {
        status = 'There was an error processing your credit card.'
    }

    redirect(action: "confirmation", params: [msg: status])
    return
}

我也收到以下错误,因为我安装了插件,当我删除它时我没有看到它,它发生在尝试访问或刷新任何视图时:

java.lang.RuntimeException: It looks like you are missing some calls to the r:layoutResources tag. After rendering your page the following have not been rendered: [head] 

就目前而言,您可能没有导入 com.stripe.Stripe class 这就是您收到该特定消息的原因。

您无需尝试手动将密钥分配给 Stripe class。只需在您的 Config.groovy 中定义 grails.plugins.stripe.secretKey ,插件将处理其余部分,您可以 see in the plugin source.

试试这个:

在您的主布局文件中,例如:main.gsp,在 body 和 head 中添加两个。此外,手动加载 stripe-v2。不需要在 web-app/js 中添加它,因为插件本身已经有了它。

<html>
    <head>
        <g:javascript src="stripe-v2.js" />
        <r:layoutResources/>
    </head>
    <body>
        <g:layoutBody/>
        <r:layoutResources/>
    </body>
</html>

在config.groovy中添加:

grails.resources.modules = {
    stripe {
        dependsOn 'jquery'
        resource url:'/js/stripe-v2.js', disposition: 'head', exclude:'minify'
    }
}

示例 gsp(取自文档)但我添加了 payment-errors 作为来源使用它:

<stripe:script formName="payment-form"/>
<g:form controller="checkout" action="charge" method="POST" name="payment-form">
    <div class="payment-errors"></div>
    <div class="form-row">
        <label>Amount (USD)</label>
        <input type="text" size="20" autocomplete="off" id="amount" name="amount"/>
    </div>

    <stripe:creditCardInputs cssClass="form-row"/>

    <button type="submit">Submit Payment</button>
</g:form>

我认为插件本身不支持当前的 Grails Asset 实现Pipeline.I认为它与 grails 2.4.3 及更高版本不兼容。