public async Task <CreatePaymentResponse> ReadCharge(string paymentId, string publicKey, string privateKey, bool sandboxMode) { bool isLive = !sandboxMode; // await this.GetIsLiveSetting(); IAffirmAPI affirmAPI = new AffirmAPI(_httpContextAccessor, _httpClient, isLive, _context); dynamic affirmResponse = await affirmAPI.ReadChargeAsync(publicKey, privateKey, paymentId); return(affirmResponse); }
/// <summary> /// After completing the checkout flow and receiving the checkout token, authorize the charge. /// Authorizing generates a charge ID that you’ll use to reference the charge moving forward. /// You must authorize a charge to fully create it. A charge is not visible in the Read response, /// nor in the merchant dashboard until you authorize it. /// </summary> /// <param name="paymentIdentifier"></param> /// <param name="token"></param> /// <param name="publicKey"></param> /// <param name="privateKey"></param> /// <returns></returns> public async Task <CreatePaymentResponse> Authorize(string paymentIdentifier, string token, string publicKey, string privateKey, string callbackUrl, int amount, string orderId, bool sandboxMode, string transactionId) { bool isLive = !sandboxMode; // await this.GetIsLiveSetting(); if (string.IsNullOrEmpty(orderId)) { orderId = paymentIdentifier; } IAffirmAPI affirmAPI = new AffirmAPI(_httpContextAccessor, _httpClient, isLive, _context); dynamic affirmResponse = await affirmAPI.AuthorizeAsync(publicKey, privateKey, token, orderId, transactionId); string paymentStatus = AffirmConstants.Vtex.Denied; if (affirmResponse.amount != null && affirmResponse.amount == amount) { paymentStatus = AffirmConstants.Vtex.Approved; _context.Vtex.Logger.Info("CreatePaymentResponse", null, $"PaymentResponse: {paymentStatus}"); } else //if (affirmResponse.status_code != null && affirmResponse.status_code == StatusCodes.Status403Forbidden.ToString() && affirmResponse.code != null && affirmResponse.code == AffirmConstants.TokenUsed) { if (affirmResponse.charge_id != null || affirmResponse.reference_id != null) { string chargeId = affirmResponse.charge_id ?? affirmResponse.reference_id; affirmResponse = await affirmAPI.ReadChargeAsync(publicKey, privateKey, chargeId); if (affirmResponse.status != null && affirmResponse.status == AffirmConstants.SuccessResponseCode) { paymentStatus = AffirmConstants.Vtex.Approved; _context.Vtex.Logger.Info("CreatePaymentResponse", null, $"PaymentResponse: {paymentStatus}"); } } } CreatePaymentResponse paymentResponse = new CreatePaymentResponse(); paymentResponse.paymentId = paymentIdentifier; paymentResponse.status = paymentStatus; paymentResponse.tid = affirmResponse.id ?? null; paymentResponse.authorizationId = affirmResponse.checkout_id ?? null; paymentResponse.code = affirmResponse.status ?? affirmResponse.status_code ?? affirmResponse.Error?.Code; string message = string.Empty; if (affirmResponse.events != null) { message = JsonConvert.SerializeObject(affirmResponse.events); } else if (affirmResponse.message != null) { message = affirmResponse.message; } else if (affirmResponse.Error != null && affirmResponse.Error.Message != null) { message = affirmResponse.Error.Message; } if (affirmResponse.fields != null) { message = $"{message}: {affirmResponse.fields}"; } paymentResponse.message = message; _context.Vtex.Logger.Info("PaymentResponse", null, $"PaymentResponse: {paymentResponse.message}"); // Save the Affirm id & order number - will need for capture CreatePaymentRequest paymentRequest = new CreatePaymentRequest { transactionId = affirmResponse.id, orderId = orderId }; // Don't save if data is missing if (!string.IsNullOrEmpty(paymentRequest.transactionId) && !string.IsNullOrEmpty(paymentRequest.orderId)) { await this._paymentRequestRepository.SavePaymentRequestAsync(paymentIdentifier, paymentRequest); await this._paymentRequestRepository.SavePaymentResponseAsync(paymentResponse); } await this._paymentRequestRepository.PostCallbackResponse(callbackUrl, paymentResponse); return(paymentResponse); }