Grails Stripe plugin error

2019-07-15 09:08发布

问题:

i'm using Stripe plugin for Grails ,Grails version 2.5.1 i can't make any successful transaction i always get There was an error processing your credit card. as shown in the controller , i noticed that Charge method is not defined as shown in the screenshot

i tried to import com.stripe.Stripe but i'm getting unable to resolve class com.stripe.Stripe.

Here is the action:

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
}

回答1:

Try this,

In build config add:

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

In your controller:

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
    }
}

In your main layout file:

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

In your credit card form View:

<!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>

Create another view in the same controller folder in my case checkout/confirmation.gsp

<!DOCTYPE html>
<html>
    <head>

    </head>
    <body>


       <h3>Checkout</h3>
       <p>${msg}</p>

        <p>Data: </p>
        <p>${chargeObject}</p>

    </body>
</html>

Run grails clean and then, Run grails run-app

If you need to test a sample app you can clone my app here: Sample App



回答2:

The question was about Grails 2.5.x. The plug-in is not available for Grails 3.x.x. I got it working and posted an online tutorial. There is a full, working Grails 3.2.3 application available for download. It also implements a shopping cart. The page is: http://www.databaseapplications.com.au/stripe_payments.jsp



回答3:

i did "refresh dependencies" in eclipse , and all worked fine