MishkaPay.Test.PayPal (Mishka Pay v0.0.1)

Copy Markdown View Source

A stand-in for PayPal's REST API for a project's tests: it answers in PayPal's own shapes (Orders v2, captures, refunds, catalog products, billing plans, subscriptions and their transactions, OAuth, webhook verification), so a host tests its PayPal flows with no keys and no network. Mishka Pay's own PayPal tests run on it, and its sandbox tests check it against PayPal (mix test --only paypal_live).

setup do
  MishkaPay.Test.start_plugin!(MishkaPay.Providers.PayPal)
  MishkaPay.Test.put_keys("paypal", %{publishable_key: "client_1", secret_key: "secret_1",
    webhook_secret: "WH-1", mode: :test})
  MishkaPay.Test.PayPal.stub!()
end

test "the buyer pays and comes back" do
  {:ok, checkout} = MishkaPay.checkout(%{provider: "paypal", ...})
  MishkaPay.Test.PayPal.order(checkout.id, "APPROVED")
  {:ok, %{payment_id: _paid}} = MishkaPay.fetch_checkout("paypal", checkout.id)
end

order/3 puts an Order in a state (an "APPROVED" one is captured when read; declined: true declines the capture), subscription/3 a subscription ("ACTIVE" with :billing_info and :transactions). A webhook message is verified when its paypal-transmission-sig header is "good". Every answer carries a PayPal-Debug-Id header, "f3a1c2b4d5e60", and a declined capture its information_link, as PayPal's do.

MishkaPay.Test.setup!/0 makes its table. What it holds belongs to the test that called stub!/0, read from any process the test starts (through $callers), so async tests never see each other's. It tells the test every request it got as {:paypal, method, path, headers, body}, and its bytes as {:paypal_raw, path, body}.

Summary

Functions

The plug stub!/0 answers with, for MishkaPay.Test.stub_http/1.

Keeps the Order id for this test, in PayPal's status (APPROVED, COMPLETED...) with opts.

Answers this test's PayPal requests with the stand-in, holding what it makes for this test.

Puts the subscription id made earlier (or a new one) in PayPal's status (ACTIVE, SUSPENDED...): :billing_info and :transactions as PayPal would answer them.

Functions

forward(conn)

The plug stub!/0 answers with, for MishkaPay.Test.stub_http/1.

order(id, status, opts \\ [])

Keeps the Order id for this test, in PayPal's status (APPROVED, COMPLETED...) with opts.

stub!()

@spec stub!() :: :ok

Answers this test's PayPal requests with the stand-in, holding what it makes for this test.

subscription(id, status, opts \\ [])

Puts the subscription id made earlier (or a new one) in PayPal's status (ACTIVE, SUSPENDED...): :billing_info and :transactions as PayPal would answer them.