Skip to content

Instantly share code, notes, and snippets.

@NeverwinterMoon
Created November 24, 2023 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeverwinterMoon/a5d8e5a1d3dd9abae8dd8e4cea14f871 to your computer and use it in GitHub Desktop.
Save NeverwinterMoon/a5d8e5a1d3dd9abae8dd8e4cea14f871 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<script src="https://pay.every-pay.eu/google_pay/google-pay-client-1.4.js"></script>
</head>
<body>
<div id="google-pay-container">
<script>
// Initialize the Web SDK
const googlePayClient = GooglePayClient.initialize({ testEnv: true })
googlePayClient.addGooglePayButton({
buttonColor: 'default',
buttonType: 'plain',
buttonSizeMode: 'fill',
onClick: googlePayClicked,
})
async function googlePayClicked() {
// Initialise the payment and retrieve the response data
const data = await initPayment()
// Pass data to the handleResponse function to configure Google Pay client
handleResponse(data)
// Display the Google Pay payment sheet
googlePayClient.createGooglePayPayment()
}
async function initPayment() {
// Send a request to the server endpoint with the amount as a query parameter
const response = await fetch(`/send-request`, {
method: 'post',
headers: {
'content-type': 'application/json',
},
})
if (!response?.ok) {
throw new Error('Invalid response from server')
}
return response.json()
}
// Function to handle the response data
function handleResponse({ responseData }) {
// Create responseParams object using var keyword
const responseParams = {
access_token: responseData.mobile_access_token,
api_username: responseData.api_username,
account_name: responseData.account_name,
payment_reference: responseData.payment_reference,
country_code: responseData.descriptor_country,
currency_code: responseData.currency,
payment_link: responseData.payment_link,
amount: `${responseData.initial_amount}`,
}
googlePayClient.configure(responseParams)
}
</script>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment