Paddle 要求在 HTML 中之前插入这两个脚本</body>:
<script src="https://cdn.paddle.com/paddle/paddle.js"></script>
<script type="text/javascript">
Paddle.Setup({ vendor: 1234567 });
</script>
Run Code Online (Sandbox Code Playgroud)
但是 NextJS 没有 HTML 文件,当我插入返回函数时,{ vendor: 1234567, debug: true }部分会产生'}' expected错误。
有谁知道如何解决这个问题?
kac*_*har 14
您可以使用ScriptNext.Js提供的组件
https://nextjs.org/docs/basic-features/script
import Script from 'next/script'
<Script
key="init-paddle"
src="https://cdn.paddle.com/paddle/paddle.js"
onLoad={() => {
console.log('On paddle load')
if (PADDLE_SANDBOX) {
window.Paddle.Environment.set('sandbox')
}
window.Paddle.Setup({
vendor: 12345,
eventCallback: function (data) {
// The data.event will specify the event type
if (data.event === 'Checkout.Loaded') {
console.log(data.eventData) // Data specifics on the event
} else if (data.event === 'Checkout.Complete') {
console.log(data.eventData) // Data specifics on the event
} else if (data.event === 'Checkout.Close') {
console.log(data.eventData) // Data specifics on the event
}
},
})
}}
/>
Run Code Online (Sandbox Code Playgroud)
如果您使用打字稿并且想要输入 paddle 集成,您可以使用:
(不详尽,但总比没有好)
/// paddle.d.ts
export declare global {
interface Customer {
total: number
total_tax: number
currency: string
}
type PaddleEvent =
| 'Checkout.Loaded'
| 'Checkout.Close'
| 'Checkout.Complete'
| 'Checkout.User.Subscribed'
| 'Checkout.Quantity.Change'
| 'Checkout.Login'
| 'Checkout.Logout'
| 'Checkout.PaymentMethodSelected'
| 'Checkout.Coupon.Add'
| 'Checkout.Coupon.Submit'
| 'Checkout.Coupon.Cancel'
| 'Checkout.Coupon.Applied'
| 'Checkout.Coupon.Remove'
| 'Checkout.Error'
| 'Checkout.Location.Submit'
| 'Checkout.Language.Change'
| 'Checkout.Vat.Add'
| 'Checkout.Vat.Cancel'
| 'Checkout.Vat.Submit'
| 'Checkout.Vat.Applied'
| 'Checkout.Vat.Remove'
| 'Checkout.WireTransfer.Complete'
| 'Checkout.PaymentComplete'
| 'Checkout.PaymentMethodChange'
| 'Checkout.WireTransfer.PaymentMethodChange'
interface Window {
Paddle: {
Checkout: {
open: (options: {
product: string | number
title?: string
message?: string
coupon?: string
email?: string
marketingConsent?: '0' | '1'
country?: string
postcode?: string
allowQuantity?: boolean
quantity?: number
disableLogout?: boolean
locale?: string
passthrough?: string
referring_domain?: string
success?: string
successCallback?: string
closeCallback?: string
loadCallback?: string
upsell?: string | number
upsellTitle?: string
upsellText?: string
upsellAction?: string
upsellCoupon?: string
upsellPassthrough?: string
override?: string
displayModeTheme?: string
// Inline checkout
method?: string
frameTarget?: string
frameInitialHeight?: number
frameStyle?: string
}) => void
}
Environment: {
set: (env: string) => void
}
Setup: (options: {
vendor: number
eventCallback: (data: {
event: PaddleEvent
eventData: {
payment_method?: string
available_payment_methods?: string
available_payment_methods_count?: number
checkout: {
recurring_prices: {
customer: Customer
interval: {
type: string
length: number
}
}
prices: {
customer: Customer
}
}
product: { id: number; name: string; quantity: number }
user: { id: string; email: string; country: string }
}
checkoutData: {
'paddlejs-version': '2.0.9'
apple_pay_enabled: string
display_mode: string
guest_email: string
is_popup: string
method: string
paddle_js: string
parentURL: string
parent_url: string
passthrough: string
popup: string
product: string
referring_domain: string
}
}) => void
}) => void
}
}
}
Run Code Online (Sandbox Code Playgroud)
不要太从字面上理解文档。由于您使用的是 React,因此包含任何内联 JavaScript 是没有意义的。Paddle.js 仅是客户端,因此应在useEffect根组件的回调中对其进行初始化。
// pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document'
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<script src="https://cdn.paddle.com/paddle/paddle.js" />
<NextScript />
</body>
</Html>
)
}
}
Run Code Online (Sandbox Code Playgroud)
// pages/_app.js
import { useEffect } from 'react'
function MyApp({ Component, pageProps }) {
// Initialize Paddle
useEffect(() => {
Paddle.Setup({ vendor: 1234567 })
}, [])
return <Component {...pageProps} />
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2729 次 |
| 最近记录: |