【问题标题】:Grails Stripe plugin errorGrails Stripe 插件错误
【发布时间】:2016-12-19 14:43:32
【问题描述】:

我正在使用 Stripe plugin for Grails,Grails 版本 2.5.1 我无法进行任何成功的交易我总是得到There was an error processing your credit card.,如控制器所示,我注意到Charge 方法没有像截图 中显示的那样定义@

我尝试导入com.stripe.Stripe,但我得到unable to resolve class com.stripe.Stripe

下面是动作:

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
}

【问题讨论】:

  • 您不需要导入 com.stripe.Stripe 来设置 api 密钥,因为插件会处理这个问题。我将首先检查您在 CardException 中收到的消息。它可能会提供有关交易失败原因的线索。

标签: grails stripe-payments grails-plugin


【解决方案1】:

问题是关于 Grails 2.5.x。该插件不适用于 Grails 3.x.x。我让它工作并发布了一个在线教程。有一个完整的、可运行的 Grails 3.2.3 应用程序可供下载。它还实现了一个购物车。页面为:http://www.databaseapplications.com.au/stripe_payments.jsp

【讨论】:

    【解决方案2】:

    试试这个,

    在构建配置中添加:

    plugins {
     ...
     compile "org.grails.plugins:stripe:2.8"
     ...
    }
    

    在您的控制器中:

    package stripesample
    
    import com.stripe.model.Charge
    import com.stripe.exception.CardException;
    
    class CheckoutController {
    
        def index() {}
    
        def charge(String stripeToken, Double amount) {
            def amountInCents = (amount * 100) as Integer
    
            def chargeParams = [
                'amount': amountInCents, 
                'currency': 'usd', 
                'card': stripeToken, 
                'description': 'customer@sample.org'
            ]
    
            def status
            Charge chargeStatus
            try {
                chargeStatus = Charge.create(chargeParams)
                println chargeStatus
                status = 'Your purchase was successful.'
            } catch(CardException) {
                println status
                status = 'There was an error processing your credit card.'
            }
    
            render view: "confirmation", model:[msg: status,chargeObject:chargeStatus]
            return
        }
    }
    

    在您的主布局文件中:

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

    在您的信用卡表单中查看:

    <!DOCTYPE html>
    <html>
        <head>
            <meta name="layout" content="main"/>
    
        </head>
        <body>
            <h3>Checkout</h3>
    
                   <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>
    
            </div>
        </body>
    </html>
    

    在我的情况下,在同一个控制器文件夹中创建另一个视图 checkout/confirmation.gsp

    <!DOCTYPE html>
    <html>
        <head>
    
        </head>
        <body>
    
    
           <h3>Checkout</h3>
           <p>${msg}</p>
    
            <p>Data: </p>
            <p>${chargeObject}</p>
    
        </body>
    </html>
    

    运行grails clean 接着, 运行grails run-app

    如果您需要测试示例应用,可以在此处克隆我的应用:Sample App

    【讨论】:

    • 您的示例运行良好,但是当我尝试在我的控制器中导入import com.stripe.model.Charge , import com.stripe.exception.CardException 时,我得到Groovy:unable to resolve class com.stripe.model.ChargeGroovy:unable to resolve class com.stripe.exception.CardException 任何解决此问题的线索,我也重新安装了插件,但仍然出现错误.
    【解决方案3】:

    我在eclipse中做了“刷新依赖”,一切正常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-25
      • 2015-02-03
      • 2013-01-28
      • 2014-01-07
      相关资源
      最近更新 更多