@Override public AuthorizationResult authorize(TransactionRequest transactionRequest, CreditCard creditCard) { return saleOrAuthorize(transactionRequest, creditCard, false); }
private static void addMetaData(boolean update, Map<String,Object> metadata, String key, Object value, boolean allowTrimValue) { addMetaData( update, metadata, key, value==null ? (String)value : value.toString(), allowTrimValue ); }
/** https://stripe.com/docs/api#update_customer */ private static void addCustomerParams( CreditCard creditCard, boolean update, Map<String,Object> customerParams ) { addParam(update, customerParams, "description", creditCard.getComments()); addParam(update, customerParams, "email", creditCard.getEmail()); addParam(update, customerParams, "metadata", makeMetadata(creditCard, update)); }
addParam(false, chargeParams, "amount", amount); addParam(false, chargeParams, "currency", currency.getCurrencyCode()); if(creditCard.getProviderUniqueId() != null) { addParam(false, chargeParams, "customer", creditCard.getProviderUniqueId()); } else { addParam(false, chargeParams, "card", makeCardParams(creditCard, false)); addParam(false, chargeParams, "description", transactionRequest.getDescription()); addParam(false, chargeParams, "metadata", makeMetadata(transactionRequest, creditCard, false)); addParam(false, chargeParams, "capture", capture); if(transactionRequest.getOrderNumber() != null) { String combined = STATEMENT_DESCRIPTOR_PREFIX + transactionRequest.getOrderNumber(); if(combined.length() <= MAX_STATEMENT_DESCRIPTOR_LEN) addParam(false, chargeParams, "statement_descriptor", combined); addParam(false, chargeParams, "receipt_email", creditCard.getEmail()); addParam(false, chargeParams, "shipping", makeShippingParams(transactionRequest, creditCard, false)); try { Charge charge = Charge.create(chargeParams, options); if(approvalCode != null && approvalCode.startsWith("ch_")) approvalCode = approvalCode.substring(3); return new AuthorizationResult( getProviderId(), TransactionResult.CommunicationResult.SUCCESS, ConvertedError converted = convertError(e); if(converted.declineReason == null) {
@Override public String storeCreditCard(CreditCard creditCard) throws IOException { // Create the Customer Map<String,Object> customerParams = new HashMap<String,Object>(); addParam(false, customerParams, "card", makeCardParams(creditCard, false)); addCustomerParams(creditCard, false, customerParams); // Make API call try { Customer customer = Customer.create(customerParams, options); return customer.getId(); } catch(StripeException e) { ConvertedError converted = convertError(e); // TODO: Throw ErrorCodeException to provide more details throw new LocalizedIOException(e, accessor, "MerchantServicesProvider.storeCreditCard.notSuccessful"); } }
@Override public void updateCreditCardNumberAndExpiration( CreditCard creditCard, String cardNumber, byte expirationMonth, short expirationYear, String cardCode ) throws IOException { // Replace the default Card Map<String,Object> cardParams = makeCardParams( creditCard, true, cardNumber, expirationMonth, expirationYear, cardCode!=null ? CreditCard.numbersOnly(cardCode) : creditCard.getCardCode() ); Map<String,Object> updateParams = new HashMap<String,Object>(); addParam(true, updateParams, "card", cardParams); try { Customer customer = Customer.retrieve(creditCard.getProviderUniqueId(), options); customer.update(updateParams, options); } catch(StripeException e) { ConvertedError converted = convertError(e); // TODO: Throw ErrorCodeException to provide more details throw new LocalizedIOException(e, accessor, "MerchantServicesProvider.updateCreditCardNumberAndExpiration.notSuccessful"); } }
@Override public void updateCreditCard(CreditCard creditCard) throws IOException { // Update the Customer Map<String,Object> updateCustomerParams = new HashMap<String,Object>(); addCustomerParams(creditCard, true, updateCustomerParams); // Update the default Card Map<String,Object> updateCardParams = new HashMap<String,Object>(); addCardParams(creditCard, true, updateCardParams); try { Customer customer = Customer.retrieve(creditCard.getProviderUniqueId(), options); customer.update(updateCustomerParams, options); CustomerCardCollection cards = customer.getCards(); Card defaultCard = cards.retrieve(customer.getDefaultCard(), options); defaultCard.update(updateCardParams, options); } catch(StripeException e) { ConvertedError converted = convertError(e); // TODO: Throw ErrorCodeException to provide more details throw new LocalizedIOException(e, accessor, "MerchantServicesProvider.updateCreditCardNumberAndExpiration.notSuccessful"); } }
private static Map<String,Object> makeCardParams( CreditCard creditCard, boolean update, String cardNumber, byte expirationMonth, short expirationYear, String cardCode ) { Map<String,Object> cardParams = new HashMap<String,Object>(); addParam(update, cardParams, "number", CreditCard.numbersOnly(cardNumber)); addParam(update, cardParams, "exp_month", expirationMonth); addParam(update, cardParams, "exp_year", expirationYear); addParam(update, cardParams, "cvc", cardCode); addCardParams(creditCard, update, cardParams); return cardParams; }
/** * Meta data contains both card meta data (also associated with "customer" for stored cards) and transaction meta data. * https://stripe.com/docs/api#create_charge */ private static Map<String,Object> makeMetadata(TransactionRequest transactionRequest, CreditCard creditCard, boolean update) { Map<String,Object> metadata = makeMetadata(creditCard, update); // Additional customer meta data addMetaData(update, metadata, "customer_description", creditCard.getComments(), true); addMetaData(update, metadata, "customer_email", creditCard.getEmail(), false); // Transaction meta data addMetaData(update, metadata, "customer_ip", transactionRequest.getCustomerIp(), false); addMetaData(update, metadata, "order_number", transactionRequest.getOrderNumber(), false); addMetaData(update, metadata, "amount", transactionRequest.getAmount(), false); addMetaData(update, metadata, "tax_amount", transactionRequest.getTaxAmount(), false); addMetaData(update, metadata, "tax_exempt", transactionRequest.getTaxExempt(), false); addMetaData(update, metadata, "shipping_amount", transactionRequest.getShippingAmount(), false); addMetaData(update, metadata, "duty_amount", transactionRequest.getDutyAmount(), false); addMetaData(update, metadata, "shipping_company_name", transactionRequest.getShippingCompanyName(), true); addMetaData(update, metadata, "invoice_number", transactionRequest.getInvoiceNumber(), false); addMetaData(update, metadata, "purchase_order_number", transactionRequest.getPurchaseOrderNumber(), false); return metadata; }
/** https://stripe.com/docs/api#create_charge */ private static Map<String,Object> makeShippingParams(TransactionRequest transactionRequest, CreditCard creditCard, boolean update) { Map<String,Object> shippingParams = new HashMap<String,Object>(); addParam(update, shippingParams, "address", makeShippingAddressParams(transactionRequest, update)); addParam(update, shippingParams, "name", CreditCard.getFullName(transactionRequest.getShippingFirstName(), transactionRequest.getShippingLastName())); // Phone cannot be in the shipping by itself if(!shippingParams.isEmpty()) addParam(update, shippingParams, "phone", creditCard.getPhone()); // Unused: tracking_number return shippingParams; } // </editor-fold>
private static ConvertedError convertError(StripeException e) { if(e instanceof AuthenticationException) { return new ConvertedError( TransactionResult.CommunicationResult.GATEWAY_ERROR, null, TransactionResult.ErrorCode.PROVIDER_CONFIGURATION_ERROR, getMessage(e), null ); InvalidRequestException ire = (InvalidRequestException)e; String param = ire.getParam(); return new ConvertedError( TransactionResult.CommunicationResult.GATEWAY_ERROR, param, TransactionResult.ErrorCode.PROVIDER_CONFIGURATION_ERROR, getMessage(e), null ); return new ConvertedError( TransactionResult.CommunicationResult.IO_ERROR, null, TransactionResult.ErrorCode.ERROR_TRY_AGAIN, getMessage(e), null ); return new ConvertedError( communicationResult,
@Override public void updateCreditCardExpiration( CreditCard creditCard, byte expirationMonth, short expirationYear ) throws IOException { // Update the default Card Map<String,Object> updateParams = new HashMap<String,Object>(); addParam(true, updateParams, "exp_month", expirationMonth); addParam(true, updateParams, "exp_year", expirationYear); try { Customer customer = Customer.retrieve(creditCard.getProviderUniqueId(), options); CustomerCardCollection cards = customer.getCards(); Card defaultCard = cards.retrieve(customer.getDefaultCard(), options); defaultCard.update(updateParams, options); } catch(StripeException e) { ConvertedError converted = convertError(e); // TODO: Throw ErrorCodeException to provide more details throw new LocalizedIOException(e, accessor, "MerchantServicesProvider.updateCreditCardExpiration.notSuccessful"); } }
/** https://stripe.com/docs/api#create_charge */ private static Map<String,Object> makeShippingAddressParams(TransactionRequest transactionRequest, boolean update) { Map<String,Object> shippingAddressParams = new HashMap<String,Object>(); addParam(update, shippingAddressParams, "line1", transactionRequest.getShippingStreetAddress1()); addParam(update, shippingAddressParams, "line2", transactionRequest.getShippingStreetAddress2()); addParam(update, shippingAddressParams, "city", transactionRequest.getShippingCity()); addParam(update, shippingAddressParams, "state", transactionRequest.getShippingState()); addParam(update, shippingAddressParams, "postal_code", transactionRequest.getShippingPostalCode()); addParam(update, shippingAddressParams, "country", transactionRequest.getShippingCountryCode()); return shippingAddressParams; }
@Override public void deleteCreditCard(CreditCard creditCard) throws IOException { try { Customer customer = Customer.retrieve(creditCard.getProviderUniqueId(), options); if(customer.getDeleted() == null || !customer.getDeleted()) { DeletedCustomer deletedCustomer = customer.delete(options); } } catch(StripeException e) { ConvertedError converted = convertError(e); // TODO: Throw ErrorCodeException to provide more details throw new LocalizedIOException(e, accessor, "MerchantServicesProvider.deleteCreditCard.notSuccessful"); } } }
private static Map<String,Object> makeCardParams(CreditCard creditCard, boolean update) { return makeCardParams( creditCard, update, creditCard.getCardNumber(), creditCard.getExpirationMonth(), creditCard.getExpirationYear(), creditCard.getCardCode() ); }
@Override public SaleResult sale(TransactionRequest transactionRequest, CreditCard creditCard) { AuthorizationResult authorizationResult = saleOrAuthorize(transactionRequest, creditCard, true); return new SaleResult( authorizationResult, new CaptureResult( authorizationResult.getProviderId(), authorizationResult.getCommunicationResult(), authorizationResult.getProviderErrorCode(), authorizationResult.getErrorCode(), authorizationResult.getProviderErrorMessage(), authorizationResult.getProviderUniqueId() ) ); }
/** https://stripe.com/docs/api#metadata */ private static Map<String,Object> makeMetadata(CreditCard creditCard, boolean update) { Map<String,Object> metadata = new LinkedHashMap<String,Object>(); addMetaData(update, metadata, "company_name", creditCard.getCompanyName(), true); addMetaData(update, metadata, "phone", creditCard.getPhone(), true); addMetaData(update, metadata, "fax", creditCard.getFax(), true); addMetaData(update, metadata, "customer_id", creditCard.getCustomerId(), true); addMetaData(update, metadata, "customer_tax_id", creditCard.getCustomerTaxId(), true); return metadata; }
/** https://stripe.com/docs/api#update_card */ private static void addCardParams( CreditCard creditCard, boolean update, Map<String,Object> cardParams ) { addParam(update, cardParams, "name", CreditCard.getFullName(creditCard.getFirstName(), creditCard.getLastName())); addParam(update, cardParams, "address_line1", creditCard.getStreetAddress1()); addParam(update, cardParams, "address_line2", creditCard.getStreetAddress2()); addParam(update, cardParams, "address_city", creditCard.getCity()); addParam(update, cardParams, "address_zip", creditCard.getPostalCode()); addParam(update, cardParams, "address_state", creditCard.getState()); addParam(update, cardParams, "address_country", creditCard.getCountryCode()); }
@Override public CaptureResult capture(AuthorizationResult authorizationResult) { String id = authorizationResult.getProviderUniqueId(); try { authorizationResult.getProviderUniqueId(); Charge ch = Charge.retrieve(id, options); ch.capture(options); return new CaptureResult( providerId, TransactionResult.CommunicationResult.SUCCESS, null, null, null, id ); } catch(StripeException e) { ConvertedError converted = convertError(e); return new CaptureResult( providerId, converted.communicationResult, converted.providerErrorCode, converted.errorCode, converted.providerErrorMessage, id ); } }