-
Notifications
You must be signed in to change notification settings - Fork 531
Open
Description
I am generating the QR code to pay SOL using solana-pay SDK and while scanning QR code in solflare, I just added "https" url to get the payment status and if payment is not done then I am creating transaction but after getting response from API, it's getting error in solflare as "Request Failed". The QR code is generated for one time payment so please could you suggest where I am doing wrong and how should I implement. And if you have any reference, It'll be highly appreciated. Thanks
- QR code generation:
const baseUrl =
process.env.BASE_URL || 'https://218e5072ebc4.ngrok-free.app';
// This endpoint will serve the Transaction Request (GET + POST)
const transactionUrl = new URL(
`${baseUrl}/api/v1/solana-pay/transaction?network=devnet&reference=${requestId.toBase58()}`
);
console.log('transactionURL:::', transactionUrl);
// Build Solana Pay transaction request URL
// encodeURL already gives you: solana:<https-url>?label=...&message=...
const solanaPayUrl = encodeURL({
link: transactionUrl,
label: 'TagoCash Payment',
message: 'One-time payment',
}).toString();
// For external wallets: Generate QR code directly from the Solana Pay URL
const qrCodeDataUrl = await this.generateQRCode(solanaPayUrl);
-
Get API:
async getTransactionRequest(
reference: string,
account?: string
): Promise<any> {
try {
if (!reference) {
throw new BadRequestException('Reference parameter is required');
}
const request = await this.solanaPayRepo.getSolanaPayDetails({
id: reference,
});
if (!request) {
throw new BadRequestException('Payment request not found');
}
if (request.status === SOLANAPAYSTATUS.COMPLETED) {
throw new BadRequestException('Payment already completed');
}
if (request.status === SOLANAPAYSTATUS.EXPIRED) {
throw new BadRequestException('Payment request expired');
}
// Convert amount from wei to display format
let displayAmount = request.amount
? Number(weiToEth(request.amount as bigint))
: 0;
const responseData = {
label: request.label || 'TagoCash Payment',
icon: 'https://tagocash.com/icon.png',
// description: request.memo || 'One-time payment',
// amount: displayAmount,
// currency: request.currency,
// reference: reference,
// recipient: this.merchantWallet.toString(),
// memo: request.memo,
};
console.log('GET transaction response:', responseData);
return responseData;
} catch (error) {
console.error('Get transaction request failed:', error);
throw error;
}
}
3. POST API:
async createSolanaTransaction(
reference: string,
payerAccount: string,
res: Response
): Promise<any> {
try {
// Validate input parameters
if (!reference) {
throw new BadRequestException('Reference parameter is required');
}
if (!payerAccount) {
throw new BadRequestException('Account parameter is required');
}
// Validate payer account format
let payer: PublicKey;
try {
payer = new PublicKey(payerAccount);
} catch (error) {
throw new BadRequestException(
`Invalid account parameter: ${error.message}`
);
}
// Get payment request details
const request = await this.solanaPayRepo.getSolanaPayDetails({
id: reference,
});
if (!request) {
throw new BadRequestException('Payment request not found');
}
if (request.status === SOLANAPAYSTATUS.COMPLETED) {
throw new BadRequestException(
'Payment request already used - one-time payment completed'
);
}
if (request.status === SOLANAPAYSTATUS.EXPIRED) {
throw new BadRequestException('Payment request expired');
}
// Build a transfer instruction
const instruction = SystemProgram.transfer({
fromPubkey: payer,
toPubkey: new PublicKey('5yoD66g5SoKqnncxih19BCAjsS9gjBQzCZDC1Mbo9jiR'),
lamports: 0.01 * 1e9, // 0.01 SOL
});
// Attach reference as a readonly account
instruction.keys.push({
pubkey: new PublicKey(reference),
isSigner: false,
isWritable: false,
});
// Build transaction (unsigned)
const tx = new Transaction().add(instruction);
// ⚠️ Leave feePayer and blockhash unset → Solflare will replace them
tx.feePayer = payer;
const { blockhash, lastValidBlockHeight } =
await this.connection.getLatestBlockhash('confirmed');
tx.recentBlockhash = blockhash;
// Serialize unsigned tx
const serialized = tx.serialize({
requireAllSignatures: false,
});
const base64 = serialized.toString('base64');
console.log('base64::::::', base64);
// Respond per Solana Pay spec
res.json({
transaction: base64,
message: 'Pay 0.01 SOL to My dApp', // optional
});
} catch (error) {
console.error('Transaction creation failed:', error);
if (error instanceof BadRequestException) {
throw error;
}
throw new BadRequestException(
`Transaction creation failed: ${error.message || 'Unknown error'}`
);
}
}
these are code I am using to call using generated QR code if external wallets like solflare scans this QR code then the transaction should be created and complete.
but I am getting the message with error so please help me here
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
