I'm having a financial related service that enable our users to topup their balance in our system, and then disburse the money. They'll be charged for each disbursement.
The problem is, I'm having a need to do a series of step that need to be done in several places.
Related component:
Transaction : record of the transaction info (amount, beneficiary account, etc)
Payment : record of the payment info (amount, payment status)
Journal Entries : record the flow of money for every transaction and payment (for every movement of money in our system, we track which account act as the debit account and which as the credit account, like in accounting)
Flow of the business process:
Suppose there's a transaction and payment already created, next step is to confirm the transaction
Change transaction status to confirmed
Change payment status to confirmed
Insert new record to journal entries, with debit and credit account specified specifically for this process
Increment or decrement the balance of the credit and debit account
Those series of step might be done in several places, so I try to find a way to prevent other programmer that try to implement this flow forgetting a step or doing it wrong.
What I do now:
Create a journal entry helper. Consist of a lot of static function for each of possible business process that involve moving money between accounts. For example in this question, when confirming a payment, it moved money from user_deposit to unearned_revenue account, so user_deposit will be the debit account, and unearned_revenue will be the credit account in the newly inserted journal entry. Each of this static function only differ in telling which account will be the credit account and which will be the debit account. Beside that, this helper will also decrement or increment the balance of the credit and debit account. So in this example, it will decrement user_deposit balance and increment unearned_revenue balance. Example function name related to this example: withdrawPayment and withdrawTransaction
Create a payment helper. Different payment type, in different state, will need different journal entry helper. So there are a lot of static function in this helper that change the payment status, and then call the related journal entry helper. Example function name related to this example: confirmAndWithdrawCashPayment
Create an instance method on the transaction named like confirmAndwithdraw, which will call confirmAndWithdrawCashPayment on payment helper and withdrawTransaction on journal entry helper, which will call withdrawPayment on journal entry helper.
Whenever a programmer need to do this step, they just call the confirmAndwithdraw function from the transaction object
This'll work of course, but I also know that this is a very bad design. Any suggestion of a design pattern or any solution suitable for this case?
Mainly, I'm looking for a way to wrap a series of steps so that I can remove those helper class while also providing a convenient way for other programmers to implement this business process.
Sorry for the long question. If the question is not clear, please do tell me so I can provide more information.
In scenarios like this where instantiating a type requires a certain order of steps I recommend taking a look at the Builder Pattern. This way you can chain the required steps and execute them at any time (deferred) by storing an intermediate state of the object. When done you would call build() an receive the initialized instance. Each method of the builder can return a different object so that you can force the client to use only a selection of methods depended on the prior invoked operation.
Could look like this:
PaymentBuilder paymentBuilder = new PaymentBuilder();
paymentBuilder.OpenTransaction();
// PaymentBuilder e.g. has only the OpenTransaction() method which returns a
// PaymentTransactionOptions instance. This instance contains the
// state of OpenTransaction() and adds further information by
// invoking its SetPayment() method.
paymentBuilder.OpenTransaction(args).SetPaymentMethod(moreArgs);
// SetPaymentMethod(moreArgs) returns a PaymentJournalOptions instance
// that contains the prior collected information and a method SetJournalData().
paymentBuilder.OpenTransaction(args).SetPaymentMethod(moreArgs).SetJournalData(journalArgs);
You continue like this until your transaction data is complete for execution. Then you invoke (on the last result object) build() to retrieve the build instance which may exposes a method 'commit()'.
Instead of chaining the method calls you could store the intermediate result and pass it around and complete it at any point in code you like.
Related
For our use case, we have an existing form that captures the customer's credit card information. To smooth out the transition from one payment processor to the next since we're not sure when that will happen, we would like to vault the payment method in Braintree without charging the card but keep our existing form.
I see how to vault the nonce that we receive from the Drop-In UI, and I was able to find an answer on SO that described how to pass the payment information directly to Braintree and charging it, however, I have had no luck in finding a way to just pass the card information to braintree for the purposes of vaulting the payment method (with or without the intermediate step of receiving a nonce).
EDIT:
I've discovered the $gateway->customer()->create() and $gateway->creditCard()->create() functions, however, of the various 'unique identifiers' returned from the credit card create call, none of them appear to be vaultable.
It was buried a bit in the documentation, but I was able to do this with an intermediate step:
create customer $gateway->customer()->create(...)
create credit card $gateway->creditCard()->create(...)
take the token from the previous call and pass it to $gateway->paymentMethodNonce()->create(..)
then take the customer id and the newly created nonce and passed them to $gateway->paymentMethod()->create(...)
the necessary vault token was stored in the results object $result->paymentMethod->graphQLId
I'm developing a website (with payments) that needs to support multiple payment gateways. I'm going to use omnipay package (thanks God there is a package to handle this) but now I was wondering what is the best way to store all the payment information in the database without binding it to a specific gateway.
My first idea is to have the following tables:
gateway(gateway_id, gateway_name,...)
payment(payment_id, payment_amount,...)
transaction(transaction_id, gateway_id, payment_id, transaction_reference, transaction_type transaction_status, transaction_request_data,
transaction_response_data...).
The type can be something like 'authorize', 'purchase', 'refund', 'void', etc and the status something like 'successful', 'failed', 'pending', etc.
In this way, I can have a list of gateways (Paypal, Stripe, TargetPay, WorldPay) and each payment can have multiple transactions (a payment attempt can fail at the first time and then be attempted again and work but then voided or refunded) but one transaction belongs to a single payment. Each transaction was executed using a specific gateway and this will also be stored.
I don't know if this is the best approach (maybe it's too simple?) and I would like to hear some other ideas.
Thanks!
Your initial structure is a good start. Other things that I would include are:
responses, which would hold the response messages sent back from the gateway in response to your purchase calls
callbacks. An increasing number of gateways supply POST messages on the notify url to the caller. These indicate change of status messages from the gateway such as cancelled transactions or chargebacks
refunds and voids, which I would store in a single table.
responses to the callbacks, refunds and voids, which you could store in the same table as the regular responses if you structured it correctly
Some of the data you would probably have to store as JSON blobs because the structure of messages from each gateway would be different. Some of the data you may want to encrypt, for example if it contains data that could identify a customer or perhaps a credit card
I believe the best answer is: it depends on how close the data provided / required by the gateway provided to each other. If these are close to each other, or you can map the various status types, then store all of these in a single transactions table. If not, then you need to maintain separate tables for each payment gateways.
However, even in the latter case I would have a single transactions table that consolidates the most important and common data accross all providers, such as amount, payment status, and store only the vendor specific data in their corresponding tables.
I'm developing an e-commerce website using PHP and integrating paypal for processing the credit card payments.
I found APIs in PHP that are used to make payments and capture transaction id but none of these discussed about adding adjustments to the amount that is captured.
My use cases include:
Adding tip later
Adding time extension adjustments
Returning only portion of the amount if order gets cancelled
and few more of similar kind
May anyone tell me how this kind of a task is handled with paypal and also is there any API that will provide this functionality as well?
References Transactions would work fine to charge a separate transaction for the tip, but then the customers would see two separate charges on their credit card and you'll probably get lots of questions and confused people wondering why their card was hit twice. As obvious as it seems, I've been there.
The best way to handle it would be to run an Authorization when you first run the card. Authorize it for something like 50% above the ticket total, or whatever you think it would take to cover the final total including a tip for that order.
If the order is canceled for any reason you simply Void the authorization. If you're ready to actually process the funds with a final total including tip, then you can Capture the amount that you need up to the amount that you originally authorized. I believe you may even be able to capture up to 10% more than the original authorization, but I'm not 100% sure on that.
The thing you would need to be careful about is authorizing a card for some large amount and then not voiding or capturing shortly after. The authorization is only guaranteed for 3 days. You can run a re-authorization and get an additional 3 days if you need to.
If the order gets canceled and you don't void it then you'll get complaints that a large amount is "charged" on their account. It looks like a charge until it clears back or is voided. If you don't void it then it can take up to 29 days for them to get access to those funds on their account again, so you want to avoid those types of complaints too.
You would need Payments Pro to handle credit cards directly for that sort of a system. Depending on which version of Pro they put you on you'll use either the PayFlow API, or you'll use variations of DoDirectPayment, DoVoid, DoCapture, DoReauthorization.
Reference transactions would work for what you are trying to do. Reference transactions allows you to reference a previous transaction to run another transaction on that information at a later date. Here is a link to more information on reference transactions. https://developer.paypal.com/webapps/developer/docs/classic/express-checkout/integration-guide/ECReferenceTxns/
I'm trying to come up with a generic way to add multiple implementations of payment providers in a system that only uses one for now.
I'd like to have a Company object linked to a Subscription. A Subscription has multiple Invoice objects connected to it. A Subscription can only have one PaymentProviderInterface linked to it but we have to option to replace the payment provider for the subscription. The PaymentProviderInterface linked to the Subscription is build by a Factory.
The problem is that for different PaymentProviderInterface implementations the logic to get the user to be notified might be different. For a special PaymentProviderInterface an email must me send to our system instead of the actual end user. For Stripe the email should be send to the user connected to the Stripe account. For another service the user is stored in the Company object.
Is there a way to implement this without the PaymentProvider object having to actually know about the Subscription it's linked to?
The same thing for the BillingAddress. The way we retrieve the billing address to put on the Invoice object is different per payment provider. If it's Stripe, it's the billing address stored on Stripe. If it's another one, the BillingAddress is stored in our database.
You should be able to define custom arguments passed to the payment provider as parameters of the query and returned to your server "as it" after a payment attempt (wheter it is a successfull or failed attempt doesn't impact the presence of these custom args). When your Factory elect the right PaymentProviderInterface for the Subscription, it can then select the User using the logic you want and pass it as parameter to the PaymentProvider (as a string), then the server can know who to notify based on what's returned by the PaymentProvider by parsing the content of this custom arguments field. Custom Arguments in PaymentProviders are usualy used for this kind of purpose.
Does this makes sense ?
I guess that the concept of Observer design pattern is one of the possible solutions that you're after for. Since it main purpose is to ensure a way of notifying change to a number of classes. It defines a dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
You can consider following implementation:
Subject (Subscription)
knows its observers. Any number of Observer objects may observe a subject
provides an interface for attaching and detaching Observer objects.
ConcreteSubject (ConcreteSubscription)
stores state of interest to ConcreteObserver
sends a notification to its observers when its state changes
Observer (IPaymentProvider)
defines an updating interface for objects that should be notified of changes in a subject.
ConcreteObserver (PaymentProvider)
maintains a reference to a ConcreteSubject object
stores state that should stay consistent with the subject's
implements the Observer updating interface to keep its state consistent with the subject's
That will be a little hard do explain, but let me try:
I'm building an online store using Laravel 4 and now I have to create the payment methods and they might be completely different from each other and for each one of them I'll have to build a different number of pages (views), wich could be, as examples:
Credit Card
Get the credit card data (to, first, create a token).
Nice! It was accepted by the company and your token was created, would you really like to pay?
It was paid, thanks!
Credit Card
Get the debit card data.
It was paid, thanks!
Paypal
Jump to paypal site and wait for it to get back.
Stripe
Gosh, we still doesn't have it in Brazil. :(
Billet (Banking) I think this is something we only have in Brazil. It's is a document (like a bill or a fracture, here's an example: Billet) you can print at home (it has a barcode) and pay at your bank (online or in person). Payment is received in our bank account a couple of days after payment, so there's nothing we can do, just show the document and wait for a payment that may never happen:
Just open a new window with it, customer may print it or not.
And, of course, for each step there might be decisions to make, problems, error messages, retries in case of errors (credit card may say "try again" and I must ask the user if he wants to do it).
So how would you achitecture this, in terms of (mainly) views, controllers and services (or repositories or libraries...), the way we can at anytime add more payment methods to the list. Are there any Design Patterns (to add to the Repository Pattern) wich would help to design this kind of interaction?
If you want to save the request in a queue you can use Command pattern.
As per my understanding in this scenario after getting the credit and debit card detail we will swipe then we will jump paypal site then we will have the Billet these are sequence activity or these are request queue here we can use Command pattern and for the different pages of your credit card ,debit card etc we can use factory class to get the instance of credit and debit cards etc.
Modifications are welcomed.