# Code snippets
## Setup your Apple Pay Button
```html
<div id="applepay-container"></div>
```
```js
if (!window.ApplePaySession) {
console.error('This device does not support Apple Pay');
}
if (!ApplePaySession.canMakePayments()) {
console.error('This device is not capable of making Apple Pay payments');
}
const applepay = paypal.Applepay();
applepay.config()
.then(applepayConfig => {
if (applepayConfig.isEligible) {
document.getElementById("applepay-container").innerHTML = '<apple-pay-button id="btn-appl" buttonstyle="black" type="buy" locale="en">';
}
})
.catch(applepayConfigError => {
console.error('Error while fetching Apple Pay configuration.');
});
```
## Create an ApplePaySession
```typescript=
// Please note that `applepayConfig` below is the response from `paypal.Applepay().config()`.
const paymentRequest = {
countryCode: applepayConfig.countryCode,
merchantCapabilities: applepayConfig.merchantCapabilities,
supportedNetworks: applepayConfig.supportedNetworks,
currencyCode: "USD",
requiredShippingContactFields: ["name", "phone", "email", "postalAddress"],
requiredBillingContactFields: ["name", "phone", "email", "postalAddress"],
total: {
label: "Demo",
type: "final",
amount: "100.00",
}
};
const session = new ApplePaySession(4, paymentRequest);
```
## onvalidatemerchant callback
```typescript=
session.onvalidatemerchant = (event) => {
applepay.validateMerchant({
validationUrl: event.validationURL,
displayName: "My Store"
})
.then(validateResult => {
session.completeMerchantValidation(validateResult.merchantSession);
})
.catch(validateError => {
console.error(validateError);
session.abort();
});
};
```
## onpaymentauthorized callback
```typescript=
session.onpaymentauthorized = (event) => {
console.log('Your billing address is:', event.payment.billingContact);
console.log('Your shipping address is:', event.payment.shippingContact);
// `billingContact` and `shippingContact` object structure/format can be found below:
// https://developer.apple.com/documentation/apple_pay_on_the_web/applepaypaymentcontact
fetch(`/api/orders`, {
method: 'post'
body:
// use the "body" param to optionally pass additional order information like
// amount and amount break down - tax, shipping, handling, etc.,
// item data - sku, name, unit_amount, quantity, etc.,
// shipping - name, address, type, etc. name and address are available in `event.payment.shippingContact`.
})
.then(res => res.json())
.then((createOrderData) => {
var orderId = createOrderData.id;
applepay.confirmOrder({
orderID: orderId,
token: event.payment.token,
billingContact: event.payment.billingContact
})
.then(confirmResult => {
session.completePayment(ApplePaySession.STATUS_SUCCESS);
//Submit approval to the server and authorize or capture the order.
fetch(`/api/orders/${orderId}/capture`, {
method: "post",
})
.then(res => res.json())
.then(captureResult => {
console.log(captureResult);
})
.catch(captureError => console.error(captureError));
})
.catch(confirmError => {
if (confirmError) {
console.error('Error confirming order with applepay token');
console.error(confirmError);
session.completePayment(ApplePaySession.STATUS_FAILURE);
}
});
});
};
```
## Show the payment sheet
```typescript=
session.begin();
```
## Connected Path Merchant Onboarding
### Request
```
curl --location --request POST 'https://api.paypal.com/v2/customer/partner-referrals' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer A21AALPeseIupGvBTN1s8aSMgHilfXMO_uDSUVFHoxH1PjxU4oMGdVqq7jq8Co2fntxQTNr-EyHaeM8j_HHEaL_2YZgq57EFw' \
--data-raw '{
"email": "seller@example.com",
"individual_owners": [{
....
}
],
"business_entity": {
....
},
"preferred_language_code": "en-US",
"operations": [{
....
}
],
"products": [
"PPCP",
"PAYMENT_METHODS"
],
"capabilities": [
"APPLE_PAY"
],
"legal_consents": [
....
]
}'
```
### Response
```
200 OK
{
"links": [
{
"href": "https://api.paypal.com/v2/customer/partner-referrals/OTc5NWRkMjEtN2ZmMy00NWIyLTgwMTctZTE5OTM2YTczZTA2czYwc25tRmRSS1g1WCs4WDBSYlRiaDY2ZGFwV2IrYUxKNDhzbWpxd2hUZz12Mg==",
"rel": "self",
"method": "GET",
"description": "Read Referral Data shared by the Caller."
},
{
"href": "https://www.paypal.com/bizsignup/partner/entry?referralToken=OTc5NWRkMjEtN2ZmMy00NWIyLTgwMTctZTE5OTM2YTczZTA2czYwc25tRmRSS1g1WCs4WDBSYlRiaDY2ZGFwV2IrYUxKNDhzbWpxd2hUZz12Mg==",
"rel": "action_url",
"method": "GET",
"description": "Target WEB REDIRECT URL for the next action. Customer should be redirected to this URL in the browser."
}
]
}
```