-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(demo): add payment form (unzer payment integration) #671
Open
itscark
wants to merge
3
commits into
shopware:main
Choose a base branch
from
itscark:feat/payment-form
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"vue-demo-store": minor | ||
"@shopware-pwa/composables-next": minor | ||
--- | ||
|
||
add payment form |
202 changes: 202 additions & 0 deletions
202
examples/unzer-payment/components/CheckoutPaymentUnzerCreditCardPaymentHandler.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
<script setup lang="ts"> | ||
import type { Schemas } from "#shopware"; | ||
import { defu } from "defu"; | ||
import { useCmsTranslations } from "@shopware-pwa/composables-next"; | ||
|
||
type Translations = { | ||
UnzerPayment: { | ||
frame: { | ||
creditCard: { | ||
choose: string; | ||
new: string; | ||
number: string; | ||
expiry: string; | ||
cvc: string; | ||
remember: string; | ||
}; | ||
}; | ||
}; | ||
}; | ||
|
||
let translations: Translations = { | ||
UnzerPayment: { | ||
frame: { | ||
creditCard: { | ||
choose: "Choose a saved credit card", | ||
new: "Use a new credit card", | ||
number: "Card number", | ||
expiry: "Expiry date", | ||
cvc: "CVC", | ||
remember: "Remember this card", | ||
}, | ||
}, | ||
}, | ||
}; | ||
translations = defu(translations, useCmsTranslations()) as Translations; | ||
|
||
const { isUnzerPaymentMethod, unzerInstance } = useUnzer(); | ||
|
||
const orderButtonDisabled = inject<Ref<boolean | undefined>>( | ||
"orderButtonDisabled", | ||
ref(true), | ||
); | ||
|
||
async function createResource(): Promise<void> { | ||
console.debug("[unzer] create:resource", creditCard.value); | ||
|
||
if (creditCard.value !== null) { | ||
try { | ||
const response: { id: string } = await creditCard.value.createResource(); | ||
console.debug("[unzer] create:resource:response", response); | ||
const store = useLocalStorage("unzerId", ""); | ||
store.value = response.id; | ||
orderButtonDisabled.value = false; | ||
} catch (error) { | ||
console.error("[unzer] create:resource:error", error); | ||
if (error?.message) { | ||
errorText.value = error?.message; | ||
} | ||
throw error; | ||
} | ||
} else { | ||
throw new Error("[unzer] Resource ID not found"); | ||
} | ||
} | ||
|
||
const creditCard = ref(null); | ||
const number = ref<HTMLElement>(); | ||
const expiry = ref<HTMLElement>(); | ||
const cvc = ref<HTMLElement>(); | ||
|
||
watch(unzerInstance, (unzer) => { | ||
const card = unzer.Card(); | ||
|
||
card.create("number", { | ||
containerId: "card-element-id-number", | ||
onlyIframe: false, | ||
}); | ||
|
||
card.create("expiry", { | ||
containerId: "card-element-id-expiry", | ||
onlyIframe: false, | ||
}); | ||
|
||
card.create("cvc", { | ||
containerId: "card-element-id-cvc", | ||
onlyIframe: false, | ||
}); | ||
|
||
creditCard.value = card; | ||
}); | ||
|
||
watch( | ||
() => [number, expiry, cvc], | ||
([number, expiry, cvc]) => { | ||
if (!number.value || !expiry.value || !cvc.value) return; | ||
console.log(number.value, expiry.value, cvc.value); | ||
const allSuccess = [number.value, expiry.value, cvc.value].every( | ||
(ref) => ref.firstChild && ref.firstChild.classList.contains("success"), | ||
); | ||
//orderButtonDisabled.value = !allSuccess; | ||
}, | ||
{ deep: true, immediate: true }, | ||
); | ||
|
||
onBeforeUnmount(() => { | ||
document.querySelectorAll(".unzerSandboxNotify").forEach((el) => el.remove()); | ||
orderButtonDisabled.value = false; | ||
// avoid duplicated iframes | ||
[number, expiry, cvc].forEach((field) => { | ||
while (field.value && field.value.firstChild) { | ||
field.value.removeChild(field.value.firstChild); | ||
} | ||
}); | ||
}); | ||
|
||
const errorText = ref(); | ||
|
||
const { listen } = useEventBus(); | ||
listen( | ||
["order:placed", "order:retry-payment"], | ||
async (paymentMethod: Schemas["PaymentMethod"], eventName: string) => { | ||
const isUnzerMethod = isUnzerPaymentMethod(paymentMethod); | ||
if (!isUnzerMethod) return; | ||
|
||
console.debug(`[unzer] ${eventName}`); | ||
errorText.value = null; | ||
|
||
try { | ||
await createResource(); | ||
console.debug("[unzer] order:placed:resource-created"); | ||
} catch (e) { | ||
console.error("[unzer] order:placed:resource-error", e); | ||
if (e?.message) { | ||
errorText.value = e.message; | ||
} | ||
throw e; | ||
} | ||
}, | ||
); | ||
</script> | ||
|
||
<template> | ||
<div id="unzer-payment-base" class="unzer-payment-base"> | ||
<div class="unzer-payment-base-body"> | ||
<div | ||
class="unzer-payment-frame border-brand-lightgray lg:shadow-md lg:border-1 lg:p-4" | ||
> | ||
<div class="unzer-payment-credit-card-wrapper"> | ||
<UnzerTestDataTable /> | ||
|
||
<div | ||
v-if="errorText" | ||
id="alert-border-2" | ||
class="unzer-error flex items-center p-4 mt-2 mb-4 text-red-800 border-t-4 border-red-300 bg-red-50 dark:text-red-400 dark:bg-gray-800 dark:border-red-800" | ||
role="alert" | ||
> | ||
<svg | ||
class="flex-shrink-0 w-4 h-4" | ||
aria-hidden="true" | ||
xmlns="http://www.w3.org/2000/svg" | ||
fill="currentColor" | ||
viewBox="0 0 20 20" | ||
> | ||
<path | ||
d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5ZM9.5 4a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM12 15H8a1 1 0 0 1 0-2h1v-3H8a1 1 0 0 1 0-2h2a1 1 0 0 1 1 1v4h1a1 1 0 0 1 0 2Z" | ||
/> | ||
</svg> | ||
<div class="ml-3 text-sm font-medium"> | ||
{{ errorText }} | ||
</div> | ||
</div> | ||
|
||
<form id="payment-form" class="unzerUI form" novalidate> | ||
<div class="field"> | ||
<div | ||
ref="number" | ||
id="card-element-id-number" | ||
class="unzerInput" | ||
></div> | ||
</div> | ||
<div class="two fields"> | ||
<div class="field ten wide"> | ||
<div | ||
ref="expiry" | ||
id="card-element-id-expiry" | ||
class="unzerInput" | ||
></div> | ||
</div> | ||
<div class="field six wide"> | ||
<div | ||
ref="cvc" | ||
id="card-element-id-cvc" | ||
class="unzerInput" | ||
></div> | ||
</div> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</template> |
159 changes: 159 additions & 0 deletions
159
examples/unzer-payment/components/UnzerTestDataTable.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<script setup lang="ts"> | ||
import { defu } from "defu"; | ||
import { useCmsTranslations } from "@shopware-pwa/composables-next"; | ||
|
||
const showTestData = computed(() => { | ||
return process.env.NODE_ENV === "development" ?? false; | ||
}); | ||
|
||
type CreditCard = { | ||
brand: string; | ||
number: string; | ||
expiry: string; | ||
cvc: string; | ||
secret: string; | ||
}; | ||
|
||
const creditCards: CreditCard[] = [ | ||
{ | ||
brand: "Mastercard", | ||
number: "5232050000010003", | ||
expiry: "12/30", | ||
cvc: "123", | ||
secret: "None", | ||
}, | ||
{ | ||
brand: "Visa", | ||
number: "4711100000000000", | ||
expiry: "12/30", | ||
cvc: "123", | ||
secret: "secret", | ||
}, | ||
{ | ||
brand: "Visa", | ||
number: "4012888888881881", | ||
expiry: "12/30", | ||
cvc: "123", | ||
secret: "None", | ||
}, | ||
{ | ||
brand: "Visa", | ||
number: "4644400000308888", | ||
expiry: "12/30", | ||
cvc: "123", | ||
secret: "None", | ||
}, | ||
]; | ||
|
||
type Translations = { | ||
UnzerPayment: { | ||
frame: { | ||
creditCard: { | ||
testData: { | ||
brand: string; | ||
number: string; | ||
expiry: string; | ||
cvc: string; | ||
secret: string; | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; | ||
|
||
let translations: Translations = { | ||
UnzerPayment: { | ||
frame: { | ||
creditCard: { | ||
testData: { | ||
brand: "Brand", | ||
number: "Number", | ||
expiry: "Expiry", | ||
cvc: "CVC", | ||
secret: "Secret", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
translations = defu(translations, useCmsTranslations()) as Translations; | ||
</script> | ||
|
||
<template> | ||
<div v-if="showTestData" class="unzer-test-data"> | ||
<div class="mb-8 flow-root"> | ||
<div class="-mx-4 -my-2 overflow-x-auto sm:-mx-6 lg:-mx-8"> | ||
<div class="inline-block min-w-full py-2 align-middle sm:px-6 lg:px-8"> | ||
<table class="min-w-full divide-y divide-gray-300"> | ||
<thead> | ||
<tr class="divide-x divide-gray-200"> | ||
<th | ||
scope="col" | ||
class="py-3.5 pl-4 pr-4 text-left text-sm font-semibold text-gray-900 sm:pl-0" | ||
> | ||
{{ | ||
translations.UnzerPayment.frame.creditCard.testData.brand | ||
}} | ||
</th> | ||
<th | ||
scope="col" | ||
class="px-4 py-3.5 text-left text-sm font-semibold text-gray-900" | ||
> | ||
{{ | ||
translations.UnzerPayment.frame.creditCard.testData.number | ||
}} | ||
</th> | ||
<th | ||
scope="col" | ||
class="px-4 py-3.5 text-left text-sm font-semibold text-gray-900" | ||
> | ||
{{ | ||
translations.UnzerPayment.frame.creditCard.testData.expiry | ||
}} | ||
</th> | ||
<th | ||
scope="col" | ||
class="py-3.5 pl-4 pr-4 text-left text-sm font-semibold text-gray-900 sm:pr-0" | ||
> | ||
{{ translations.UnzerPayment.frame.creditCard.testData.cvc }} | ||
</th> | ||
<th | ||
scope="col" | ||
class="py-3.5 pl-4 pr-4 text-left text-sm font-semibold text-gray-900 sm:pr-0" | ||
> | ||
{{ | ||
translations.UnzerPayment.frame.creditCard.testData.secret | ||
}} | ||
</th> | ||
</tr> | ||
</thead> | ||
<tbody class="divide-y divide-gray-200 bg-white"> | ||
<tr | ||
v-for="card in creditCards" | ||
:key="card.brand" | ||
class="divide-x divide-gray-200 text-gray-500 text-sm" | ||
> | ||
<td class="py-4 pl-4 pr-4 font-medium text-gray-900 sm:pl-0"> | ||
{{ card.brand }} | ||
</td> | ||
<td class="p-4"> | ||
{{ card.number }} | ||
</td> | ||
<td class="p-4"> | ||
{{ card.expiry }} | ||
</td> | ||
<td class="p-4"> | ||
{{ card.cvc }} | ||
</td> | ||
<td class="py-4 pl-4 pr-4 sm:pr-0"> | ||
{{ card.secret }} | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</template> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.