<commit_before><?php

namespace Kopper\ReceiptVerification;

use Exception;
use Kopper\Config;
use Kopper\Environment;
use Kopper\Exception\NonFatalException;
use Kopper\Logger\Logger;
use Kopper\URLRequest;

class AppleReceiptVerification extends ReceiptVerification {

  const STORE_NAME = 'apple';
  const PRODUCTION_API = 'https://buy.itunes.apple.com/verifyReceipt';
  const SANDBOX_API = 'https://sandbox.itunes.apple.com/verifyReceipt';

  public function verify($id, $receipt) {
    $data = $this->decodeReceipt($receipt);

    if (empty($data->receipt) === true) {
      throw new NonFatalException('missing receipt');
    }

    if(Config::get('receiptVerification.sandbox') === true){
      $url = self::SANDBOX_API;
    }else{
      $url = Environment::is(Environment::PRODUCTION) ? self::PRODUCTION_API : self::SANDBOX_API;
    }
    $request = new URLRequest($url);

    $response = $request->postJSON(array('receipt-data' => $data->receipt), false);

    if (isset($response->status) === false || $response->status != 0) {
      throw new NonFatalException('invalid receipt - ' . $response->status, 400);
    }

    if (empty($response->receipt->in_app) === false) {
      foreach ($response->receipt->in_app as $purchasedItem) {
        if ($purchasedItem->product_id === $id) {
          return true;
        }
      }
    }
<commit_msg>handle single receipts as well<commit_after><?php

namespace Kopper\ReceiptVerification;

use Exception;
use Kopper\Config;
use Kopper\Environment;
use Kopper\Exception\NonFatalException;
use Kopper\Logger\Logger;
use Kopper\URLRequest;

class AppleReceiptVerification extends ReceiptVerification {

  const STORE_NAME = 'apple';
  const PRODUCTION_API = 'https://buy.itunes.apple.com/verifyReceipt';
  const SANDBOX_API = 'https://sandbox.itunes.apple.com/verifyReceipt';

  public function verify($id, $receipt) {
    $data = $this->decodeReceipt($receipt);

    if (empty($data->receipt) === true) {
      throw new NonFatalException('missing receipt');
    }

    if (Config::get('receiptVerification.sandbox') === true) {
      $url = self::SANDBOX_API;
    } else {
      $url = Environment::is(Environment::PRODUCTION) ? self::PRODUCTION_API : self::SANDBOX_API;
    }
    
    $request = new URLRequest($url);

    $response = $request->postJSON(array('receipt-data' => $data->receipt), false);

    if (isset($response->status) === false || $response->status != 0) {
      throw new NonFatalException('invalid receipt - ' . $response->status, 400);
    }

    if (empty($response->receipt->in_app) === false) {
      foreach ($response->receipt->in_app as $purchasedItem) {
        if ($purchasedItem->product_id === $id) {
          return true;
        }
      }
    } else if (empty($response->receipt->product_id) === false) {
      if ($response->receipt->product_id === $id) {
        return true;
      }
    }

{'fraction_non_alphanumeric': 0.15272474835126693, 'fraction_numerical': 0.0027768136063866713, 'mean_word_length': 3.701468189233279, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1791371', 'n_tokens_mistral': 1000, 'n_tokens_neox': 899, 'n_words': 174}