CAPI-297: Cart to payment and refund (#329)

* added cart to payment and refund

* fixed

* review fix

* updated swag

* added requested changes

* nano

* added cart to dummny handler match

* Add missing throw handling

* Fix
This commit is contained in:
Артем 2019-11-27 11:01:03 +03:00 committed by Sergey Yelin
parent a56643e878
commit c1d3df222c
5 changed files with 70 additions and 21 deletions

View File

@ -196,16 +196,19 @@ process_request('CapturePayment', Req, Context) ->
CaptureParams = maps:get('CaptureParams', Req),
InvoiceID = maps:get(invoiceID, Req),
PaymentID = maps:get(paymentID, Req),
CallArgs = [
InvoiceID,
PaymentID,
#payproc_InvoicePaymentCaptureParams{
reason = maps:get(<<"reason">>, CaptureParams),
cash = encode_optional_cash(CaptureParams, InvoiceID, PaymentID, Context)
}
],
Call = {invoicing, 'CapturePaymentNew', CallArgs},
case capi_handler_utils:service_call_with([user_info], Call, Context) of
try
CallArgs = [
InvoiceID,
PaymentID,
#payproc_InvoicePaymentCaptureParams{
reason = maps:get(<<"reason">>, CaptureParams),
cash = encode_optional_cash(CaptureParams, InvoiceID, PaymentID, Context),
cart = capi_handler_encoder:encode_invoice_cart(CaptureParams)
}
],
Call = {invoicing, 'CapturePayment', CallArgs},
capi_handler_utils:service_call_with([user_info], Call, Context)
of
{ok, _} ->
{ok, {202, #{}, undefined}};
{exception, Exception} ->
@ -242,14 +245,16 @@ process_request('CapturePayment', Req, Context) ->
io_lib:format("Max amount: ~p", [PaymentAmount])
)}
end
catch
throw:invoice_cart_empty ->
{ok, logic_error(invalidInvoiceCart, <<"Wrong size. Path to item: cart">>)}
end;
process_request('CreateRefund' = OperationID, Req, Context) ->
InvoiceID = maps:get(invoiceID, Req),
PaymentID = maps:get(paymentID, Req),
RefundParams = maps:get('RefundParams', Req),
Result = create_refund(InvoiceID, PaymentID, RefundParams, Context, OperationID),
case Result of
try create_refund(InvoiceID, PaymentID, RefundParams, Context, OperationID) of
{ok, Refund} ->
{ok, {201, #{}, capi_handler_decoder_invoicing:decode_refund(Refund, Context)}};
{exception, Exception} ->
@ -305,6 +310,9 @@ process_request('CreateRefund' = OperationID, Req, Context) ->
end;
{error, {external_id_conflict, RefundID, ExternalID}} ->
{ok, logic_error(externalIDConflict, {RefundID, ExternalID})}
catch
throw:invoice_cart_empty ->
{ok, logic_error(invalidInvoiceCart, <<"Wrong size. Path to item: cart">>)}
end;
process_request('GetRefunds', Req, Context) ->
@ -515,7 +523,8 @@ create_refund(InvoiceID, PaymentID, RefundParams, #{woody_context := WoodyCtx} =
id = ID,
external_id = ExternalID,
reason = genlib_map:get(<<"reason">>, RefundParams),
cash = encode_optional_cash(RefundParams, InvoiceID, PaymentID, Context)
cash = encode_optional_cash(RefundParams, InvoiceID, PaymentID, Context),
cart = capi_handler_encoder:encode_invoice_cart(RefundParams)
},
Call = {invoicing, 'RefundPayment', [InvoiceID, PaymentID, Params]},
capi_handler_utils:service_call_with([user_info], Call, Context);

View File

@ -211,7 +211,9 @@ decode_stat_payment(Stat, Context) ->
<<"makeRecurrent" >> => capi_handler_decoder_invoicing:decode_make_recurrent(
Stat#merchstat_StatPayment.make_recurrent
),
<<"statusChangedAt">> => decode_status_changed_at(Stat#merchstat_StatPayment.status)
<<"statusChangedAt">> => decode_status_changed_at(Stat#merchstat_StatPayment.status),
<<"cart" >> => capi_handler_decoder_invoicing:decode_invoice_cart(Stat#merchstat_StatPayment.cart)
}, decode_stat_payment_status(Stat#merchstat_StatPayment.status, Context)).
decode_stat_tx_info(undefined) ->
@ -504,7 +506,9 @@ decode_stat_refund(Refund, Context) ->
<<"createdAt">> => Refund#merchstat_StatRefund.created_at,
<<"amount">> => Refund#merchstat_StatRefund.amount,
<<"currency">> => Refund#merchstat_StatRefund.currency_symbolic_code,
<<"reason">> => Refund#merchstat_StatRefund.reason
<<"reason">> => Refund#merchstat_StatRefund.reason,
<<"cart">> => capi_handler_decoder_invoicing:decode_invoice_cart(
Refund#merchstat_StatRefund.cart)
},
decode_stat_refund_status(Refund#merchstat_StatRefund.status, Context)
).

View File

@ -630,11 +630,18 @@ create_refund_idemp_fail_test(Config) ->
-spec create_partial_refund(config()) ->
_.
create_partial_refund(Config) ->
capi_ct_helper:mock_services([{invoicing, fun('RefundPayment', [_, _, _,
#payproc_InvoicePaymentRefundParams{
cash = ?CASH,
cart = ?THRIFT_INVOICE_CART
}
]) -> {ok, ?REFUND} end}], Config),
BenderKey = <<"bender_key">>,
Req = #{
<<"reason">> => ?STRING,
<<"currency">> => ?RUB,
<<"amount">> => ?INTEGER
<<"amount">> => ?INTEGER,
<<"cart">> => ?INVOICE_CART
},
Ctx = capi_msgp_marshalling:marshal(#{<<"params_hash">> => erlang:phash2(Req)}),
capi_ct_helper:mock_services([

View File

@ -67,6 +67,18 @@
external_id = EID
}).
-define(INVOICE_CART, [
#{
<<"taxMode">> => #{
<<"type">> => <<"InvoiceLineTaxVAT">>,
<<"rate">> => <<"10%">>
},
<<"product">> => ?STRING,
<<"price">> => ?INTEGER,
<<"quantity">> => ?INTEGER
}
]).
-define(PAYPROC_INVOICE(Payments), #payproc_Invoice{
invoice = ?INVOICE,
payments = Payments
@ -87,7 +99,18 @@
product = ?STRING,
quantity = ?INTEGER,
price = ?CASH,
metadata = #{?STRING => {obj, #{}}}
metadata = #{?STRING := {obj, #{}}}
}).
-define(THRIFT_INVOICE_CART, #domain_InvoiceCart{
lines = [
#domain_InvoiceLine{
product = ?STRING,
quantity = ?INTEGER,
price = ?CASH,
metadata = #{<<"TaxMode">> := {str, <<"10%">>}}
}
]
}).
-define(INVOICE_TPL, #domain_InvoiceTemplate{

View File

@ -397,7 +397,7 @@ cancel_payment_ok_test(Config) ->
-spec capture_payment_ok_test(config()) ->
_.
capture_payment_ok_test(Config) ->
capi_ct_helper:mock_services([{invoicing, fun('CapturePaymentNew', _) -> {ok, ok} end}], Config),
capi_ct_helper:mock_services([{invoicing, fun('CapturePayment', _) -> {ok, ok} end}], Config),
Req = #{
<<"reason">> => ?STRING
},
@ -406,11 +406,17 @@ capture_payment_ok_test(Config) ->
-spec capture_partial_payment_ok_test(config()) ->
_.
capture_partial_payment_ok_test(Config) ->
capi_ct_helper:mock_services([{invoicing, fun('CapturePaymentNew', _) -> {ok, ok} end}], Config),
capi_ct_helper:mock_services([{invoicing, fun('CapturePayment', [_, _, _,
#payproc_InvoicePaymentCaptureParams{
cash = ?CASH,
cart = ?THRIFT_INVOICE_CART
}
]) -> {ok, ok} end}], Config),
Req = #{
<<"reason">> => ?STRING,
<<"amount">> => 123,
<<"currency">> => ?RUB
<<"amount">> => ?INTEGER,
<<"currency">> => ?RUB,
<<"cart">> => ?INVOICE_CART
},
ok = capi_client_payments:capture_payment(?config(context, Config), Req, ?STRING, ?STRING).