Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #150 from commercetools/149-transaction-workflow-c…
Browse files Browse the repository at this point in the history
…hange

149: execute payment only if there are no successful transactions of type "Charge"
  • Loading branch information
JudeNiroshan authored Nov 25, 2019
2 parents dd52b02 + c80e2bc commit 90a4939
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,16 @@ public CompletionStage<PaymentHandleResponse> executePayment(@Nonnull String pay
format("Payment not found for interfaceid=[%s]", paypalPlusPaymentId))
);
} else {
return executePaymentAndCreateTxn(paypalPlusPaymentId, paypalPlusPayerId, ctpPayment);
boolean isAlreadyCharged = ctpPayment.getTransactions().stream()
.filter(transaction -> transaction.getType().equals(TransactionType.CHARGE))
.anyMatch(transaction -> transaction.getState().equals(SUCCESS));
if (isAlreadyCharged) {
// Should not charge for already charged transactions
return CompletableFuture.completedFuture(PaymentHandleResponse
.ofHttpStatus(HttpStatus.CREATED));
} else {
return executePaymentAndCreateTxn(paypalPlusPaymentId, paypalPlusPayerId, ctpPayment);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@
import com.commercetools.pspadapter.tenant.TenantConfig;
import com.commercetools.pspadapter.tenant.TenantConfigFactory;
import com.commercetools.service.paypalPlus.PaypalPlusPaymentService;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.paypal.api.payments.*;
import com.paypal.api.payments.Payment;
import com.paypal.api.payments.Transaction;
import io.sphere.sdk.carts.Cart;
import io.sphere.sdk.carts.CartDraft;
import io.sphere.sdk.carts.CartDraftBuilder;
import io.sphere.sdk.carts.commands.CartCreateCommand;
import io.sphere.sdk.carts.commands.CartUpdateCommand;
import io.sphere.sdk.carts.commands.updateactions.AddPayment;
import io.sphere.sdk.client.SphereClient;
import io.sphere.sdk.payments.PaymentDraftBuilder;
import io.sphere.sdk.payments.PaymentMethodInfoBuilder;
import io.sphere.sdk.commands.UpdateAction;
import io.sphere.sdk.payments.*;
import io.sphere.sdk.payments.commands.PaymentCreateCommand;
import io.sphere.sdk.payments.commands.PaymentUpdateCommand;
import io.sphere.sdk.payments.commands.updateactions.AddTransaction;
import io.sphere.sdk.payments.commands.updateactions.SetInterfaceId;
import io.sphere.sdk.payments.queries.PaymentByIdGet;
import io.sphere.sdk.types.CustomFieldsDraftBuilder;
import org.bitbucket.radistao.test.annotation.AfterAllMethods;
Expand All @@ -41,6 +47,9 @@
import org.springframework.http.HttpStatus;

import javax.annotation.Nonnull;
import javax.money.MonetaryAmount;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -202,13 +211,53 @@ public void shouldCreateChargeTransaction() {
}

@Test
public void whenPaypalPaymentIsNotApproved_transactionShouldNotBeCreated() {
CtpPaymentWithCart ctpPaymentWithCart = createCartWithPayment();
PaymentHandler paymentHandler = paymentHandlerProvider.getPaymentHandler(MAIN_TEST_TENANT_NAME).get();
public void whenCtpPaymentIsAlreadyMarkedAsSuccess_shoudNotCallCreateTransactionInPaypalPlus() {
final CtpPaymentWithCart ctpPaymentWithCart = createCartWithPayment();
final PaymentHandler paymentHandler = paymentHandlerProvider
.getPaymentHandler(MAIN_TEST_TENANT_NAME)
.get();

io.sphere.sdk.payments.Payment payment = ctpPaymentWithCart.getPayment();

final TransactionDraft transactionDraft = TransactionDraftBuilder
.of(CHARGE, Money.of(200, EUR)).state(TransactionState.SUCCESS)
.build();
final AddTransaction addTransaction = AddTransaction
.of(transactionDraft);
final SetInterfaceId interfaceIdUpdateAction = SetInterfaceId.of("testInterfaceId");
List<UpdateAction<io.sphere.sdk.payments.Payment>> actions = new ArrayList<>();
actions.add(addTransaction);
actions.add(interfaceIdUpdateAction);

final io.sphere.sdk.payments.Payment updatedPayment = this
.sphereClient
.execute(PaymentUpdateCommand.of(payment, actions))
.toCompletableFuture()
.join();

final String ctpPaymentId = updatedPayment.getId();

String ctpPaymentId = ctpPaymentWithCart.getPayment().getId();
executeBlocking(paymentHandler.createPayment(ctpPaymentId));

PaymentHandleResponse response = executeBlocking(paymentHandler
.executePayment(updatedPayment.getInterfaceId(), "testPayerId"));

io.sphere.sdk.payments.Payment ctpPayment = executeBlocking(sphereClient
.execute(PaymentByIdGet.of(ctpPaymentId)));
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED.value());
assertThat(ctpPayment.getTransactions()).hasSize(1);
}


@Test
public void whenPaypalPaymentIsNotApproved_transactionShouldNotBeCreated() {
final CtpPaymentWithCart ctpPaymentWithCart = createCartWithPayment();
final PaymentHandler paymentHandler = paymentHandlerProvider
.getPaymentHandler(MAIN_TEST_TENANT_NAME)
.get();
final String ctpPaymentId = ctpPaymentWithCart.getPayment().getId();

executeBlocking(paymentHandler.createPayment(ctpPaymentId));
io.sphere.sdk.payments.Payment ctpPayment = executeBlocking(this.sphereClient.execute(PaymentByIdGet.of(ctpPaymentId)));

PaymentHandleResponse response = executeBlocking(paymentHandler.executePayment(ctpPayment.getInterfaceId(), "invalidTestPayerId"));
Expand Down

0 comments on commit 90a4939

Please sign in to comment.