how you im using your sdk and everything the code is working with my other merchant id. it works but i added a new business now below is the code
@csrf_exempt
def initiate_paynow_payment(request):
if request.method == ‘POST’:
try:
data = json.loads(request.body)
policy_id = data[‘policy_id’]
email = data[‘email’]
phone = data[‘phone’]
# Get the policy but don't create payment record yet
policy = InsurancePolicy.objects.get(id=policy_id)
# Calculate amount based on currency selection
payment_currency = data.get('payment_currency', 'USD')
if payment_currency == 'USD':
amount = float(policy.base_premium_usd)
description = f"Insurance Policy {policy.policy_number} - USD {amount}"
else:
amount = float(policy.premium_zwl)
description = f"Insurance Policy {policy.policy_number} - ZWL {amount}"
# Create PayNow payment
payment = paynow.create_payment(policy.policy_number, email)
payment.add(description, amount)
# Send payment to PayNow
response = paynow.send(payment)
print(f"PayNow response: {response.__dict__}")
if response.success:
print(f"Payment initiated successfully: {response.poll_url}")
print(f"Redirect URL: {response.redirect_url}")
# Only now create the payment record after successful initiation
currency = Currency.objects.get(name=payment_currency)
payment_record = Payment.objects.create(
policy_holder=policy.policy_holder,
amount=amount,
currency=currency,
payment_method='PAYNOW',
reference_number=f"PAYNOW-{datetime.now().strftime('%Y%m%d%H%M%S')} poll_url={response.poll_url}",
policy=policy,
payment_status='PENDING',
description=description,
poll_url=response.poll_url
)
print(f"Payment record created: {payment_record.id} for policy {policy.policy_number} paymet record {payment_record}")
# Store payment ID in session for status checks
request.session['current_payment_id'] = payment_record.id
return JsonResponse({
'success': True,
'payment_id': payment_record.id,
'redirect_url': response.redirect_url,
'instructions': 'Redirect to PayNow to complete payment',
'poll_url': response.poll_url
})
else:
return JsonResponse({
'success': False,
'error': response.error,
'message': 'Payment initiation failed'
}, status=400)
except InsurancePolicy.DoesNotExist:
return JsonResponse({'error': 'Policy not found'}, status=404)
except Currency.DoesNotExist:
return JsonResponse({'error': 'Currency not supported'}, status=400)
except Exception as e:
return JsonResponse({'error': str(e)}, status=500)
return JsonResponse({'error': 'Method not allowed'}, status=405)