【问题标题】:Stripe Payment Library Issue with Scala and Play appScala 和 Play 应用程序的条纹支付库问题
【发布时间】:2016-08-23 18:48:59
【问题描述】:

我已经创建了 Scala 和 Play Framework 应用程序,并且我正在尝试使用 Stripe Payment Library。但是当我使用时,我遇到了以下错误:

编译错误

对象条带不是包 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 -

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

【问题讨论】:

  • 在我看来那个库已经过时了,最后一次更新是在 2012 年。这似乎是你想要使用的:mvnrepository.com/artifact/com.stripe/stripe-java
  • 要对此进行扩展,请查看 stripe.com/docs/libraries - 看起来他们没有维护单独的 scala 库(我不知道他们为什么会这样做)。
  • @childofsoong 我也用过,但遇到了同样的问题。所以使用 java sbt 插件我也有同样的问题。
  • 好吧,我建议发布一个关于该问题的问题,因为这是他们的网站列出的问题,实际上今年已更新,而不是四年前。这可能是语法错误。另外,我刚刚意识到 scala 在那个页面上,但它是一个“社区支持的库”(它的 github 存储库也同样过时)
  • 除了@childofsoong 所说的之外,您应该能够通过将libraryDependencies += "com.stripe" % "stripe-java" % "2.3.0" 添加到您的build.sbt 来包含Stripe 的Java API,然后从那里开始。

标签: scala sbt stripe-payments playframework-2.3


【解决方案1】:

嗯,根据错误消息,您遇到了不兼容版本的 Scala 的问题。基本上是@cchantep 在 cmets 上告诉你的。请记住,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
}

但是...请记住,此代码只是一个起点,它存在问题(它是阻塞的,每个实例)。

【讨论】:

  • 感谢@marcospereira,上述解决方案对我有用。非常感谢..!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 2018-07-13
  • 2017-03-02
  • 2021-08-02
  • 2017-09-14
相关资源
最近更新 更多