Yii 2.0 Merchant Cash Withdrawal Function of MYSQL Transaction Tutorial

  • 2021-12-09 10:20:22
  • OfStack

Preface

I am a halfway monk PHP programmer, so far, not counting in the training class learning time, has been writing code for two years. Perhaps due to business reasons, I have not used MySQL transactions in these two years. Just yesterday, there was a business about Alipay transfer that had to be completed by MySQL transaction. Others said a lot, but they still didn't understand what MySQL transaction was, so they started a new round of remedial classes. When they came out to mix, they always had to pay back the knowledge accounts they owed.

Let's briefly talk about the business I met yesterday. I want to launch an Alipay cash withdrawal business on the mobile side. I wrote this business in three steps before:

Step 1:

If I withdraw cash first, I must first write a cash withdrawal record in the cash withdrawal table, and then update the user balance table under 1. The records of these two data are accrued records, that is to say, their status is being processed;

Step 2:

Then request the third party interface of Alipay;

Step 3:

If the interface request is successful, after the user has received the money paid by Alipay, we are updating the status of the cash withdrawal record table and the data of the user balance table.

If the interface request fails and the user does not receive the money transferred to the user by Alipay, the status of the cash withdrawal record table is cash withdrawal failure.

However, if the user initiates cash withdrawal, the user's cash withholding business has been carried out, but the third-party Alipay interface request fails, and the power is suddenly cut off, the database status cannot be changed, and then the result is not what we expected. At this time, MySQL transaction shows its own value in this business.

MySQL transactions: The operations to be executed in Series 1 are called transactions, and transaction management is to manage that these operations are either completely executed or not executed at all, that is to say, the cash withdrawal business above me will either be executed successfully or not successfully, and there will be no half execution. The significance of MySQL transaction is to ensure the integrity of data. However, not all data engines in mysql support transaction management, only innodb supports transaction management.

Characteristics of transaction management:

1. Atomicity: The whole operation of a transaction is a whole, which cannot be divided, either all succeeds or all fails.

2. 1 Attribute: The data in the data table does not change before and after the transaction operation.

3. Isolation: Transaction operations are isolated from each other without being affected.

4. Persistence: Once the data is submitted, it cannot be changed, and the data in the data table is changed permanently.

Transaction management operations:

Turn on transaction management: When turned on, the following sql statement is not executed immediately and writes the results to the table, but to the transaction log.

start transaction;

Rollback operation: Rollback clears the contents written to the transaction log after transaction management started, that is, reverts to before transaction management started.

Syntax: rollback;

Note: The fallback operation only rolls back the contents of "write", and cannot be rolled back for ordinary select statements.

Transaction Commit: Writes the result of the sql statement to the data table.

Syntax: commit;

Sample code:


/***
 * Notes : Merchants pay cash withdrawal at merchants' end 
 * @Author : Dang Mengmeng
 * @Date : 2019/7/9 0009 10:01
 * @param $params
 * @return mixed
 * @throws BadRequestHttpException
 */
public function storeWith($params){
 try{
 $payee_account = $params['pay_account'];
 $amount = $params['amount_of_acc'];
 $request = new AlipayFundTransToaccountTransferRequest();
 $connection = Yii::$app->db;
 $transaction = $connection->beginTransaction(); // Start a transaction 
 // Write the initiation cash withdrawal record 
 $RES = StoreWith::updateWithData($x_id);
 $financeId = StoreWith::insertLog($params);
 $params['acc_id'] = $financeId;
 $paymentId = StoreWith::insertPaymentLog($params);
 $request->setBizContent("{" .
  "\"out_biz_no\":$orderId," .
  "\"payee_type\":\"ALIPAY_LOGONID\"," .
  "\"payee_account\":$payee_account," .
  "\"amount\":$amount," .
  "\"remark\":\" Withdrawal of income \"" .
  " }");
 $result = $aop->execute($request);
 $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
 $resultCode = $result->$responseNode->code;
 if(!empty($resultCode) && $resultCode == 10000){
  // Successful cash withdrawal 
  $status = 1;
  //1. Update posting fund data status 
  StoreWith::updateWithDataTwo($x_id,$status);
 } else {
  // Cash withdrawal failure 
  $status = 2;
  StoreWith::updateWithDataTwo($x_id,$status);
 }
 if($resultCode == 10000){
  $transaction->commit();// Transaction commit 
 }else{
  $transaction->rollBack();// Execution failed, transaction rolled back 
 }
 return $resultCode;
 }catch (\Exception $exception){
 throw new BadRequestHttpException($exception->getMessage());
 }
}

The knowledge account owed is always to be paid back. It is better to know early than to know later! ! !

Study sooner rather than later, and give it to everyone as early as possible. Ha ha ha! ! !

Summarize

Original link: https://www.cnblogs.com/honely/p/11156929. html


Related articles: