-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add razorpay #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bajrangCoder
wants to merge
9
commits into
main
Choose a base branch
from
razorpay
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
96a108d
feat: add razorpay
bajrangCoder 217fe81
fix
bajrangCoder 849a46a
fix: Razorpay webhook signature verification
bajrangCoder a12857e
fix
bajrangCoder 8df0a10
Merge branch 'main' into razorpay
bajrangCoder 8c2d836
Merge branch 'main' into razorpay
bajrangCoder 7a41d40
fix
bajrangCoder 4aa480d
Merge branch 'razorpay' of https://github.com/Acode-Foundation/acode.…
bajrangCoder f5a9156
Merge branch 'main' into razorpay
bajrangCoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| /** | ||
| * Razorpay Checkout Component | ||
| * Handles web-based plugin purchases via Razorpay payment gateway | ||
| */ | ||
|
|
||
| import alert from 'components/dialogs/alert'; | ||
| import Ref from 'html-tag-js/ref'; | ||
|
|
||
| // Load Razorpay script dynamically | ||
| let razorpayLoaded = false; | ||
| function loadRazorpayScript() { | ||
| return new Promise((resolve, reject) => { | ||
| if (razorpayLoaded) { | ||
| resolve(); | ||
| return; | ||
| } | ||
|
|
||
| const script = document.createElement('script'); | ||
| script.src = 'https://checkout.razorpay.com/v1/checkout.js'; | ||
| script.async = true; | ||
| script.onload = () => { | ||
| razorpayLoaded = true; | ||
| resolve(); | ||
| }; | ||
| script.onerror = () => reject(new Error('Failed to load Razorpay script')); | ||
| document.head.appendChild(script); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Check if user owns a plugin | ||
| * @param {string} pluginId | ||
| * @returns {Promise<boolean>} | ||
| */ | ||
| export async function checkPluginOwnership(pluginId) { | ||
| try { | ||
| const res = await fetch(`/api/razorpay/check-ownership/${pluginId}`); | ||
| const data = await res.json(); | ||
| return data.owned === true; | ||
| } catch (error) { | ||
| console.error('Failed to check plugin ownership:', error); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Razorpay checkout configuration | ||
| */ | ||
| const RAZORPAY_CONFIG = { | ||
| theme: { | ||
| color: '#2563eb', | ||
| backdrop_color: 'rgba(15, 23, 42, 0.8)', | ||
| }, | ||
| branding: { | ||
| name: 'Acode Plugin Store', | ||
| image: '/logo-512.png', | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Initiate Razorpay checkout for a plugin | ||
| * @param {string} pluginId | ||
| * @param {Object} userInfo - User information for prefill | ||
| * @param {string} [userInfo.email] - User's email address | ||
| * @param {string} [userInfo.name] - User's name | ||
| * @param {Function} onSuccess - Callback on successful payment | ||
| * @param {Function} [onCancel] - Callback when checkout is cancelled | ||
| * @returns {Promise<void>} | ||
| */ | ||
| export async function initiateCheckout(pluginId, userInfo = {}, onSuccess, onCancel) { | ||
| try { | ||
| // Load Razorpay script if not already loaded | ||
| await loadRazorpayScript(); | ||
|
|
||
| // Create order on server | ||
| const orderRes = await fetch('/api/razorpay/create-order', { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify({ pluginId }), | ||
| }); | ||
|
|
||
| const orderData = await orderRes.json(); | ||
|
|
||
| if (orderData.error) { | ||
| alert('ERROR', orderData.error); | ||
| return; | ||
| } | ||
|
|
||
| const { orderId, amount, currency, keyId, pluginName, userEmail } = orderData; | ||
|
|
||
| // Open Razorpay checkout with customization | ||
| const options = { | ||
| key: keyId, | ||
| amount, | ||
| currency, | ||
| name: RAZORPAY_CONFIG.branding.name, | ||
| description: `Purchase: ${pluginName}`, | ||
| image: RAZORPAY_CONFIG.branding.image, | ||
| order_id: orderId, | ||
| handler: async (response) => { | ||
| try { | ||
| const verifyRes = await fetch('/api/razorpay/verify', { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify({ | ||
| razorpay_order_id: response.razorpay_order_id, | ||
| razorpay_payment_id: response.razorpay_payment_id, | ||
| razorpay_signature: response.razorpay_signature, | ||
| pluginId, | ||
| }), | ||
| }); | ||
|
|
||
| if (!verifyRes.ok) throw new Error('Verification request failed'); | ||
|
|
||
| const verifyData = await verifyRes.json(); | ||
|
|
||
| if (verifyData.success) { | ||
| alert('SUCCESS', 'Payment successful! You can now download this plugin.'); | ||
| if (onSuccess) onSuccess(); | ||
| } else { | ||
| alert('ERROR', verifyData.error || 'Payment verification failed'); | ||
| } | ||
| } catch (error) { | ||
| console.error('Verification error:', error); | ||
| alert('ERROR', 'Payment may have succeeded but verification failed. Please contact support if charged.'); | ||
| } | ||
| }, | ||
| // Prefill user information (email preferred over contact for web) | ||
| prefill: { | ||
| email: userInfo.email || userEmail || '', | ||
| name: userInfo.name || '', | ||
| // Note: contact (phone) is required by Razorpay for Indian regulations | ||
| // but email will be shown as primary identifier | ||
| }, | ||
| // Theme customization | ||
| theme: { | ||
| color: RAZORPAY_CONFIG.theme.color, | ||
| backdrop_color: RAZORPAY_CONFIG.theme.backdrop_color, | ||
| }, | ||
| // Modal behavior | ||
| modal: { | ||
| confirm_close: true, // Ask before closing | ||
| escape: true, // Allow ESC to close | ||
| animation: true, // Enable animations | ||
| ondismiss: () => { | ||
| if (onCancel) onCancel(); | ||
| }, | ||
| }, | ||
| // Additional checkout preferences | ||
| notes: { | ||
| pluginId, | ||
| source: 'acode_web', | ||
| }, | ||
| }; | ||
|
|
||
| const rzp = new window.Razorpay(options); | ||
| rzp.on('payment.failed', (response) => { | ||
| alert('ERROR', `Payment failed: ${response.error.description}`); | ||
| }); | ||
| rzp.open(); | ||
| } catch (error) { | ||
| console.error('Checkout error:', error); | ||
| alert('ERROR', error.message || 'Failed to initiate checkout'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Buy Button Component for paid plugins | ||
| * @param {Object} props | ||
| * @param {string} props.pluginId - Plugin ID | ||
| * @param {number} props.price - Plugin price in INR | ||
| * @param {Object} [props.user] - Logged in user object | ||
| * @param {Function} [props.onPurchaseComplete] - Callback after successful purchase | ||
| * @returns {HTMLElement} | ||
| */ | ||
| export default function BuyButton({ pluginId, price, user, onPurchaseComplete }) { | ||
| const buttonRef = Ref(); | ||
| const buttonTextRef = Ref(); | ||
|
|
||
| const handleClick = async () => { | ||
| buttonRef.el.disabled = true; | ||
| buttonTextRef.el.textContent = 'Processing...'; | ||
|
|
||
| const handleSuccess = () => { | ||
| buttonTextRef.el.textContent = 'Purchased ✓'; | ||
| buttonRef.el.disabled = true; | ||
| if (onPurchaseComplete) onPurchaseComplete(); | ||
| }; | ||
|
|
||
| const handleCancel = () => { | ||
| buttonTextRef.el.textContent = `Buy ₹${price}`; | ||
| buttonRef.el.disabled = false; | ||
| }; | ||
|
|
||
| const userInfo = user ? { email: user.email, name: user.name } : {}; | ||
| await initiateCheckout(pluginId, userInfo, handleSuccess, handleCancel); | ||
| }; | ||
|
|
||
| return ( | ||
| <button ref={buttonRef} type='button' className='buy-button' onclick={handleClick}> | ||
| <span className='icon shopping_cart' /> | ||
| <span ref={buttonTextRef}>Buy ₹{price}</span> | ||
| </button> | ||
| ); | ||
| } | ||
bajrangCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.