Search code examples
javapaypal-sandbox

How do I set the name of the store in the paypal API?


I'm able to make payments using the paypal api, but when I looked at the receipt, the name of the store is "Test Store", is there a way to change this?

enter image description here

I tried changing the name on my business information page, but it still appears as "Test Store".

enter image description here

Here's what my code looks like:

public class PaypalPaymentClient {

    private final String paypalClientId;
    private final String paypalClientSecret;
    private final CreditBundleRepository creditBundleRepository;

    //TODO: Refactor redirect urls.
    public PaypalPaymentClient(String paypalClientId, String paypalClientSecret, CreditBundleRepository creditBundleRepository) {
        this.paypalClientId = paypalClientId;
        this.paypalClientSecret = paypalClientSecret;
        this.creditBundleRepository = creditBundleRepository;
    }

    public PaypalCompletePaymentResponse completePayment(PaypalCompletePaymentRequest request) throws PaypalPaymentException {
        Payment payment = new Payment();
        payment.setId(request.getPaymentId());

        PaymentExecution paymentExecution = new PaymentExecution();
        paymentExecution.setPayerId(request.getPayerId());

        PaypalCompletePaymentResponse response = new PaypalCompletePaymentResponse();
        try {
            APIContext context = new APIContext(paypalClientId, paypalClientSecret, "sandbox");
            Payment createdPayment = payment.execute(context, paymentExecution);
            if(createdPayment != null) {
                log.info("Successfully completed paypal payment with payment ID: {}", createdPayment.getId());
                response.setStatus("success");
            }
        } catch (PayPalRESTException exception) {
            log.error("Failed to complete paypal payment due to exception: {}", exception);
            throw new PaypalPaymentException("Failed to complete paypal payment.");
        }

        return response;
    }

    public PaypalPaymentResponse createPayment(PaypalPaymentRequest paymentRequest) throws PaypalPaymentException {
        PaypalPaymentResponse response = new PaypalPaymentResponse();
        Payment payment = getPayment(paymentRequest);
        try {

            String redirectUrl = "";
            APIContext context = new APIContext(paypalClientId, paypalClientSecret, "sandbox");
            Payment createdPayment = payment.create(context);

            if(createdPayment != null) {
                List<Links> links = createdPayment.getLinks();
                for(Links link:links) {
                    if(link.getRel().equals("approval_url")) {
                        redirectUrl = link.getHref();
                        break;
                    }
                }

                response.setStatus("success");
                response.setRedirectUrl(redirectUrl);
            }
        } catch (PayPalRESTException exception) {
            log.error("Failed to create paypal payment due to exception: {}", exception);
            throw new PaypalPaymentException("Failed to create paypal payment.");
        }

        return response;
    }

    private Payment getPayment(PaypalPaymentRequest paymentRequest) throws PaypalPaymentException {
        Amount amount = new Amount();
        amount.setCurrency(paymentRequest.getCurrency().toString());
        amount.setTotal(paymentRequest.getTotalCost().toString());

        Transaction transaction = new Transaction();
        transaction.setAmount(amount);
        transaction.setDescription(getCreditBundleName(paymentRequest.getCreditBundleId()));

        List<Transaction> transactionList = new ArrayList<>();
        transactionList.add(transaction);

        Payer payer = new Payer();
        payer.setPaymentMethod("paypal");

        Payment payment = new Payment();
        payment.setIntent("sale");
        payment.setPayer(payer);
        payment.setTransactions(transactionList);

        RedirectUrls redirectUrls = new RedirectUrls();
        redirectUrls.setCancelUrl("http://localhost:4200/buy-credits");
        redirectUrls.setReturnUrl("http://localhost:4200/complete-payment");

        payment.setRedirectUrls(redirectUrls);
        return payment;
    }

    private String getCreditBundleName(UUID creditBundleId) throws PaypalPaymentException {
        log.info("Finding credit bundle: {}", creditBundleId);
        Optional<CreditBundle> creditBundleOptional = creditBundleRepository.findById(creditBundleId);

        if(creditBundleOptional.isPresent()) {
            return creditBundleOptional.get().getName();
        }

        throw new PaypalPaymentException("Credit bundle not found.");
    }

}

Solution

  • It appears you're using a 10-year-old, deprecated PayPal API (v1/payments)

    If this is a new integration, you should utilize v2/checkout/orders instead. See the standard checkout integration guide, which also covers using the JS SDK for approval instead of redirecting away from your site.

    At the time of this writing there are no supported SDKs for any PayPal APIs; instead, direct HTTPS integrations are done by first obtaining an access_token (which can be cached for up to 9 hours) and then posting operations in JSON format for requests and responses. The v2/checkout/orders API parameter to override the business name is:

      "payment_source": {
        "paypal": {
          "experience_context": {
            "brand_name": "EXAMPLE INC",