Paystack->testSecretKey; $url = "https://api.paystack.co/transaction/initialize"; $fields = [ 'email' => $email, 'amount' => $amount * 100 ]; $fields_string = http_build_query($fields); //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST, true); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Authorization: Bearer {$secretKey}", "Cache-Control: no-cache", )); //So that curl_exec returns the contents of the cURL; rather than echoing it curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); //execute post $result = curl_exec($ch); $result = json_decode($result, true); return [ 'status' => $result['status'], 'message' => $result['message'], 'data' => $result['data'], ]; } /** * 交易支付. * @param $ref * @return array */ public static function transactionVerify($ref): array { $payStack = \Yii::$app->Paystack; $transaction = $payStack->transaction()->setRequestOptions($ref); $transaction->verify(); return [ 'status' => $transaction->status, 'message' => $transaction->message, 'data' => $transaction->data, ]; } /** * 交易退款. * @param string $transaction * @param int $amount */ public static function transactionRefund(string $reference, int $amount) { $secretKey = \Yii::$app->Paystack->testSecretKey; $url = "https://api.paystack.co/refund"; $fields = [ 'transaction' => $reference, 'amount' => $amount, ]; $fields_string = http_build_query($fields); //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Authorization: Bearer {$secretKey}", "Cache-Control: no-cache", )); //So that curl_exec returns the contents of the cURL; rather than echoing it curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //execute post $response = curl_exec($ch); $response = json_decode($response, true); return [ 'status' => $response['status'], 'message' => $response['message'], 'data' => $response['data'], ]; } }