Move the example in the SDK package to our Controller as follows:
/** * */ //SDK address(You could manage it yourself) include('AllPay.Payment.Integration.php'); try { $obj = newAllInOne();
//Service parameters $obj->ServiceURL = "https://payment-stage.opay.tw/Cashier/AioCheckOut/V5"; //Service location $obj->HashKey = '5294y06JbISpM5x9' ; //Testing Hashkey, in real case, please use the one provided by AllPay $obj->HashIV = 'v77hoKGq4kWxNNIS' ; //Testing HashIV, in real case, please use the one provided by AllPay $obj->MerchantID = '2000132'; //Testing MerchantID, in real case, please use the one provided by AllPay $obj->EncryptType = EncryptType::ENC_SHA256; //CheckMacValue encrypted type, please stay 1, using SHA256
//Basic parameters(It depends on your need) $MerchantTradeNo = "Test".time();
$obj->Send['ReturnURL'] = 'http://localhost/simple_ServerReplyPaymentStatus.php' ; //The URL AllPay will return after the payment is paid $obj->Send['MerchantTradeNo'] = $MerchantTradeNo; $obj->Send['MerchantTradeDate'] = date('Y/m/d H:i:s'); $obj->Send['TotalAmount'] = 2000; $obj->Send['TradeDesc'] = "good to drink"; $obj->Send['ChoosePayment'] = PaymentMethod::ALL;
//Because I am going to insert data into two tables at a time, so I use //Transaction of Laravel to prevent the possible inconsistency of two tables
//start transaction DB::beginTransaction();
//All those scripts below should be executed without errors, otherwise the whole action rollback try { $payment_service_order = newPaymentServiceOrders();
$payment_service_order->user_id = User::getUserID($request); //Payment service ID $payment_service_order->payment_service_id = $thirdPartyPaymentService->id; $payment_service_order->expiry_time = (newCarbon())->now()->addDay(1)->toDateTimeString(); $payment_service_order->MerchantID = env('MERCHANTID'); $payment_service_order->MerchantTradeNo = $MerchantTradeNo; $payment_service_order->MerchantTradeDate = $MerchantTradeDate; $payment_service_order->TotalAmount = $totalAmount; $payment_service_order->TradeDesc = $TradeDesc; //Item order number $payment_service_order->ItemName = $ordersName; $payment_service_order->save();
foreach ($ordersas$order) { $order_relations = newOrderRelations(); $order_relations->payment_service_id = $thirdPartyPaymentService->id; $order_relations->payment_service_order_id = $payment_service_order->id; $order_relations->order_id = $order->id; $order_relations->save(); } //Once any errors occur, the whole action stops and rollback, and provide customized error message } catch (Exception$e) { DB::rollBack();
returnHelpers::result('false', 'Something went wrong with DB', 400); } //If no errors occur, the whole action commit DB::commit();
Create an API for PaymentsController
Add new route in api.php under routes folder
Route::post('pay', 'PaymentsController@pay');
Create a simplest HTML
You could simply revise the content of default welcome.blade page as follows:
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
php artisan make:middleware TestLog
註冊middleware
In/app/Http/Kernel.php,register the middleware we just made
Think about it, if your API was accidentally leaked, and some developer knew it. He bought some items from your service, and called your API, how could you tell?
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.
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
if ($payerEmail !== null) Mail::to($payerEmail)->send(newPaymentReceived($paymentServiceOrder, $orderRelations));
return'1|OK'; }
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
Password:test1234
Refund
Refund example as follows:
public static function refund($order, $paymentServiceInstance, $orderRelation) { try { $obj = new AllInOne();
$obj->ServiceURL = "https://payment-stage.opay.tw/Cashier/AioChargeback"; // endpoint $obj->HashKey = env('HASHKEY'); // Hash key provided by AllPay $obj->HashIV = env('HASHIV'); // Hash IV provided by AllPay $obj->MerchantID = env('MERCHANTID'); // Merchant ID provided by AllPay $obj->EncryptType = EncryptType::ENC_SHA256; // CheckMacValue type. Stay 1 as SHA256
$obj->ChargeBack['MerchantTradeNo'] = $paymentServiceInstance->MerchantTradeNo; // The trade number you provided to AllPay
$obj->ChargeBack['TradeNo'] = $paymentServiceInstance->TradeNo; // The trade no provided by AllPay $obj->ChargeBack['ChargeBackTotalAmount'] = $order->total_amount; // Refunded amount $obj->AioChargeback();
} catch (Exception $e) { // If something wrong, return. return Helpers::result(true, 'Something wrong happened', 200); // Debug mode, print out the error echo $e->getMessage(); }
Comments