Scala 和 Play 应用的 Stripe 支付库问题

Stripe Payment Library Issue with Scala and Play app

我已经创建了 Scala 和 Play Framework 应用程序,我正在尝试使用它来使用 Stripe 支付库。但是当我使用时出现以下错误:

Compilation error

object stripe is not a member of package com

我使用的版本如下,

  1. Scala 版本:2.11.6
  2. SBT 版本:0.13.8

我在 build.sbt 中使用了 Stripe SBT 依赖项,如下所示,

"com.stripe"    % "stripe-scala_2.9.1"      % "1.1.2"

我的 build.sbt 文件如下:

name := """My App Name"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.11.6"

libraryDependencies ++= Seq(
  cache,
  ws,
  filters,
  "com.typesafe.play"                         %% "play-mailer"            % "3.0.1",
  "com.typesafe.play"                         %% "play-slick"             % "1.1.1",
  "com.typesafe.play"                         %% "play-slick-evolutions"  % "1.1.1",
  "postgresql"                                %  "postgresql"             % "9.1-901.jdbc4",
  "commons-lang"                              % "commons-lang"            % "2.6",
  "com.stripe"                                % "stripe-scala_2.9.1"      % "1.1.2"
)

resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator

我的控制器出现异常:

package controllers

import com.stripe
import com.stripe.{Charge, Customer}
import play.api.mvc._
/**
  * Created by Nishan Patel on 28-04-2016.
  */
class PaymentController extends Controller {

  def getPayment = Action { request =>
    val formData = (request.body).asFormUrlEncoded

    val stripe.apiKey = "sk_test_xxxxxxxxxxxxx";
    val token = formData.get("stripeToken")(0)

    val customerParams = Map("source" -> token, "description" -> "Customer Rollback Renew req.")

    val customer = Customer.create(customerParams)

    val chargeParams = Map("amount" -> 2000, "currency" -> "usd", "customer" -> customer.id)

    Charge.create(chargeParams);

    Ok
  }

}

[error] missing or invalid dependency detected while loading class file 'package
.class'.
[error] Could not access type ScalaObject in package scala,
[error] because it (or its dependencies) are missing. Check your build definitio
n for
[error] missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to s
ee the problematic classpath.)
[error] A full rebuild may help if 'package.class' was compiled against an incom
patible version of scala.
[error] missing or invalid dependency detected while loading class file 'APIReso
urce.class'.
[error] Could not access type ScalaObject in package scala,
[error] because it (or its dependencies) are missing. Check your build definitio
n for
[error] missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to s
ee the problematic classpath.)
[error] A full rebuild may help if 'APIResource.class' was compiled against an i
ncompatible version of scala.
[error] missing or invalid dependency detected while loading class file 'Custome
r.class'.
[error] Could not access type ScalaObject in package scala,
[error] because it (or its dependencies) are missing. Check your build definitio
n for
[error] missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to s
ee the problematic classpath.)
[error] A full rebuild may help if 'Customer.class' was compiled against an inco
mpatible version of scala.
[error] missing or invalid dependency detected while loading class file 'Charge.
class'.
[error] Could not access type ScalaObject in package scala,
[error] because it (or its dependencies) are missing. Check your build definitio
n for
[error] missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to s
ee the problematic classpath.)
[error] A full rebuild may help if 'Charge.class' was compiled against an incomp
atible version of scala.
[error] E:\Nishan\Confidential\My Workspace\MetaForce\app\controllers\PaymentCon
troller.scala:14: stable identifier required, but com.stripe.`package`.apiKey fo
und.
[error]     val stripe.apiKey = "sk_test_xxxxxx";
[error]                ^
[error] 5 errors found
[error] (compile:compileIncremental) Compilation failed
[error] application -

一切看起来都不错,但不知道为什么我会遇到这个编译问题。

嗯,根据错误消息,您遇到了不兼容的 Scala 版本问题。基本上@cchantep 在评论中告诉你的。请记住,Scala 2.9.x 与 Scala 2.11.x 二进制不兼容,您只是在混合两个版本时提出问题。

删除 build.sbt 文件的以下行:

"com.stripe" % "stripe-scala_2.9.1" % "1.1.2"

并添加Stripe提供的更新和维护的客户端版本:

"com.stripe" % "stripe-java" % "2.4.0"

之后,您需要重写代码才能使用这个新客户端。这是一个基于 Stripe docs 以及您的代码的示例:

def getPayment = Action { request =>
  val requestOptions = new RequestOptionsBuilder()
    .setApiKey("sk_test_xxxxxxxxxxxxx")
    .build()

  val formData = (request.body).asFormUrlEncoded
  val token = formData.get("stripeToken")(0)
  val customerParams = Map("source" -> token, "description" -> "Customer Rollback Renew req.")

  val customer = Customer.create(customerParams)

  val chargeParams = Map("amount" -> 2000, "currency" -> "usd", "customer" -> customer.id)

  Charge.create(chargeParams, requestOptions);

  Ok
}

但是...请记住,这段代码只是一个起点,它有问题(每个实例都是阻塞的)。