# Handle AllPay third party payment service with Laravel

### Let’s initialize git, it’s a must be!

git init


git clone https://github.com/o-pay/Payment_PHP


### Move SDK Laravel’s app folder

cp Payment_PHP/sdk/AllPay.Payment.Integration.php AllPay/app/


### Create a testing controller

php artisan make:controller PaymentsController


• In .env:

### Use use to replace include

• Delete

• Add it in AllPay.Payment.Integration.php到composer.json file

• In terminal, under AllPay project

• In the PaymentsController, use all those required classes

### Create an API for PaymentsController

• Add new route in api.php under routes folder

### Create a simplest HTML

• You could simply revise the content of default welcome.blade page as follows:

### Simple test

• Now we are on the default page of Laravel, it should change to the simple form we just created
• Check nothing, go summit
• Yes, we successfully arrived payment page

### Create a log

• In order to get what AllPay will send to us after the payment is made, we need to use Log to see what we will receive.
• Is there some place that all requests and responses will have to go through where we could have full accessibility? It seems to be a perfect one for logging
• We could make a middleware, and use Log function of Laravel therein to log whatever it goes through
• Make a middleware, in the terminal under the AllPay project
• 註冊middleware
• In/app/Http/Kernel.php，register the middleware we just made
• Create Log
• Actually, what you might log varies upon different cases

### Create a public url

• We need to get the response from third party, so we need a public url to get it
• We could use ngrok to create the public url
• Install ngrok, please refer to its official website
• Change ngrok to be globally executable
• mv ngrok /usr/local/bin
• Get public url

• In terminal under AllPay project

• Copy the public url produced by ngrok

• We are going to catch the response from AllPay, firstly we need to create a function, and after catching it, we could do things there.
• In PaymentsController, we create a function receive as follows:
• Make an API for it

### Set up client return url

• In .env, if you’ve been following what I taught, there should be this parameter, just simply paste the public url there

### Test payment

• Let’s connect AllPay payment page again
• Account：stageuser001
• Use the testing credit card provided by AllPay
• Number：4311-9522-2222-2222
• Expiry Date：12 / 20 or later than current date
• Security number：222
• After payment, let’s check log to see if there is a response from AllPay, in terminal under AllPay project

• On, we’ve got the response.

### Validation

• So, there is a specific mechanism that only applicable between you and third party payment service
• The validation mechanism is like a formula every column goes in and out will be calculated with which is only applicable between you and third party, if you’ve been paying attention, you should notice that in the last column of the response we’ve received from AllPay.
• If you are interested in the formula in detail, you could check it on AllPay’s website.
• Since in this article, we use AllPay SDK, so we are going to share how to use official SDK to validate the information.

• Firstly, let’s check a class called CheckMacValue in app/AllPay.Payment.Integration.php
• Secondly, you could find a function called generate, and you could check it, which it the formula of the CheckMacValue
• So, we could calculate the received information with this formula, and it should exactly the same as what we’ve got from third party
• Let’s get all those information except for CheckMacValue from AllPay, we could use the following code.

• And then, we assign a variable with the CheckMacValue we’ve got from AllPay

Then, we calculate the \$parameters with function generate to produce correct CheckMacValue

• Finally, we compare if two values are identical, if so, it proves the validity of its source. If not, we shouldn’t give any credibility to this information

### What’s next?

• After validating the source, we could do things accordingly
• For example, if payment is successfully made, what we are going to do, and if not, what then?

• At the end, don’t forget to return ‘1|OK’ to let AllPay knows that we’ve received the message.

### Those I didn’t mentioned

• With the testing buyer account provided by AllPay, except for credit card, is also supports multiple payment method
• With convenient store pay or bank transferring method, you could login with a testing backed account provided by AllPay, which could simulate making the payment.
• Account：StageTest

### Refund

• Refund example as follows:

• You could refer to official document for required parameters