=======
promised-payments
Setup
we need node version > v4.2.0
npm init
npm install stripe --save
npm install bluebird --save
npm install chai chai-as-promised mocha mocha-jenkins-reporter --save-dev
- Go to stripe and create a free account
- Your Account - Setting - API Keys (TEST)
- In your shell write this
export STRIPE_SECRET_KEY="YOUR_TEST_SECRET_KEY"
- Close settings pop-up and go to plans
- Create two plans
- 20 usd monthly subscription
- 200 usd yearly subscription
Design
Customer has many Orders Orders belongs to Customer Order has many details Detail belongs to Order Detail has one Product
Services Billing Service has one payment gateway (this time is coupled to Stripe) but is testable enough, you can use the adapter pattern to be gateway agnostic so you can change it with whatever payment gateway you want.
Models
- mkdir lib/models
- create lib/models/customer.js
- create lib/models/product.js
- create lib/models/detail.js
- create lib/models/order.js
- implement Resource
save
andremove
with promises, remove should always fail and save should always return the object itself e.gcustomer.save()
- implement order has many details, create a
addDetail
method which adds a detail to orders and then save it - In Order model create a method to calculate the total price of an order and call it
totalPrice
- Unit test order
save
addDetail
andremove
with this fixtures. - Add acceptance test to full-flow emulating an API:
- STEPS:
- CREATE 5 Customers and save them
- CREATE 3 Products and save them
- CREATE Details based on Products and save them
- CREATE 5 Orders each with 1 customer and 1 random detail
- Orders should have non-empty details
- Orders should belong to customer
Payments
- create lib/services/billing/payment.js
- add methods to our billing service see stripe api docs
createToken(creditCard)
createAccountToCustomer(creditCard, customer)
this should callcreateToken
thencreateAccount
in a promised waycreateAccount(customer, stripeToken)
createSubscription(customer)
refund(customer, order)
charge(customer, order)
getCharges(customer)
- Add payment-flow acceptance tests
- STEPS:
- CREATE 5 Customers and save them
- CREATE a stripe account to each customer (tip: use
createAccountToCustomer
function). - SAVE all created customers with account
- All customers should have a stripe account
- We can charge 20usd to each account
- We can create stripe subscription on those who have a plan
For home
- Implement and Test customer's order
refund
- how can we implement shipping? tip (address model and refactor order's total price calculation)
And that's how we implement a mini-ecommerce Domain Logic, we just need to setup anything to handle request (plain node, express, sails, hapi, meteor etc) and a DB (SQL/NoSQL/whatever), then plug your Domain Logic with them and just it, we are Server/DB agnostic.
Tips:
- Do not broke Promise chain
- Always return the same result type
- If you're already in a promise do not try to avoid it, just keep the flow
- Always try to compose Promises e.g (
createAccountToCustomer
) - Prefer little composable/testable promises
- We are not handling errors here but you MUST do it
- Always TEST your code
- Use real interactions in acceptance tests when dealing with money you don't want to extra charge or nor charge at all your customers.