Conditions | 8 |
Paths | 12 |
Total Lines | 515 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
760 | function getSubmitCardDetailsJavascript() { |
||
761 | global $order, $request_type; |
||
762 | |||
763 | $this->_app->setupCredentials(); |
||
764 | |||
765 | $transaction_currency = $this->getTransactionCurrency(); |
||
766 | |||
767 | $clientToken = Braintree_ClientToken::generate(array( |
||
768 | 'merchantAccountId' => $this->getMerchantAccountId($transaction_currency) |
||
769 | )); |
||
770 | |||
771 | $order_total = $this->_app->formatCurrencyRaw($order->info['total'], $transaction_currency); |
||
772 | |||
773 | $getCardTokenRpcUrl = tep_href_link('ext/modules/payment/braintree_cc/rpc.php', 'action=getCardToken', 'SSL'); |
||
774 | |||
775 | if ((OSCOM_APP_PAYPAL_BRAINTREE_CC_THREE_D_SECURE === '1') && ($request_type == 'SSL')) { |
||
776 | $has3ds = 'all'; |
||
777 | } elseif ((OSCOM_APP_PAYPAL_BRAINTREE_CC_THREE_D_SECURE === '2') && ($request_type == 'SSL')) { |
||
778 | $has3ds = 'new'; |
||
779 | } else { |
||
780 | $has3ds = 'none'; |
||
781 | } |
||
782 | |||
783 | $url_not_available = addslashes(str_replace('&', '&', tep_href_link('checkout_payment.php', 'payment_error=braintree_cc&error=not_available', 'SSL'))); |
||
784 | |||
785 | $error_unavailable = addslashes($this->_app->getDef('module_cc_error_unavailable')); |
||
786 | $error_all_fields_required = addslashes($this->_app->getDef('module_cc_error_all_fields_required')); |
||
787 | $error_fields_required = addslashes($this->_app->getDef('module_cc_error_fields_required')); |
||
788 | $error_tmp_processing_problem = addslashes($this->_app->getDef('module_cc_error_tmp_processing_problem')); |
||
789 | |||
790 | $js = <<<EOD |
||
791 | <style> |
||
792 | .hosted-field { |
||
793 | height: 40px; |
||
794 | box-sizing: border-box; |
||
795 | width: 100%; |
||
796 | padding: 6px; |
||
797 | display: inline-block; |
||
798 | box-shadow: none; |
||
799 | font-weight: 600; |
||
800 | border-radius: 6px; |
||
801 | border: 1px solid #dddddd; |
||
802 | background: #fcfcfc; |
||
803 | margin-bottom: 12px; |
||
804 | background: linear-gradient(to right, white 50%, #fcfcfc 50%); |
||
805 | background-size: 200% 100%; |
||
806 | background-position: right bottom; |
||
807 | transition: all 300ms ease-in-out; |
||
808 | } |
||
809 | |||
810 | .hosted-fields--label { |
||
811 | display: block; |
||
812 | margin-bottom: 6px; |
||
813 | } |
||
814 | </style> |
||
815 | |||
816 | <script> |
||
817 | if ( typeof jQuery == 'undefined' ) { |
||
818 | document.write('<scr' + 'ipt src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></scr' + 'ipt>'); |
||
819 | } |
||
820 | </script> |
||
821 | |||
822 | <script> |
||
823 | $('form[name="checkout_confirmation"]').attr('id', 'braintree-payment-form'); |
||
824 | $('#braintree-payment-form button[type="submit"]').attr('id', 'braintree-payment-form-submit-button').button('disable'); |
||
825 | |||
826 | $(function() { |
||
827 | if (typeof $('#braintree-payment-form-submit-button').data('orig-button-text') === 'undefined') { |
||
828 | $('#braintree-payment-form-submit-button').data('orig-button-text', $('#braintree-payment-form-submit-button').html()); |
||
829 | } |
||
830 | |||
831 | var has3ds = '{$has3ds}'; |
||
832 | var do3ds = false; |
||
833 | |||
834 | function doTokenize(hostedFieldsInstance, clientInstance, nonce) { |
||
835 | if ((hostedFieldsInstance === undefined) && (nonce !== undefined)) { |
||
836 | if (do3ds === true) { |
||
837 | create3DS(clientInstance, nonce); |
||
838 | } else { |
||
839 | $('#braintree-payment-form input[name="payment_method_nonce"]').val(nonce); |
||
840 | |||
841 | $('#braintree-payment-form').submit(); |
||
842 | } |
||
843 | |||
844 | return; |
||
845 | } |
||
846 | |||
847 | hostedFieldsInstance.tokenize(function (tokenizeErr, payload) { |
||
848 | if (tokenizeErr) { |
||
849 | switch (tokenizeErr.code) { |
||
850 | case 'HOSTED_FIELDS_FIELDS_EMPTY': |
||
851 | $('#btCardStatus').html('{$error_all_fields_required}'); |
||
852 | |||
853 | if ($('#btCardStatus').is(':hidden')) { |
||
854 | $('#btCardStatus').show(); |
||
855 | } |
||
856 | |||
857 | if (($('#braintree_cards').length > 0) && ($('#braintree_cards').val() !== '0')) { |
||
858 | $('#braintree-payment-form label[for=card-token-cvv]').addClass('ui-state-error-text'); |
||
859 | } else { |
||
860 | $('#braintree-payment-form label[for=card-number]').addClass('ui-state-error-text'); |
||
861 | $('#braintree-payment-form label[for=card-exp]').addClass('ui-state-error-text'); |
||
862 | |||
863 | if ($('#card-cvv').length === 1) { |
||
864 | $('#braintree-payment-form label[for=card-cvv]').addClass('ui-state-error-text'); |
||
865 | } |
||
866 | } |
||
867 | |||
868 | break; |
||
869 | |||
870 | case 'HOSTED_FIELDS_FIELDS_INVALID': |
||
871 | $('#btCardStatus').html('{$error_fields_required}'); |
||
872 | |||
873 | if ($('#btCardStatus').is(':hidden')) { |
||
874 | $('#btCardStatus').show(); |
||
875 | } |
||
876 | |||
877 | if (($('#braintree_cards').length > 0) && ($('#braintree_cards').val() !== '0')) { |
||
878 | if ($.inArray('cvv', tokenizeErr.details.invalidFieldKeys) !== -1) { |
||
879 | $('#braintree-payment-form label[for=card-token-cvv]').addClass('ui-state-error-text'); |
||
880 | } |
||
881 | } else { |
||
882 | if ($.inArray('number', tokenizeErr.details.invalidFieldKeys) !== -1) { |
||
883 | $('#braintree-payment-form label[for=card-number]').addClass('ui-state-error-text'); |
||
884 | } |
||
885 | |||
886 | if ($.inArray('expirationDate', tokenizeErr.details.invalidFieldKeys) !== -1) { |
||
887 | $('#braintree-payment-form label[for=card-exp]').addClass('ui-state-error-text'); |
||
888 | } |
||
889 | |||
890 | if ($.inArray('cvv', tokenizeErr.details.invalidFieldKeys) !== -1) { |
||
891 | if ($('#card-cvv').length === 1) { |
||
892 | $('#braintree-payment-form label[for=card-cvv]').addClass('ui-state-error-text'); |
||
893 | } |
||
894 | } |
||
895 | } |
||
896 | |||
897 | break; |
||
898 | |||
899 | default: |
||
900 | $('#btCardStatus').html('{$error_tmp_processing_problem}'); |
||
901 | |||
902 | if ($('#btCardStatus').is(':hidden')) { |
||
903 | $('#btCardStatus').show(); |
||
904 | } |
||
905 | } |
||
906 | |||
907 | $('#braintree-payment-form-submit-button').html($('#braintree-payment-form-submit-button').data('orig-button-text')).button('enable'); |
||
908 | |||
909 | return; |
||
910 | } |
||
911 | |||
912 | if (nonce === undefined) { |
||
913 | nonce = payload.nonce; |
||
914 | } |
||
915 | |||
916 | if (do3ds === true) { |
||
917 | create3DS(clientInstance, nonce); |
||
918 | } else { |
||
919 | $('#braintree-payment-form input[name="payment_method_nonce"]').val(nonce); |
||
920 | |||
921 | $('#braintree-payment-form').submit(); |
||
922 | } |
||
923 | }); |
||
924 | } |
||
925 | |||
926 | function create3DS(clientInstance, nonce) { |
||
927 | try { |
||
928 | braintree.threeDSecure.create({ |
||
929 | client: clientInstance |
||
930 | }, function (threeDSecureErr, threeDSecureInstance) { |
||
931 | if (threeDSecureErr) { |
||
932 | $('#btCardStatus').html('{$error_tmp_processing_problem}'); |
||
933 | |||
934 | if ($('#btCardStatus').is(':hidden')) { |
||
935 | $('#btCardStatus').show(); |
||
936 | } |
||
937 | |||
938 | $('#braintree-payment-form-submit-button').html($('#braintree-payment-form-submit-button').data('orig-button-text')).button('enable'); |
||
939 | |||
940 | return; |
||
941 | } |
||
942 | |||
943 | threeDSecureInstance.verifyCard({ |
||
944 | amount: {$order_total}, |
||
945 | nonce: nonce, |
||
946 | addFrame: function (err, iframe) { |
||
947 | $.colorbox({ |
||
948 | transition: 'none', |
||
949 | closeButton: false, |
||
950 | overlayClose: false, |
||
951 | escKey: false, |
||
952 | arrowKey: false, |
||
953 | html: $('<div>').html(iframe).html() |
||
954 | }); |
||
955 | }, |
||
956 | removeFrame: function () { |
||
957 | $.colorbox.close(); |
||
958 | } |
||
959 | }, function (error, response) { |
||
960 | if (error) { |
||
961 | $('#btCardStatus').html('{$error_tmp_processing_problem}'); |
||
962 | |||
963 | if ($('#btCardStatus').is(':hidden')) { |
||
964 | $('#btCardStatus').show(); |
||
965 | } |
||
966 | |||
967 | $('#braintree-payment-form-submit-button').html($('#braintree-payment-form-submit-button').data('orig-button-text')).button('enable'); |
||
968 | |||
969 | return; |
||
970 | } |
||
971 | |||
972 | $('#braintree-payment-form input[name="payment_method_nonce"]').val(response.nonce); |
||
973 | |||
974 | $('#braintree-payment-form').submit(); |
||
975 | }); |
||
976 | }); |
||
977 | } catch (err) { |
||
978 | $('#btCardStatus').html('{$error_tmp_processing_problem}'); |
||
979 | |||
980 | if ($('#btCardStatus').is(':hidden')) { |
||
981 | $('#btCardStatus').show(); |
||
982 | } |
||
983 | |||
984 | $('#braintree-payment-form-submit-button').html($('#braintree-payment-form-submit-button').data('orig-button-text')).button('enable'); |
||
985 | } |
||
986 | } |
||
987 | |||
988 | var btClientInstance; |
||
989 | var btHostedFieldsInstance; |
||
990 | |||
991 | if ($('#braintree_cards').length > 0) { |
||
992 | $('#braintree_cards').change(function() { |
||
993 | $('#braintree-payment-form-submit-button').button('disable'); |
||
994 | |||
995 | var selected = $(this).val(); |
||
996 | |||
997 | if (selected == '0') { |
||
998 | braintreeShowNewCardFields(); |
||
999 | } else { |
||
1000 | braintreeShowStoredCardFields(selected); |
||
1001 | } |
||
1002 | }); |
||
1003 | } |
||
1004 | |||
1005 | braintree.client.create({ |
||
1006 | authorization: '{$clientToken}' |
||
1007 | }, function (clientErr, clientInstance) { |
||
1008 | if (clientErr) { |
||
1009 | $('#btCardStatus').html('{$error_unavailable}'); |
||
1010 | |||
1011 | if ($('#btCardStatus').is(':hidden')) { |
||
1012 | $('#btCardStatus').show(); |
||
1013 | } |
||
1014 | |||
1015 | $('#braintree-payment-form-submit-button').hide(); |
||
1016 | |||
1017 | window.location = '{$url_not_available}'; |
||
1018 | |||
1019 | return; |
||
1020 | } |
||
1021 | |||
1022 | btClientInstance = clientInstance; |
||
1023 | |||
1024 | if (($('#braintree_cards').length > 0) && ($('#braintree_cards').val() !== '0')) { |
||
1025 | braintreeShowStoredCardFields($('#braintree_cards').val()); |
||
1026 | } else { |
||
1027 | braintreeShowNewCardFields(); |
||
1028 | } |
||
1029 | }); |
||
1030 | |||
1031 | $('#braintree-payment-form').on('submit', function (event) { |
||
1032 | if ($('#braintree-payment-form input[name="payment_method_nonce"]').val().length > 0) { |
||
1033 | return; |
||
1034 | } |
||
1035 | |||
1036 | event.preventDefault(); |
||
1037 | |||
1038 | var doTokenizeCall = true; |
||
1039 | |||
1040 | if ($('#braintree_cards').length > 0) { |
||
1041 | if (($('#card-token-cvv').length === 1) && $('#braintree-payment-form label[for=card-token-cvv]').hasClass('ui-state-error-text')) { |
||
1042 | $('#braintree-payment-form label[for=card-token-cvv]').removeClass('ui-state-error-text'); |
||
1043 | } |
||
1044 | } |
||
1045 | |||
1046 | if ($('#braintree-payment-form label[for=card-number]').hasClass('ui-state-error-text')) { |
||
1047 | $('#braintree-payment-form label[for=card-number]').removeClass('ui-state-error-text'); |
||
1048 | } |
||
1049 | |||
1050 | if ($('#braintree-payment-form label[for=card-exp]').hasClass('ui-state-error-text')) { |
||
1051 | $('#braintree-payment-form label[for=card-exp]').removeClass('ui-state-error-text'); |
||
1052 | } |
||
1053 | |||
1054 | if (($('#card-cvv').length === 1) && $('#braintree-payment-form label[for=card-cvv]').hasClass('ui-state-error-text')) { |
||
1055 | $('#braintree-payment-form label[for=card-cvv]').removeClass('ui-state-error-text'); |
||
1056 | } |
||
1057 | |||
1058 | do3ds = false; |
||
1059 | |||
1060 | if (($('#braintree_cards').length > 0) && ($('#braintree_cards').val() !== '0')) { |
||
1061 | if (has3ds === 'all') { |
||
1062 | do3ds = true; |
||
1063 | } |
||
1064 | } else { |
||
1065 | if ((has3ds === 'all') || (has3ds === 'new')) { |
||
1066 | do3ds = true; |
||
1067 | } |
||
1068 | } |
||
1069 | |||
1070 | if ($('#braintree_cards').length > 0) { |
||
1071 | var cardsel = $('#braintree_cards').val(); |
||
1072 | |||
1073 | if (cardsel !== '0') { |
||
1074 | doTokenizeCall = false; |
||
1075 | |||
1076 | $.post('{$getCardTokenRpcUrl}', {card_id: cardsel}, function(response) { |
||
1077 | if ((typeof response == 'object') && ('result' in response) && (response.result === 1)) { |
||
1078 | doTokenize(btHostedFieldsInstance, btClientInstance, response.token); |
||
1079 | } |
||
1080 | }, 'json'); |
||
1081 | } |
||
1082 | } |
||
1083 | |||
1084 | if (doTokenizeCall === true) { |
||
1085 | doTokenize(btHostedFieldsInstance, btClientInstance); |
||
1086 | } |
||
1087 | }); |
||
1088 | |||
1089 | function braintreeShowNewCardFields() { |
||
1090 | if ($('#braintree_stored_card_cvv').length === 1) { |
||
1091 | if ($('#braintree_stored_card_cvv').is(':visible')) { |
||
1092 | $('#braintree_stored_card_cvv').hide(); |
||
1093 | } |
||
1094 | } |
||
1095 | |||
1096 | if ($('#braintree-payment-form label[for=card-number]').hasClass('ui-state-error-text')) { |
||
1097 | $('#braintree-payment-form label[for=card-number]').removeClass('ui-state-error-text'); |
||
1098 | } |
||
1099 | |||
1100 | if ($('#braintree-payment-form label[for=card-exp]').hasClass('ui-state-error-text')) { |
||
1101 | $('#braintree-payment-form label[for=card-exp]').removeClass('ui-state-error-text'); |
||
1102 | } |
||
1103 | |||
1104 | if (($('#card-cvv').length === 1) && $('#braintree-payment-form label[for=card-cvv]').hasClass('ui-state-error-text')) { |
||
1105 | $('#braintree-payment-form label[for=card-cvv]').removeClass('ui-state-error-text'); |
||
1106 | } |
||
1107 | |||
1108 | if ($('#braintree_new_card').not(':visible')) { |
||
1109 | $('#braintree_new_card').show(); |
||
1110 | } |
||
1111 | |||
1112 | if (btHostedFieldsInstance !== undefined) { |
||
1113 | btHostedFieldsInstance.teardown(function (teardownErr) { |
||
1114 | if (teardownErr) { |
||
1115 | return; |
||
1116 | } |
||
1117 | |||
1118 | braintreeCreateInstance(); |
||
1119 | }); |
||
1120 | |||
1121 | return; |
||
1122 | } |
||
1123 | |||
1124 | braintreeCreateInstance(); |
||
1125 | } |
||
1126 | |||
1127 | function braintreeShowStoredCardFields(id) { |
||
1128 | if ($('#braintree_stored_card_cvv').length === 1) { |
||
1129 | if ($('#braintree-payment-form label[for=card-token-cvv]').hasClass('ui-state-error-text')) { |
||
1130 | $('#braintree-payment-form label[for=card-token-cvv]').removeClass('ui-state-error-text'); |
||
1131 | } |
||
1132 | |||
1133 | if ($('#braintree_stored_card_cvv').not(':visible')) { |
||
1134 | $('#braintree_stored_card_cvv').show(); |
||
1135 | } |
||
1136 | } |
||
1137 | |||
1138 | if ($('#braintree_new_card').is(':visible')) { |
||
1139 | $('#braintree_new_card').hide(); |
||
1140 | |||
1141 | if (btHostedFieldsInstance !== undefined) { |
||
1142 | btHostedFieldsInstance.teardown(function (teardownErr) { |
||
1143 | if (teardownErr) { |
||
1144 | return; |
||
1145 | } |
||
1146 | |||
1147 | braintreeCreateStoredCardInstance(); |
||
1148 | }); |
||
1149 | |||
1150 | return; |
||
1151 | } |
||
1152 | |||
1153 | braintreeCreateStoredCardInstance(); |
||
1154 | |||
1155 | return; |
||
1156 | } |
||
1157 | |||
1158 | if (btHostedFieldsInstance === undefined) { |
||
1159 | braintreeCreateStoredCardInstance(); |
||
1160 | } else { |
||
1161 | $('#braintree-payment-form-submit-button').button('enable'); |
||
1162 | } |
||
1163 | } |
||
1164 | |||
1165 | function braintreeCreateInstance() { |
||
1166 | var fields = { |
||
1167 | number: { |
||
1168 | selector: '#card-number' |
||
1169 | }, |
||
1170 | expirationDate: { |
||
1171 | selector: '#card-exp', |
||
1172 | placeholder: 'MM / YYYY' |
||
1173 | } |
||
1174 | }; |
||
1175 | |||
1176 | if ($('#card-cvv').length === 1) { |
||
1177 | fields.cvv = { |
||
1178 | selector: '#card-cvv' |
||
1179 | }; |
||
1180 | } |
||
1181 | |||
1182 | braintree.hostedFields.create({ |
||
1183 | client: btClientInstance, |
||
1184 | styles: { |
||
1185 | ':focus': { |
||
1186 | 'color': 'black' |
||
1187 | }, |
||
1188 | '.valid': { |
||
1189 | 'color': '#8bdda8' |
||
1190 | } |
||
1191 | }, |
||
1192 | fields: fields |
||
1193 | }, function (hostedFieldsErr, hostedFieldsInstance) { |
||
1194 | if (hostedFieldsErr) { |
||
1195 | $('#btCardStatus').html('{$error_unavailable}'); |
||
1196 | |||
1197 | if ($('#btCardStatus').is(':hidden')) { |
||
1198 | $('#btCardStatus').show(); |
||
1199 | } |
||
1200 | |||
1201 | $('#braintree-payment-form-submit-button').hide(); |
||
1202 | |||
1203 | window.location = '{$url_not_available}'; |
||
1204 | |||
1205 | return; |
||
1206 | } |
||
1207 | |||
1208 | btHostedFieldsInstance = hostedFieldsInstance; |
||
1209 | |||
1210 | $('#braintree-payment-form-submit-button').button('enable'); |
||
1211 | }); |
||
1212 | } |
||
1213 | |||
1214 | function braintreeCreateStoredCardInstance() { |
||
1215 | if ($('#card-token-cvv').length === 1) { |
||
1216 | braintree.hostedFields.create({ |
||
1217 | client: btClientInstance, |
||
1218 | styles: { |
||
1219 | ':focus': { |
||
1220 | 'color': 'black' |
||
1221 | }, |
||
1222 | '.valid': { |
||
1223 | 'color': '#8bdda8' |
||
1224 | } |
||
1225 | }, |
||
1226 | fields: { |
||
1227 | cvv: { |
||
1228 | selector: '#card-token-cvv' |
||
1229 | } |
||
1230 | } |
||
1231 | }, function (hostedFieldsErr, hostedFieldsInstance) { |
||
1232 | if (hostedFieldsErr) { |
||
1233 | $('#btCardStatus').html('{$error_unavailable}'); |
||
1234 | |||
1235 | if ($('#btCardStatus').is(':hidden')) { |
||
1236 | $('#btCardStatus').show(); |
||
1237 | } |
||
1238 | |||
1239 | $('#braintree-payment-form-submit-button').hide(); |
||
1240 | |||
1241 | window.location = '{$url_not_available}'; |
||
1242 | |||
1243 | return; |
||
1244 | } |
||
1245 | |||
1246 | btHostedFieldsInstance = hostedFieldsInstance; |
||
1247 | |||
1248 | $('#braintree-payment-form-submit-button').button('enable'); |
||
1249 | }); |
||
1250 | } else { |
||
1251 | btHostedFieldsInstance = undefined; |
||
1252 | |||
1253 | $('#braintree-payment-form-submit-button').button('enable'); |
||
1254 | } |
||
1255 | } |
||
1256 | }); |
||
1257 | </script> |
||
1258 | EOD; |
||
1259 | |||
1260 | $js_scripts = '<script src="https://js.braintreegateway.com/web/3.2.0/js/client.min.js"></script>' . |
||
1261 | '<script src="https://js.braintreegateway.com/web/3.2.0/js/hosted-fields.min.js"></script>'; |
||
1262 | |||
1263 | if ((OSCOM_APP_PAYPAL_BRAINTREE_CC_THREE_D_SECURE === '1') || (OSCOM_APP_PAYPAL_BRAINTREE_CC_THREE_D_SECURE === '2')) { |
||
1264 | $js_scripts .= '<script src="https://js.braintreegateway.com/web/3.2.0/js/three-d-secure.min.js"></script>'; |
||
1265 | } |
||
1266 | |||
1267 | if ($this->templateClassExists()) { |
||
1268 | $GLOBALS['oscTemplate']->addBlock($js_scripts, 'footer_scripts'); |
||
1269 | } else { |
||
1270 | $js .= $js_scripts; |
||
1271 | } |
||
1272 | |||
1273 | return $js; |
||
1274 | } |
||
1275 | |||
1281 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: