|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file provides an example implementation of the Googlepay payment type. |
| 5 | + */ |
| 6 | + |
| 7 | +/** Require the constants of this example */ |
| 8 | +require_once __DIR__ . '/Constants.php'; |
| 9 | + |
| 10 | +/** @noinspection PhpIncludeInspection */ |
| 11 | +/** Require the composer autoloader file */ |
| 12 | +require_once __DIR__ . '/../../../../autoload.php'; |
| 13 | +?> |
| 14 | + |
| 15 | +<!DOCTYPE html> |
| 16 | +<html lang="en"> |
| 17 | +<head> |
| 18 | + <meta charset="UTF-8"/> |
| 19 | + <title>Unzer UI Examples</title> |
| 20 | + <script src="https://code.jquery.com/jquery-3.6.0.min.js" |
| 21 | + integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" |
| 22 | + crossorigin="anonymous"></script> |
| 23 | + |
| 24 | + <link rel="stylesheet" href="https://static.unzer.com/v1/unzer.css"/> |
| 25 | + <script type="text/javascript" src="https://static.unzer.com/v1/unzer.js"></script> |
| 26 | + <script src="https://pay.google.com/gp/p/js/pay.js"></script> |
| 27 | + |
| 28 | +</head> |
| 29 | + |
| 30 | +<body style="margin: 70px 70px 0;"> |
| 31 | + |
| 32 | +<p><a href="https://docs.unzer.com/reference/test-data" target="_blank">Click here to open our test data in new tab.</a><br/> |
| 33 | +</p> |
| 34 | + |
| 35 | +<form id="payment-form" class="unzerUI form" novalidate> |
| 36 | + <!-- This is just for the example - Start --> |
| 37 | + <div class="fields inline"> |
| 38 | + <label for="transaction_type">Chose the transaction type you want to test:</label> |
| 39 | + <div class="field"> |
| 40 | + <div class="unzerUI radio checkbox"> |
| 41 | + <input type="radio" name="transaction_type" value="authorize" checked=""> |
| 42 | + <label>Authorize</label> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + <div class="field"> |
| 46 | + <div class="unzerUI radio checkbox"> |
| 47 | + <input type="radio" name="transaction_type" value="charge"> |
| 48 | + <label>Charge</label> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | + </div> |
| 52 | + <!-- This is just for the example - End --> |
| 53 | + |
| 54 | + <div> |
| 55 | + <div class="field" id="error-holder" style="color: #9f3a38"> </div> |
| 56 | + <div class="button-well"> |
| 57 | + <div class="applePayButtonContainer"> |
| 58 | + <div class="apple-pay-button apple-pay-button-black" lang="us" |
| 59 | + onclick="setupApplePaySession()" |
| 60 | + title="Start Apple Pay" role="link" tabindex="0"> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | +</form> |
| 66 | + |
| 67 | +<div id="dimmer-holder-googlepay" class="ui active dimmer" style="display: none;"> |
| 68 | + <div class="ui loader"></div> |
| 69 | +</div> |
| 70 | + |
| 71 | +<div id="example-googlepay-stack"></div> |
| 72 | +<div id="error-holder-googlepay" class="field" style="text-align: center; color: #9f3a38"></div> |
| 73 | + |
| 74 | +<script> |
| 75 | + const unzerInstance = new unzer('<?php echo UNZER_PAPI_PUBLIC_KEY; ?>'); |
| 76 | + const colors = ['black','white']; |
| 77 | + const googlepayChannelId = "<?php echo UNZER_EXAMPLE_GOOGLEPAY_CHANNEL; ?>" |
| 78 | + const stackHolder = document.querySelector('#example-googlepay-stack'); |
| 79 | + |
| 80 | + const tmpPaymentDataRequestObject = { |
| 81 | + gatewayMerchantId: googlepayChannelId, |
| 82 | + allowedCardNetworks: [ |
| 83 | + 'MASTERCARD', |
| 84 | + 'VISA', |
| 85 | + 'DISCOVER', |
| 86 | + 'JCB', |
| 87 | + ], |
| 88 | + merchantInfo: { |
| 89 | + merchantId: googlepayChannelId, |
| 90 | + merchantName: 'Example Merchant' |
| 91 | + }, |
| 92 | + transactionInfo: { |
| 93 | + displayItems: [], |
| 94 | + countryCode: 'DE', |
| 95 | + currencyCode: 'EUR', |
| 96 | + totalPrice: '12.00', |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + function handleGooglepayError(error) { |
| 101 | + let errorMessage = error.customerMessage || error.message || 'Error'; |
| 102 | + if (error.data && Array.isArray(error.data.errors) && error.data.errors[0]) { |
| 103 | + errorMessage = error.data.errors[0].customerMessage || 'Error' |
| 104 | + } |
| 105 | + |
| 106 | + document.getElementById('error-holder-googlepay').innerHTML = errorMessage; |
| 107 | + |
| 108 | + return { |
| 109 | + status: 'error', |
| 110 | + message: errorMessage || 'Unexpected error' |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + colors.map(function (color) { |
| 115 | + const htmlString = '<div id="example-googlepay-' + color + '" class="field" style="text-align: center; margin: 5px 0"></div>' |
| 116 | + return ({ |
| 117 | + instance: null, |
| 118 | + color: color, |
| 119 | + htmlString: htmlString |
| 120 | + }) |
| 121 | + }).forEach(function (item) { |
| 122 | + stackHolder.insertAdjacentHTML('beforeend', item.htmlString) |
| 123 | + item.instance = unzerInstance.Googlepay() |
| 124 | + const extendedPaymentDataRequestObject = { |
| 125 | + ...tmpPaymentDataRequestObject, |
| 126 | + buttonColor: item.color, |
| 127 | + onPaymentAuthorizedCallback: (paymentData) => { |
| 128 | + document.getElementById('error-holder-googlepay').innerHTML = '' |
| 129 | + const $form = $('form[id="payment-form"]'); |
| 130 | + const formObject = QueryStringToObject($form.serialize()); |
| 131 | + |
| 132 | + return item.instance.createResource(paymentData) |
| 133 | + .then(typeCreationResult => { |
| 134 | + document.getElementById('dimmer-holder-googlepay').style.display = 'block'; |
| 135 | + formObject.typeId = typeCreationResult.id; |
| 136 | + |
| 137 | + return fetch( |
| 138 | + './Controller.php', |
| 139 | + { |
| 140 | + body: JSON.stringify(formObject), |
| 141 | + method: 'POST' |
| 142 | + } |
| 143 | + ) |
| 144 | + .then(response => response.json()) |
| 145 | + .then( transactionResult => { |
| 146 | + const status = transactionResult.transactionStatus; |
| 147 | + |
| 148 | + if (status === 'success' || status === 'pending') { |
| 149 | + if (transactionResult.redirectUrl.trim().length !== 0) { |
| 150 | + window.location.href = transactionResult.redirectUrl; |
| 151 | + } else { |
| 152 | + window.location.href = '<?php echo RETURN_CONTROLLER_URL; ?>'; |
| 153 | + } |
| 154 | + return { status: 'success' }; |
| 155 | + } |
| 156 | + window.location.href = '<?php echo FAILURE_URL; ?>'; |
| 157 | + return { |
| 158 | + status: 'error', |
| 159 | + message: transactionResult.customerMessage || transactionResult.message || 'Unexpected error' |
| 160 | + } |
| 161 | + }) |
| 162 | + .catch(function (error) { |
| 163 | + return handleGooglepayError(error); |
| 164 | + } |
| 165 | + ) |
| 166 | + }) |
| 167 | + .catch(function (error) { |
| 168 | + return handleGooglepayError(error); |
| 169 | + }) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + const paymentDataRequestObject = item.instance.initPaymentDataRequestObject(extendedPaymentDataRequestObject) |
| 174 | + item.instance.create( |
| 175 | + { |
| 176 | + containerId: 'example-googlepay-' + item.color |
| 177 | + }, |
| 178 | + paymentDataRequestObject |
| 179 | + ) |
| 180 | + }) |
| 181 | + |
| 182 | + // Translates query string to object |
| 183 | + function QueryStringToObject(queryString) { |
| 184 | + const pairs = queryString.slice().split('&'); |
| 185 | + let result = {}; |
| 186 | + |
| 187 | + pairs.forEach(function(pair) { |
| 188 | + pair = pair.split('='); |
| 189 | + result[pair[0]] = decodeURIComponent(pair[1] || ''); |
| 190 | + }); |
| 191 | + return JSON.parse(JSON.stringify(result)); |
| 192 | + } |
| 193 | + |
| 194 | +</script> |
| 195 | + |
| 196 | +</body> |
| 197 | +</html> |
0 commit comments